Java Code Examples for com.fasterxml.jackson.core.JsonToken#VALUE_NUMBER_INT

The following examples show how to use com.fasterxml.jackson.core.JsonToken#VALUE_NUMBER_INT . 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: CustomLocalDateTimeDeserializer.java    From mall with Apache License 2.0 6 votes vote down vote up
@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (DATE_TIME_FORMATTER_PATTERN.length() == str.length()) {
            return LocalDateTime.parse(str, DATE_TIME_FORMATTER);
        } else if (DATE_TIME_FORMATTER_MILLI_PATTERN.length() == str.length()) {
            return LocalDateTime.parse(str, DATE_TIME_FORMATTER_MILLI);
        }
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new Timestamp(jp.getLongValue()).toLocalDateTime();
    }
    throw ctxt.mappingException(handledType());
}
 
Example 2
Source File: WriteHolders.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public void write(IntWriter writer, JsonToken token, JsonParser parser) throws IOException {
  if (token == JsonToken.VALUE_NUMBER_INT) {
    writer.writeInt(parser.getIntValue());
  } else {
    try {
      writer.writeInt(Integer.parseInt(parser.getValueAsString()));
    } catch (Exception ex1){
      try {
        // it is possible that we need to coerce a float to an integer.
        writer.writeInt((int) Double.parseDouble(parser.getValueAsString()));
      } catch (Exception ex2){
        throw ex1;
      }
    }
  }
}
 
Example 3
Source File: ParseSupport.java    From curiostack with MIT License 6 votes vote down vote up
/** Parsers an int32 value out of the input. */
public static int parseInt32(JsonParser parser) throws IOException {
  JsonToken token = parser.currentToken();
  if (token == JsonToken.VALUE_NUMBER_INT) {
    // Use optimized code path for integral primitives, the normal case.
    return parser.getIntValue();
  }
  // JSON doesn't distinguish between integer values and floating point values so "1" and
  // "1.000" are treated as equal in JSON. For this reason we accept floating point values for
  // integer fields as well as long as it actually is an integer (i.e., round(value) == value).
  try {
    BigDecimal value =
        new BigDecimal(
            parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
    return value.intValueExact();
  } catch (Exception e) {
    throw new InvalidProtocolBufferException("Not an int32 value: " + parser.getText());
  }
}
 
Example 4
Source File: JsonldBeanDeserializerModifier.java    From jackson-jsonld with MIT License 6 votes vote down vote up
private Object parseJsonldObject(JsonParser jp) throws IOException {
    Object rval = null;
    final JsonToken initialToken = jp.getCurrentToken();

    if (initialToken == JsonToken.START_ARRAY) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(List.class);
    } else if (initialToken == JsonToken.START_OBJECT) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Map.class);
    } else if (initialToken == JsonToken.VALUE_STRING) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(String.class);
    } else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Boolean.class);
    } else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT
            || initialToken == JsonToken.VALUE_NUMBER_INT) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Number.class);
    } else if (initialToken == JsonToken.VALUE_NULL) {
        rval = null;
    }
    return rval;
}
 
Example 5
Source File: JsonXContentParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Object objectBytes() throws IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == JsonToken.VALUE_STRING) {
        return utf8Bytes();
    } else if (currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT) {
        return parser.getNumberValue();
    } else if (currentToken == JsonToken.VALUE_TRUE) {
        return Boolean.TRUE;
    } else if (currentToken == JsonToken.VALUE_FALSE) {
        return Boolean.FALSE;
    } else if (currentToken == JsonToken.VALUE_NULL) {
        return null;
    } else {
        //TODO should this really do UTF-8 conversion?
        return utf8Bytes();
    }
}
 
Example 6
Source File: JsonXContentParser.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Object objectText() throws IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == JsonToken.VALUE_STRING) {
        return text();
    } else if (currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT) {
        return parser.getNumberValue();
    } else if (currentToken == JsonToken.VALUE_TRUE) {
        return Boolean.TRUE;
    } else if (currentToken == JsonToken.VALUE_FALSE) {
        return Boolean.FALSE;
    } else if (currentToken == JsonToken.VALUE_NULL) {
        return null;
    } else {
        return text();
    }
}
 
