Java Code Examples for com.fasterxml.jackson.core.JsonParser#getDecimalValue()
The following examples show how to use
com.fasterxml.jackson.core.JsonParser#getDecimalValue() .
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: DurationDeserializer.java From bootique with Apache License 2.0 | 6 votes |
@Override public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.getCurrentTokenId()) { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return Duration.ofSeconds(seconds, nanoseconds); case JsonTokenId.ID_NUMBER_INT: return Duration.ofSeconds(parser.getLongValue()); case JsonTokenId.ID_STRING: String string = parser.getText().trim(); if (string.length() == 0) { return null; } return Duration.parse(string); } throw context.mappingException("Expected type float, integer, or string."); }
Example 2
Source File: CoinDeserializer.java From consensusj with Apache License 2.0 | 6 votes |
@Override public Coin deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken token = p.getCurrentToken(); switch (token) { case VALUE_NUMBER_FLOAT: BigDecimal bd = p.getDecimalValue(); return BitcoinMath.btcToCoin(bd); case VALUE_NUMBER_INT: long val = p.getNumberValue().longValue(); // should be optimal, whatever it is return Coin.valueOf(val); default: return (Coin) ctxt.handleUnexpectedToken(Coin.class, p); } }
Example 3
Source File: MincodeParserSamplesTest.java From divolte-collector with Apache License 2.0 | 6 votes |
private static Object getCurrentValue(final JsonParser parser) throws IOException { final Object value; switch (parser.getCurrentToken()) { case VALUE_STRING: value = parser.getText(); break; case VALUE_TRUE: case VALUE_FALSE: value = parser.getBooleanValue(); break; case VALUE_NUMBER_FLOAT: case VALUE_NUMBER_INT: value = parser.getDecimalValue(); break; case VALUE_NULL: default: value = null; } return value; }
Example 4
Source File: DurationDeserializer.java From jackson-modules-java8 with Apache License 2.0 | 5 votes |
@Override public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.currentTokenId()) { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); return DecimalUtils.extractSecondsAndNanos(value, Duration::ofSeconds); case JsonTokenId.ID_NUMBER_INT: if(context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) { return Duration.ofSeconds(parser.getLongValue()); } return Duration.ofMillis(parser.getLongValue()); case JsonTokenId.ID_STRING: String string = parser.getText().trim(); if (string.length() == 0) { if (!isLenient()) { return _failForNotLenient(parser, context, JsonToken.VALUE_STRING); } return null; } try { return Duration.parse(string); } catch (DateTimeException e) { return _handleDateTimeException(context, e, string); } case JsonTokenId.ID_EMBEDDED_OBJECT: // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily return (Duration) parser.getEmbeddedObject(); case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(parser, context); } return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT); }
Example 5
Source File: SdkPojoDeserializer.java From cloudformation-cli-java-plugin with Apache License 2.0 | 4 votes |
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException { MarshallingType<?> type = field.marshallingType(); switch (p.currentToken()) { case VALUE_FALSE: case VALUE_TRUE: { if (type.equals(MarshallingType.BOOLEAN)) { return p.getBooleanValue(); } throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value"); } case VALUE_NUMBER_FLOAT: case VALUE_NUMBER_INT: { if (type.equals(MarshallingType.INTEGER)) { return p.getIntValue(); } else if (type.equals(MarshallingType.LONG)) { return p.getLongValue(); } else if (type.equals(MarshallingType.FLOAT)) { return p.getFloatValue(); // coerce should work } else if (type.equals(MarshallingType.DOUBLE)) { return p.getDoubleValue(); // coerce should work } else if (type.equals(MarshallingType.BIG_DECIMAL)) { return p.getDecimalValue(); // coerce should work } else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class)); return deser.deserialize(p, ctxt); } throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant"); } case VALUE_STRING: { if (type.equals(MarshallingType.STRING)) { return p.getText(); } else if (type.equals(MarshallingType.SDK_BYTES)) { byte[] bytes = p.getBinaryValue(); return SdkBytes.fromByteArray(bytes); } throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes"); } case START_OBJECT: { if (type.equals(MarshallingType.MAP)) { return readMap(field, p, ctxt); } else if (type.equals(MarshallingType.SDK_POJO)) { return readPojo(field.constructor().get(), p, ctxt); } throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo"); } case START_ARRAY: { if (type.equals(MarshallingType.LIST)) { return readList(field, p, ctxt); } throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type"); } case VALUE_NULL: return null; default: throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken()); } }
Example 6
Source File: InstantDeserializer.java From icure-backend with GNU General Public License v2.0 | 4 votes |
@Override public Instant deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { BigDecimal val = jp.getDecimalValue(); return getInstant(val); }
Example 7
Source File: SimpleValueReader.java From jackson-jr with Apache License 2.0 | 4 votes |
@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)); }