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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getValueAsInt() . 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 integer 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 integer representation of the requested field.
 * @throws IOException If there is an error parsing the field.
 */
protected int getNextIntField(JsonParser jparser, String expectedFieldName)
        throws IOException
{
    assertFieldName(jparser, expectedFieldName);

    //move to the value token
    jparser.nextToken();
    return jparser.getValueAsInt();
}
 
Example 3
Source File: LocalDateTimeBaseDeserializer.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
private int getInt(JsonParser parser, DeserializationContext context, int defaultValue, boolean required) throws IOException {
    if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
        int value = parser.getValueAsInt();
        parser.nextToken();
        return value;
    } else {
        if (required) {
            throw context.wrongTokenException(parser, _valueClass, JsonToken.VALUE_NUMBER_INT, "");
        } else {
            return defaultValue;
        }
    }
}
 
Example 4
Source File: JsonTerminologyIO.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private static Object readTokenObject(JsonParser jp, JsonToken token) throws IOException {
	switch (token) {
	case VALUE_NUMBER_INT: return jp.getValueAsInt();
	case VALUE_NUMBER_FLOAT: return jp.getValueAsDouble();
	case VALUE_FALSE: return false;
	case VALUE_TRUE: return true;
	case VALUE_NULL: return null;
	case VALUE_STRING: return jp.getValueAsString();
	default:
		throw new IllegalStateException("Token type not allowed in value collections: " + token);
	}
}
 
Example 5
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
private final int _nextInt(JsonParser p) throws IOException {
    int i = p.nextIntValue(-2);
    if (i != -2) {
        return i;
    }
    return p.getValueAsInt();
}
 
Example 6
Source File: RealmsConfigurationLoader.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private static Integer getIntegerValue(JsonParser p) throws IOException {
    JsonToken t = p.nextToken();
    if (t != JsonToken.VALUE_NUMBER_INT) {
        throw new RuntimeException("Error while reading field '" + p.getCurrentName() + "'. Expected integer value [" + t + "]");
    }
    return p.getValueAsInt();
}
 
Example 7
Source File: GenesisFile.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
public static GenesisFile read(byte[] contents) throws IOException {
  JsonFactory factory = new JsonFactory();
  JsonParser parser = factory.createParser(contents);

  int chainId = 0;
  String nonce = null;
  String difficulty = null;
  String mixhash = null;
  String coinbase = null;
  String timestamp = null;
  String extraData = null;
  String gasLimit = null;
  String parentHash = null;
  Map<String, String> allocs = null;
  List<Long> forks = new ArrayList<>();
  while (!parser.isClosed()) {
    JsonToken jsonToken = parser.nextToken();
    if (JsonToken.FIELD_NAME.equals(jsonToken)) {
      String fieldName = parser.getCurrentName();

      parser.nextToken();

      if ("nonce".equalsIgnoreCase(fieldName)) {
        nonce = parser.getValueAsString();
      } else if ("difficulty".equalsIgnoreCase(fieldName)) {
        difficulty = parser.getValueAsString();
      } else if ("mixHash".equalsIgnoreCase(fieldName)) {
        mixhash = parser.getValueAsString();
      } else if ("coinbase".equalsIgnoreCase(fieldName)) {
        coinbase = parser.getValueAsString();
      } else if ("gasLimit".equalsIgnoreCase(fieldName)) {
        gasLimit = parser.getValueAsString();
      } else if ("timestamp".equalsIgnoreCase(fieldName)) {
        timestamp = parser.getValueAsString();
      } else if ("extraData".equalsIgnoreCase(fieldName)) {
        extraData = parser.getValueAsString();
      } else if ("parentHash".equalsIgnoreCase(fieldName)) {
        parentHash = parser.getValueAsString();
      } else if ("alloc".equalsIgnoreCase(fieldName)) {
        allocs = readAlloc(parser);
      } else if ("chainId".equalsIgnoreCase(fieldName)) {
        chainId = parser.getValueAsInt();
      } else if (fieldName.contains("Block")) {
        forks.add(parser.getValueAsLong());
      }
    }

  }
  Collections.sort(forks);
  return new GenesisFile(
      nonce,
      difficulty,
      mixhash,
      coinbase,
      timestamp,
      extraData,
      gasLimit,
      parentHash,
      allocs,
      chainId,
      forks);
}
 
Example 8
Source File: TriggerWhen.java    From mattermost4j with Apache License 2.0 4 votes vote down vote up
@Override
public TriggerWhen deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  int jsonValue = p.getValueAsInt();
  return of(jsonValue);
}
 
Example 9
Source File: IntToBooleanDeserializer.java    From testrail-api-java-client with MIT License 4 votes vote down vote up
@Override
public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    return jp.getValueAsInt(0) <= 0 ? Boolean.FALSE : Boolean.TRUE;
}
 
Example 10
Source File: UnixTimestampModule.java    From testrail-api-java-client with MIT License 4 votes vote down vote up
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    return new Date(jp.getValueAsInt() * 1000L);
}
 
Example 11
Source File: ParseInt.java    From robe with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Integer parse(JsonParser parser, Field field) throws IOException {
    return isValid(parser) ? new Integer(parser.getValueAsInt()) : null;
}
 
Example 12
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));
    }