Java Code Examples for com.fasterxml.jackson.databind.DeserializationContext#wrongTokenException()

The following examples show how to use com.fasterxml.jackson.databind.DeserializationContext#wrongTokenException() . 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: LocalDateDeserializer.java    From bootique with Apache License 2.0 6 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    if (parser.hasToken(JsonToken.VALUE_STRING)) {
        String string = parser.getText().trim();
        if (string.length() == 0) {
            return null;
        }
        // as per [datatype-jsr310#37], only check for optional (and, incorrect...) time marker 'T'
        // if we are using default formatter
        DateTimeFormatter format = _formatter;
        if (format == DEFAULT_FORMATTER) {
            if (string.contains("T")) {
                return LocalDate.parse(string, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            }
        }
        return LocalDate.parse(string, format);
    }
    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
 
Example 2
Source File: FilterDeserializer.java    From ffwd with Apache License 2.0 6 votes vote down vote up
@Override
public Filter deserialize(JsonParser p, DeserializationContext ctx)
    throws IOException, JsonProcessingException {
    if (p.getCurrentToken() != JsonToken.START_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.START_ARRAY, null);
    }

    final String id = p.nextTextValue();

    final PartialDeserializer d = suppliers.get(id);

    if (d == null) {
        throw ctx.weirdStringException(id, Filter.class,
            String.format("Expected one of %s", suppliers.keySet()));
    }

    final Filter instance = d.deserialize(p, ctx);

    if (p.getCurrentToken() != JsonToken.END_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.END_ARRAY, null);
    }

    return instance;
}
 
Example 3
Source File: PointSerialization.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public Point deserialize(JsonParser p, DeserializationContext c) throws IOException {
    if (p.getCurrentToken() != JsonToken.START_ARRAY) {
        throw c.mappingException(
            String.format("Expected start of array, not %s", p.getCurrentToken()));
    }

    if (!p.nextToken().isNumeric()) {
        throw c.wrongTokenException(p, JsonToken.VALUE_NUMBER_INT,
            "Expected timestamp (number)");
    }

    final long timestamp = p.getLongValue();

    if (!p.nextToken().isNumeric()) {
        throw c.wrongTokenException(p, JsonToken.VALUE_NUMBER_FLOAT,
            "Expected value (number)");
    }

    final double value = p.getDoubleValue();

    if (p.nextToken() != JsonToken.END_ARRAY) {
        throw c.mappingException(
            String.format("Expected end of array, not %s", p.getCurrentToken()));
    }

    return new Point(timestamp, value);
}
 
Example 4
Source File: OptionalLimit.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public OptionalLimit deserialize(
    final JsonParser p, final DeserializationContext ctx
) throws IOException {
    switch (p.getCurrentToken()) {
        case VALUE_NUMBER_FLOAT:
        case VALUE_NUMBER_INT:
            return new ValueOptionalLimit(p.getIntValue());
        default:
            throw ctx.wrongTokenException(p, JsonToken.VALUE_NUMBER_INT,
                "Expected limit or null");
    }
}
 
Example 5
Source File: TrueFilter.java    From ffwd with Apache License 2.0 5 votes vote down vote up
@Override
public Filter deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
    if (p.nextToken() != JsonToken.END_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.END_ARRAY, null);
    }

    return new TrueFilter();
}
 
Example 6
Source File: LocalDateTimeDeserializer.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    if (parser.hasTokenId(JsonTokenId.ID_STRING)) {
        String string = parser.getText().trim();
        if (string.length() == 0) {
            return null;
        }
        return LocalDateTime.parse(string, _formatter);
    }

    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
 
Example 7
Source File: TypeFilter.java    From ffwd with Apache License 2.0 5 votes vote down vote up
@Override
public Filter deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
    final String type = p.nextTextValue();

    if (p.nextToken() != JsonToken.END_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.END_ARRAY, null);
    }

    return new TypeFilter(type);
}
 
