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

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getEmbeddedObject() . 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: YearDeserializer.java    From jackson-modules-java8 with Apache License 2.0 6 votes vote down vote up
@Override
public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    JsonToken t = parser.currentToken();
    if (t == JsonToken.VALUE_STRING) {
        String string = parser.getValueAsString().trim();
        try {
            if (_formatter == null) {
                return Year.parse(string);
            }
            return Year.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return Year.of(parser.getIntValue());
    }
    if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
        return (Year) parser.getEmbeddedObject();
    }
    if (parser.hasToken(JsonToken.START_ARRAY)){
        return _deserializeFromArray(parser, context);
    }
    return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
}
 
Example 2
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 3
Source File: DurationDeserializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: JavaxServletPartDeserializer.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Override
public Part deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
  final Object currentValue = p.getEmbeddedObject();
  return (Part) currentValue;
}
 
Example 5
Source File: YearMonthDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public YearMonth deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    if (parser.hasToken(JsonToken.VALUE_STRING)) {
        String string = parser.getText().trim();
        if (string.length() == 0) {
            if (!isLenient()) {
                return _failForNotLenient(parser, context, JsonToken.VALUE_STRING);
            }
            return null;
        }
        try {
            return YearMonth.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (parser.isExpectedStartArrayToken()) {
        JsonToken t = parser.nextToken();
        if (t == JsonToken.END_ARRAY) {
            return null;
        }
        if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT)
                && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
            final YearMonth parsed = deserialize(parser, context);
            if (parser.nextToken() != JsonToken.END_ARRAY) {
                handleMissingEndArrayForSingle(parser, context);
            }
            return parsed;            
        }
        if (t != JsonToken.VALUE_NUMBER_INT) {
            _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "years");
        }
        int year = parser.getIntValue();
        int month = parser.nextIntValue(-1);
        if (month == -1) {
            if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
                _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "months");
            }
            month = parser.getIntValue();
        }
        if (parser.nextToken() != JsonToken.END_ARRAY) {
            throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
                    "Expected array to end");
        }
        return YearMonth.of(year, month);
    }
    if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
        return (YearMonth) parser.getEmbeddedObject();
    }
    return _handleUnexpectedToken(context, parser,
            JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
}
 
Example 6
Source File: MonthDayDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public MonthDay deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    if (parser.hasToken(JsonToken.VALUE_STRING)) {
        String string = parser.getValueAsString().trim();
        try {
            if (_formatter == null) {
                return MonthDay.parse(string);
            }
            return MonthDay.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (parser.isExpectedStartArrayToken()) {
        JsonToken t = parser.nextToken();
        if (t == JsonToken.END_ARRAY) {
            return null;
        }
        if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT)
                && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
            final MonthDay parsed = deserialize(parser, context);
            if (parser.nextToken() != JsonToken.END_ARRAY) {
                handleMissingEndArrayForSingle(parser, context);
            }
            return parsed;
        }
        if (t != JsonToken.VALUE_NUMBER_INT) {
            _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "month");
        }
        int month = parser.getIntValue();
        int day = parser.nextIntValue(-1);
        if (day == -1) {
            if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
                _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "day");
            }
            day = parser.getIntValue();
        }
        if (parser.nextToken() != JsonToken.END_ARRAY) {
            throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
                    "Expected array to end");
        }
        return MonthDay.of(month, day);
    }
    if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
        return (MonthDay) parser.getEmbeddedObject();
    }
    return _handleUnexpectedToken(context, parser,
            JsonToken.VALUE_STRING, JsonToken.START_ARRAY);
}
 
Example 7
Source File: JSR310StringParsableDeserializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
{
    if (p.hasToken(JsonToken.VALUE_STRING)) {
        final String text = p.getText().trim();
        if (text.length() == 0) {
            CoercionAction act = ctxt.findCoercionAction(logicalType(), _valueClass,
                    CoercionInputShape.EmptyString);
            if (act == CoercionAction.Fail) {
                ctxt.reportInputMismatch(this,
    "Cannot coerce empty String (\"\") to %s (but could if enabling coercion using `CoercionConfig`)",
    _coercedTypeDesc());
            }
            if (act == CoercionAction.AsEmpty) {
                return getEmptyValue(ctxt);
            }
            // None of the types has specific null value
            return null;
        }
        try {
            switch (_typeSelector) {
            case TYPE_PERIOD:
                return Period.parse(text);
            case TYPE_ZONE_ID:
                return ZoneId.of(text);
            case TYPE_ZONE_OFFSET:
                return ZoneOffset.of(text);
            }
        } catch (DateTimeException e) {
            return _handleDateTimeException(ctxt, e, text);
        }
    }
    if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) {
        // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded
        //    values quite easily
        return p.getEmbeddedObject();
    }
    if (p.hasToken(JsonToken.START_ARRAY)){
        return _deserializeFromArray(p, ctxt);
    }
    
    throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, null);
}
 
Example 8
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public OInterval deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  return (OInterval) p.getEmbeddedObject();
}
 
Example 9
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public ODate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  return (ODate) p.getEmbeddedObject();
}
 
Example 10
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public OTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  return (OTime) p.getEmbeddedObject();
}
 
Example 11
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public OTimestamp deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  return (OTimestamp) p.getEmbeddedObject();
}