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

The following examples show how to use com.fasterxml.jackson.databind.DeserializationContext#weirdStringException() . 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: 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 2
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 6 votes vote down vote up
private Long rangeCheckedLong(JsonParser parser, DeserializationContext context)
throws IOException {
    String text = parser.getText().trim();
    if (text.length() == 0) {
        return null;
    }
    try {
        return Long.valueOf(NumberInput.parseLong(text));
    }
    catch (Exception e) {
        throw context.weirdStringException(//NOSONAR
            _valueClass,
            "Over/underflow: numeric value (" + text +") out of range of Long ("
                + Long.MIN_VALUE + " to " + Long.MAX_VALUE + ")"
        );
    }
}
 
Example 3
Source File: UUIDDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
int _badChar(String uuidStr, int index, DeserializationContext ctxt, char c) throws JsonMappingException {
    // 15-May-2016, tatu: Ideally should not throw, but call `handleWeirdStringValue`...
    //   however, control flow is gnarly here, so for now just throw
    throw ctxt.weirdStringException(uuidStr, handledType(),
            String.format(
            "Non-hex character '%c' (value 0x%s), not valid for UUID String",
            c, Integer.toHexString(c)));
}
 
Example 4
Source File: DeltaDeserializer.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public Delta 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 Delta string.");
    }

    String string = jp.getText();
    try {
        return DeltaParser.parse(string);
    } catch (Exception e) {
        throw ctxt.weirdStringException(string, Delta.class, "Not a valid Delta string representation.");
    }
}
 
Example 5
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 6
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public Integer deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonToken t = parser.getCurrentToken();
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
        return rangeCheckedInteger(parser, context);
    }
    if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
        String text = parser.getText().trim();
        try {
            int len = text.length();
            if (len > 9) {
                return rangeCheckedInteger(parser, context);
            }
            if (len == 0) {
                return null;
            }
            return Integer.valueOf(NumberInput.parseInt(text));
        } catch (IllegalArgumentException iae) {
            throw context.weirdStringException(_valueClass, "not a valid Integer value");//NOSONAR
        }
    }
    if (t == JsonToken.VALUE_NULL) {
        return null;
    }
    // Otherwise, no can do:
    throw context.mappingException(_valueClass);
}
 
Example 7
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 5 votes vote down vote up
private Integer rangeCheckedInteger(JsonParser parser, DeserializationContext context)
throws IOException {
    long l = parser.getLongValue();
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw context.weirdStringException(
            _valueClass,
            "Over/underflow: numeric value (" + l +") out of range of Integer ("
                + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE + ")"
        );
    }
    return Integer.valueOf((int) l);
}
 
Example 8
Source File: JSONBindingFactory.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
protected Byte _parseByte(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken t = jp.getCurrentToken();
    Integer value = null;
    if (t == JsonToken.VALUE_NUMBER_INT || t == JsonToken.VALUE_NUMBER_FLOAT) { // coercing should work too
        value = jp.getIntValue();
    }
    else if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse
        String text = jp.getText().trim();
        try {
            int len = text.length();
            if (len == 0) {
                return getEmptyValue();
            }
            value = NumberInput.parseInt(text);
        } catch (IllegalArgumentException iae) {
            throw ctxt.weirdStringException(_valueClass, "not a valid Byte value");//NOSONAR
        }
    }
    if (value != null) {
        // So far so good: but does it fit?
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            throw ctxt.weirdStringException(_valueClass, "overflow, value can not be represented as 8-bit value");
        }
        return (byte) (int) value;
    }
    if (t == JsonToken.VALUE_NULL) {
        return getNullValue();
    }
    throw ctxt.mappingException(_valueClass, t);
}
 
Example 9
Source File: DurationDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_STRING:
      try {
        return Durations.parse(parser.getText());
      } catch (ParseException e) {
        throw context.weirdStringException(parser.getText(), Duration.class, e.getMessage());
      }
    default:
      context.reportWrongTokenException(Duration.class, JsonToken.VALUE_STRING, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example 10
Source File: TimestampDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public Timestamp deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_STRING:
      try {
        return Timestamps.parse(parser.getText());
      } catch (ParseException e) {
        throw context.weirdStringException(parser.getText(), Timestamp.class, e.getMessage());
      }
    default:
      context.reportWrongTokenException(Timestamp.class, JsonToken.VALUE_STRING, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example 11
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;
}