Example 8
Source File: MatchKey.java    From ffwd with Apache License 2.0 5 votes vote down vote up
@Override
public Filter deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
    final String key = p.nextTextValue();

    if (p.nextToken() != JsonToken.END_ARRAY) {
        throw ctx.wrongTokenException(p, JsonToken.END_ARRAY, null);
    }

    return new MatchKey(key);
}
 
Example 9
Source File: ConditionDeserializer.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public Condition deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken tok = jp.getCurrentToken();
    if (tok != JsonToken.VALUE_STRING) {
        throw ctxt.wrongTokenException(jp, tok, "Could not map to a Condition string.");
    }

    String string = jp.getText();
    try {
        return DeltaParser.parseCondition(string);
    } catch (Exception e) {
        throw ctxt.weirdStringException(string, Condition.class, "Not a valid Condition string representation.");
    }
}
 
Example 10
Source File: JSR310LocalDateDeserializer.java    From OpenIoE with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch(parser.getCurrentToken()) {
        case START_ARRAY:
            if(parser.nextToken() == JsonToken.END_ARRAY) {
                return null;
            }
            int year = parser.getIntValue();

            parser.nextToken();
            int month = parser.getIntValue();

            parser.nextToken();
            int day = parser.getIntValue();

            if(parser.nextToken() != JsonToken.END_ARRAY) {
                throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
            }
            return LocalDate.of(year, month, day);

        case VALUE_STRING:
            String string = parser.getText().trim();
            if(string.length() == 0) {
                return null;
            }
            return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
        default:
            throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
    }
}
 
Example 11
Source File: JSR310LocalDateDeserializer.java    From gpmr with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch (parser.getCurrentToken()) {
        case START_ARRAY:
            if (parser.nextToken() == JsonToken.END_ARRAY) {
                return null;
            }
            int year = parser.getIntValue();

            parser.nextToken();
            int month = parser.getIntValue();

            parser.nextToken();
            int day = parser.getIntValue();

            if (parser.nextToken() != JsonToken.END_ARRAY) {
                throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
            }
            return LocalDate.of(year, month, day);

        case VALUE_STRING:
            String string = parser.getText().trim();
            if (string.length() == 0) {
                return null;
            }
            return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
        default:
            throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
    }
}
 
Example 12
Source File: CharSeqDeserializer.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
@Override
public CharSeq deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    Object obj = deserializer.deserialize(p, ctxt);
    if (obj instanceof String) {
        return CharSeq.of((String) obj);
    } else {
        throw ctxt.wrongTokenException(p, JsonToken.VALUE_STRING, "CharSeq can only be deserialized from String");
    }
}
 
Example 13
Source File: _JSR310LocalDateDeserializer.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch(parser.getCurrentToken()) {
        case START_ARRAY:
            if(parser.nextToken() == JsonToken.END_ARRAY) {
                return null;
            }
            int year = parser.getIntValue();

            parser.nextToken();
            int month = parser.getIntValue();

            parser.nextToken();
            int day = parser.getIntValue();

            if(parser.nextToken() != JsonToken.END_ARRAY) {
                throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
            }
            return LocalDate.of(year, month, day);

        case VALUE_STRING:
            String string = parser.getText().trim();
            if(string.length() == 0) {
                return null;
            }
            return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
    }
    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
 
Example 14
Source File: JSR310LocalDateDeserializer.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch(parser.getCurrentToken()) {
        case START_ARRAY:
            if(parser.nextToken() == JsonToken.END_ARRAY) {
                return null;
            }
            int year = parser.getIntValue();

            parser.nextToken();
            int month = parser.getIntValue();

            parser.nextToken();
            int day = parser.getIntValue();

            if(parser.nextToken() != JsonToken.END_ARRAY) {
                throw context.wrongTokenException(parser, JsonToken.END_ARRAY, "Expected array to end.");
            }
            return LocalDate.of(year, month, day);

        case VALUE_STRING:
            String string = parser.getText().trim();
            if(string.length() == 0) {
                return null;
            }
            return LocalDate.parse(string, ISO_DATE_OPTIONAL_TIME);
    }
    throw context.wrongTokenException(parser, JsonToken.START_ARRAY, "Expected array or string.");
}
 