Example 7
Source File: IrregularTimeSeriesIndex.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
public static IrregularTimeSeriesIndex parseJson(JsonParser parser) {
    Objects.requireNonNull(parser);
    JsonToken token;
    try {
        TLongArrayList times = new TLongArrayList();
        while ((token = parser.nextToken()) != null) {
            if (token == JsonToken.VALUE_NUMBER_INT) {
                times.add(parser.getLongValue());
            } else if (token == JsonToken.END_ARRAY) {
                return new IrregularTimeSeriesIndex(times.toArray());
            }
        }
        throw new IllegalStateException("Should not happen");
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 8
Source File: JsonDatabaseReader.java    From dbsync with Apache License 2.0 6 votes vote down vote up
Map<String, Object> readRecord(JsonParser jp) throws IOException {

        Map<String, Object> record = new HashMap<>();
        String fielName =  "";
        Object value;
        JsonToken token;

        while((token = jp.nextToken()) != JsonToken.END_OBJECT) {
            if (token == JsonToken.FIELD_NAME) {
                fielName = jp.getCurrentName();
            } else if (token == JsonToken.VALUE_STRING) {
                value = jp.getValueAsString();
                record.put(fielName, value);
            } else if (token == JsonToken.VALUE_NUMBER_INT) {
                value = jp.getValueAsLong();
                record.put(fielName, value);
            }
        }

        //System.out.println(record.toString());
        return record;
    }
 
Example 9
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
protected long _fetchLong(JsonParser p) throws IOException
{
    JsonToken t = p.currentToken();
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return p.getLongValue();
    }
    throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct "
            +_valueType.getName()+") from "+_tokenDesc(p, t));
}
 
Example 10
Source File: DurationSerializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
protected JsonToken serializationShape(SerializerProvider provider) {
    if (useTimestamp(provider)) {
        if (useNanoseconds(provider)) {
            return JsonToken.VALUE_NUMBER_FLOAT;
        }
        return JsonToken.VALUE_NUMBER_INT;
    }
    return JsonToken.VALUE_STRING;
}
 
Example 11
Source File: IntegerNodeCalc.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
static NodeCalc parseJson(JsonParser parser) throws IOException {
    JsonToken token;
    while ((token = parser.nextToken()) != null) {
        if (token == JsonToken.VALUE_NUMBER_INT) {
            return new IntegerNodeCalc(parser.getIntValue());
        } else {
            throw NodeCalc.createUnexpectedToken(token);
        }
    }
    throw new TimeSeriesException("Invalid integer node calc JSON");
}
 
Example 12
Source File: CustomDateTimeDeserializer.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        return ISODateTimeFormat.dateTimeParser().parseDateTime(str);
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new DateTime(jp.getLongValue());
    }
    throw ctxt.mappingException(handledType());
}
 
Example 13
Source File: ISO8601LocalDateDeserializer.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        return ISODateTimeFormat.dateTimeParser().parseDateTime(str).toLocalDate();
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new LocalDate(jp.getLongValue());
    }
    throw ctxt.mappingException(handledType());
}
 
Example 14
Source File: BigDecimalNodeCalc.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
static NodeCalc parseJson(JsonParser parser) throws IOException {
    JsonToken token;
    while ((token = parser.nextToken()) != null) {
        if (token == JsonToken.VALUE_NUMBER_INT) {
            return new BigDecimalNodeCalc(BigDecimal.valueOf(parser.getLongValue()));
        } else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
            return new BigDecimalNodeCalc(BigDecimal.valueOf(parser.getDoubleValue()));
        } else {
            throw NodeCalc.createUnexpectedToken(token);
        }
    }
    throw new TimeSeriesException("Invalid big decimal node calc JSON");
}
 
