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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getCurrentTokenId() . 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: Bean2Deserializer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public Bean2 deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
	Bean2 bean2 = new Bean2();
	p.nextToken();
	do {
		String propName = p.getCurrentName();
		if (propName == null) {
			break;
		}
		if (p.getCurrentTokenId() != JsonTokenId.ID_FIELD_NAME && propName.equals("p1")) {
			bean2.setP1(p.getText());
		} else if (p.getCurrentTokenId() != JsonTokenId.ID_FIELD_NAME && propName.equals("p2")) {
			bean2.setP2(new BigDecimal(p.getText()));
		}
	} while (p.nextToken() != null);
	return bean2;
}
 
Example 2
Source File: DurationDeserializer.java    From bootique with Apache License 2.0 6 votes vote down vote up
@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.");
}