Example 15
Source File: AggregationOrListDeserializer.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public AggregationOrList deserialize(final JsonParser p, final DeserializationContext c)
    throws IOException {
    switch (p.getCurrentToken()) {
        case START_ARRAY:
            final List<Aggregation> chain = p.readValueAs(LIST_OF_AGGREGATIONS);
            return new AggregationOrList(Aggregations.chain(chain));
        case START_OBJECT:
            return new AggregationOrList(Optional.of(p.readValueAs(Aggregation.class)));
        default:
            throw c.wrongTokenException(p, JsonToken.START_OBJECT, null);
    }
}
 
Example 16
Source File: DI2Error.java    From datasync with MIT License 5 votes vote down vote up
@Override
public ErrorType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    if(jsonParser.getCurrentToken() != JsonToken.VALUE_STRING) throw deserializationContext.wrongTokenException(jsonParser, JsonToken.VALUE_STRING, "Expected string");
    String code = jsonParser.getText();
    ErrorType result = errorTypes.get(code);
    if(result == null) throw deserializationContext.weirdStringException(code, ErrorType.class, "Unknown error code");
    return result;
}
 
Example 17
Source File: PropertiesDeserializer.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
@Override
public Properties deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
    if (START_OBJECT == p.getCurrentToken()) {
        final Properties properties = new Properties();
        final Deque<String> stack = new ArrayDeque<>();
        final ArrayList<String> array = new ArrayList<>();
        for (JsonToken nextToken = p.nextToken(); nextToken != END_OBJECT || !stack.isEmpty(); nextToken = p.nextToken()) {
            switch(nextToken) {
                case FIELD_NAME:
                    stack.addLast(p.getCurrentName());
                    break;
                case VALUE_STRING:
                case VALUE_NUMBER_INT:
                case VALUE_NUMBER_FLOAT:
                case VALUE_TRUE:
                case VALUE_FALSE:
                    if (p.getParsingContext().inArray()) {
                        array.add(p.getText());
                    } else {
                        properties.put(DOT_JOINER.join(stack), p.getText());
                        stack.removeLast();
                    }
                    break;
                case START_OBJECT:
                    if (p.getParsingContext().inArray()) {
                        throw UnsupportedTypeException.from(p, START_OBJECT, "Nested objects within arrays not allowed in Properties object.");
                    }
                    break;
                case END_OBJECT:
                    stack.removeLast();
                    break;
                case START_ARRAY:
                    array.clear();
                    break;
                case END_ARRAY:
                    properties.put(DOT_JOINER.join(stack), COMMA_JOINER.join(array));
                    stack.removeLast();
                    break;
                case VALUE_NULL:
                    throw UnsupportedTypeException.from(p, VALUE_NULL, "Null values not allowed in Properties object.");
                case VALUE_EMBEDDED_OBJECT:
                    throw UnsupportedTypeException.from(p, VALUE_EMBEDDED_OBJECT, "Embedded object not allowed as part of Properties object.");
                case NOT_AVAILABLE:
                    break;
            }
        }
        return properties;
    } else {
        throw ctx.wrongTokenException(p, handledType(), START_OBJECT, "Expected nested object for Properties mapping.");
    }
}
 
Example 18
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 19
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 20
Source File: JSR310DeserializerBase.java    From bootique with Apache License 2.0 4 votes vote down vote up
protected void reportWrongToken(JsonParser parser, DeserializationContext context,
                                JsonToken exp, String unit) throws IOException {
    throw context.wrongTokenException(parser, JsonToken.VALUE_NUMBER_INT,
            "Expected " + exp.name() + " for '" + unit + "' of " + handledType().getName() + " value");
}