Java Code Examples for com.fasterxml.jackson.core.JsonParser#getValueAsBoolean()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getValueAsBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSampler.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Get Node type from the parser
 *
 * @param jsonParser
 * @return Node - return Node type based on json token
 */
private Node getValue( JsonParser jsonParser, String key ) {
  try {
    switch ( jsonParser.currentToken() ) {
      case START_OBJECT:
        return new ObjectNode( key );
      case START_ARRAY:
        return new ArrayNode( key );
      case VALUE_STRING:
        return new ValueNode<>( key, jsonParser.getValueAsString() );
      case VALUE_TRUE:
      case VALUE_FALSE:
        return new ValueNode<>( key, jsonParser.getValueAsBoolean() );
      case VALUE_NULL:
        return new ValueNode<>( key, null );
      case VALUE_NUMBER_FLOAT:
        return new ValueNode<>( key, jsonParser.getValueAsDouble() );
      case VALUE_NUMBER_INT:
        return new ValueNode<>( key, jsonParser.getValueAsInt() );
    }
  } catch ( IOException ioe ) {
    return null;
  }
  return null;
}
 
Example 2
Source File: BaseDeserializer.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Helper used to extract named boolean fields from the json parser in a streaming fashion.
 *
 * @param jparser The parser to use for extraction.
 * @param expectedFieldName The expected name of the next field in the stream.
 * @return The boolean representation of the requested field.
 * @throws IOException If there is an error parsing the field.
 */
protected boolean getNextBoolField(final JsonParser jparser, final String expectedFieldName)
        throws IOException
{
    assertFieldName(jparser, expectedFieldName);

    //move to the value token
    jparser.nextToken();
    return jparser.getValueAsBoolean();
}
 
Example 3
Source File: StdCallbackContext.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
private Object readObject(JsonParser p, DeserializationContext ctxt) throws IOException,
    NoSuchMethodException,
    InvocationTargetException {
    Object val = null;
    JsonToken next = p.currentToken();
    switch (next) {
        // Primitive Types
        case VALUE_TRUE:
        case VALUE_FALSE:
            val = p.getValueAsBoolean();
            break;

        case VALUE_STRING:
            val = p.getText();
            break;

        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            val = p.getNumberValue();
            break;

        // Encoded Object information
        case START_ARRAY:
            val = readEncoded(p, ctxt);
            break;

        default:
            throw new JsonParseException(p, "Object encoding not understood " + next);
    }
    return val;
}
 
Example 4
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
@Override
public Object readNext(JSONReader reader, JsonParser p) throws IOException
{
    // NOTE: only cases where we can optimize
    switch (_typeId) {
    // Textual types, related:
    case SER_STRING:
    case SER_CHARACTER_SEQUENCE:
        return _nextString(p);

    case SER_CHAR_ARRAY:
        String str = _nextString(p);
        return (str == null) ? null : str.toCharArray();

    // Number types:

    case SER_NUMBER_SHORT: // fall through
        return Short.valueOf((short) _nextInt(p));

    case SER_NUMBER_INTEGER:
        return Integer.valueOf(_nextInt(p));

    case SER_NUMBER_LONG:
        return Long.valueOf(_nextLong(p));

    // Other scalar types:

    case SER_BOOLEAN:
        {
            Boolean b = p.nextBooleanValue();
            if (b != null) {
                return b;
            }
            return p.getValueAsBoolean();
        }
    }

    p.nextToken();
    return read(reader, p);
}
 
Example 5
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static Boolean getBooleanValue(JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t !=  JsonToken.VALUE_TRUE && t != JsonToken.VALUE_FALSE) {
        throw new RuntimeException("Error while reading field '" + p.getCurrentName() + "'. Expected boolean value [" + t + "]");
    }
    return p.getValueAsBoolean();
}
 
Example 6
Source File: ParseBool.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Boolean parse(JsonParser parser, Field field) throws IOException {
    return isValid(parser) ? parser.getValueAsBoolean() : null;
}
 
Example 7
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
@Override
    public Object read(JSONReader reader, JsonParser p) throws IOException
    {
        switch (_typeId) {

        case SER_INT_ARRAY:
            return _readIntArray(p);

        case SER_TREE_NODE:
            return reader.readTree();

        // Textual types, related:
        case SER_STRING:
        case SER_CHARACTER_SEQUENCE:
            return p.getValueAsString();
        case SER_CHAR_ARRAY:
            return p.getValueAsString().toCharArray();
        case SER_BYTE_ARRAY:
            return _readBinary(p);

        // Number types:
            
        case SER_NUMBER_FLOAT: // fall through
            return Float.valueOf((float) p.getValueAsDouble());
        case SER_NUMBER_DOUBLE:
            return p.getValueAsDouble();

        case SER_NUMBER_BYTE: // fall through
            return (byte) p.getValueAsInt();
            
        case SER_NUMBER_SHORT: // fall through
            return (short) p.getValueAsInt();
        case SER_NUMBER_INTEGER:
            return p.getValueAsInt();
        case SER_NUMBER_LONG:
            return p.getValueAsLong();

        case SER_NUMBER_BIG_DECIMAL:
            return p.getDecimalValue();

        case SER_NUMBER_BIG_INTEGER:
            return p.getBigIntegerValue();

        // Other scalar types:

        case SER_BOOLEAN:
            return p.getValueAsBoolean();
            
        case SER_CHAR:
            {
                String str = p.getValueAsString();
                return (str == null || str.isEmpty()) ? ' ' : str.charAt(0);
            }
            
        case SER_CALENDAR:
            {
                long l = _fetchLong(p);
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(l);
                return cal;
            }

        case SER_DATE:
            return new Date(_fetchLong(p));

        case SER_CLASS:
        {
            String v = p.getValueAsString();
            try {
                return Class.forName(v);
            } catch (Exception e) {
                throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
            }
        }
        case SER_FILE:
            return new File(p.getValueAsString());
        case SER_UUID:
            return UUID.fromString(p.getValueAsString());
        case SER_URL:
            return new URL(p.getValueAsString());
        case SER_URI:
        
            return URI.create(p.getValueAsString());

//        case SER_MAP:
//        case SER_LIST:
//        case SER_COLLECTION:
//        case SER_OBJECT_ARRAY:
            // should never get here: we have dedicated readers
        default: // types that shouldn't get here
        //case SER_ENUM:
        }
        
        throw JSONObjectException.from(p,
                "Can not create a "+_valueType.getName()+" instance out of "+_tokenDesc(p));
    }