Example 15
Source File: Offsets.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public Offsets deserialize(JsonParser aJp, DeserializationContext aCtxt)
    throws IOException
{
    Offsets offsets = new Offsets();

    if (aJp.getCurrentToken() != JsonToken.START_ARRAY) {
        aCtxt.mappingException("Expecting array begin");
    }

    if (aJp.nextToken() == JsonToken.VALUE_NUMBER_INT) {
        offsets.begin = aJp.getIntValue();
    }
    else {
        aCtxt.mappingException("Expecting begin offset as integer");
    }

    if (aJp.nextToken() == JsonToken.VALUE_NUMBER_INT) {
        offsets.end = aJp.getIntValue();
    }
    else {
        aCtxt.mappingException("Expecting end offset as integer");
    }

    if (aJp.getCurrentToken() != JsonToken.END_ARRAY) {
        aCtxt.mappingException("Expecting array end");
    }

    return offsets;
}
 
Example 16
Source File: ParseSupport.java    From curiostack with MIT License 5 votes vote down vote up
/** Parses a long out of the input, using the optimized path when the value is not quoted. */
private static long parseLong(JsonParser parser) throws IOException {
  if (parser.currentToken() == JsonToken.VALUE_NUMBER_INT) {
    return parser.getLongValue();
  }
  return NumberInput.parseLong(parser.getText());
}
 
Example 17
Source File: WriteHolders.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void write(Float4Writer writer, JsonToken token, JsonParser parser) throws IOException {
  if (token == JsonToken.VALUE_NUMBER_FLOAT || token == JsonToken.VALUE_NUMBER_INT) {
    writer.writeFloat4(parser.getFloatValue());
  } else {
    writer.writeFloat4(Float.parseFloat(parser.getValueAsString()));
  }
}
 
Example 18
Source File: SimpleTypeCborUnmarshallers.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
public BigInteger unmarshall(JsonUnmarshallerContext unmarshallerContext) throws Exception {
    JsonParser parser = unmarshallerContext.getJsonParser();
    JsonToken current = parser.getCurrentToken();
    if (current == JsonToken.VALUE_NUMBER_INT) {
        return parser.getBigIntegerValue();
    } else if (current == JsonToken.VALUE_EMBEDDED_OBJECT) {
        Object embedded = parser.getEmbeddedObject();
        return new BigInteger((byte[]) embedded);
    } else {
        throw new SdkClientException("Invalid BigInteger Format.");
    }
}
 
Example 19
Source File: ConstantTensorJsonValidator.java    From vespa with Apache License 2.0 5 votes vote down vote up
private void validateTensorValue() throws IOException {
    JsonToken token = parser.nextToken();

    if (token != JsonToken.VALUE_NUMBER_FLOAT && token != JsonToken.VALUE_NUMBER_INT) {
        throw new InvalidConstantTensor(parser, String.format("Tensor value is not a number (%s)", token.toString()));
    }
}
 
Example 20
Source File: BsonParser.java    From immutables with Apache License 2.0 5 votes vote down vote up
private JsonToken toJsonToken(BsonType type) {
  switch (type) {
    case END_OF_DOCUMENT:
      reader.readEndDocument();
      return JsonToken.END_OBJECT;
    case DOCUMENT:
      reader.readStartDocument();
      return JsonToken.START_OBJECT;
    case ARRAY:
      reader.readStartArray();
      return JsonToken.START_ARRAY;
    case OBJECT_ID:
      return JsonToken.VALUE_EMBEDDED_OBJECT;
    case BOOLEAN:
      final boolean value  = reader.readBoolean();
      return value ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE;
    case DATE_TIME:
      return JsonToken.VALUE_EMBEDDED_OBJECT;
    case NULL:
      reader.readNull();
      return JsonToken.VALUE_NULL;
    case REGULAR_EXPRESSION:
      return JsonToken.VALUE_EMBEDDED_OBJECT;
    case SYMBOL:
    case STRING:
      return JsonToken.VALUE_STRING;
    case INT32:
    case INT64:
      return JsonToken.VALUE_NUMBER_INT;
    case DECIMAL128:
      return JsonToken.VALUE_NUMBER_FLOAT;
    case DOUBLE:
      return JsonToken.VALUE_NUMBER_FLOAT;
    case BINARY:
      return JsonToken.VALUE_EMBEDDED_OBJECT;
    default:
      throw new IllegalStateException(String.format("Unknown type %s", type));
  }
}