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

The following examples show how to use com.fasterxml.jackson.databind.DeserializationContext#reportWrongTokenException() . 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 divolte-collector with Apache License 2.0 5 votes vote down vote up
@Override
public Duration deserialize(final JsonParser p,
                            final DeserializationContext ctx) throws IOException {
    if (VALUE_STRING != p.getCurrentToken()) {
        ctx.reportWrongTokenException(this, VALUE_STRING, "Expected string value for Duration mapping.");
    }
    long result;
    try {
        result = parse(p.getText());
    } catch(final DurationFormatException e) {
        throw InvalidFormatException.from(p, e.getMessage(), e);
    }
    return Duration.ofNanos(result);
}
 
Example 2
Source File: ProtobufDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
private AssertionError reportWrongToken(
        JsonToken expected,
        DeserializationContext context,
        String message
) throws JsonMappingException {
  context.reportWrongTokenException(this, expected, message);
  // the previous method should have thrown
  throw new AssertionError();
}
 
Example 3
Source File: NullValueDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public NullValue deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_NULL:
      return NullValue.NULL_VALUE;
    default:
      context.reportWrongTokenException(NullValue.class, JsonToken.VALUE_NULL, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example 4
Source File: MessageDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
private AssertionError reportWrongToken(
        JsonToken expected,
        DeserializationContext context,
        String message
) throws JsonMappingException {
  context.reportWrongTokenException(this, expected, message);
  // the previous method should have thrown
  throw new AssertionError();
}
 
Example 5
Source File: FieldMaskDeserializer.java    From jackson-datatype-protobuf with Apache License 2.0 5 votes vote down vote up
@Override
public FieldMask deserialize(JsonParser parser, DeserializationContext context) throws IOException {
  switch (parser.getCurrentToken()) {
    case VALUE_STRING:
      return FieldMaskUtil.fromJsonString(parser.getText());
    default:
      context.reportWrongTokenException(FieldMask.class, JsonToken.VALUE_STRING, wrongTokenMessage(context));
      // the previous method should have thrown
      throw new AssertionError();
  }
}
 
Example 6
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 7
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 8
Source File: LocalDateTimeDeserializer.java    From heimdall with Apache License 2.0 4 votes vote down vote up
@Override
public LocalDateTime 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();
               
               parser.nextToken();
               int hour = parser.getIntValue();

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

               parser.nextToken();
               int second = parser.getIntValue();
               
               parser.nextToken();
               int nanosecond = parser.getIntValue();
               
               if (parser.nextToken() != JsonToken.END_ARRAY) {
                    context.reportWrongTokenException(JsonToken.class, JsonToken.END_ARRAY, "Expected array to end.");
               }
               return LocalDateTime.of(year, month, day, hour, minute, second, nanosecond);

          case VALUE_STRING:
               String string = parser.getText().trim();
               if (string.length() == 0) {
                    return null;
               }
               return LocalDateTime.parse(string, ISO_DATE_TIME);
     }
     context.reportWrongTokenException(JsonToken.class, JsonToken.START_ARRAY, "Expected array or string.");
     return null;
}