@SuppressWarnings("unchecked") protected InstantDeserializer(InstantDeserializer<T> base, Boolean adjustToContextTimezoneOverride) { super((Class<T>) base.handledType(), base._formatter); parsedToValue = base.parsedToValue; fromMilliseconds = base.fromMilliseconds; fromNanoseconds = base.fromNanoseconds; adjust = base.adjust; replace0000AsZ = base.replace0000AsZ; _adjustToContextTZOverride = adjustToContextTimezoneOverride; }
@Override protected JsonDeserializer<YearMonth> withDateFormat(DateTimeFormatter dtf) { return new YearMonthDeserializer(dtf); }
@Override protected JsonDeserializer<T> withDateFormat(DateTimeFormatter dtf) { if (dtf == _formatter) { return this; } return new InstantDeserializer<T>(this, dtf); }
@SuppressWarnings("unchecked") @Override public JsonDeserializer<T> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException { InstantDeserializer<T> deserializer = (InstantDeserializer<T>)super.createContextual(ctxt, property); if (deserializer != this) { JsonFormat.Value val = findFormatOverrides(ctxt, property, handledType()); if (val != null) { return new InstantDeserializer<>(deserializer, val.getFeature(JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)); } } return this; }
protected T _fromLong(DeserializationContext context, long timestamp) { if(context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)){ return fromNanoseconds.apply(new FromDecimalArguments( timestamp, 0, this.getZone(context) )); } return fromMilliseconds.apply(new FromIntegerArguments( timestamp, this.getZone(context))); }
@Override public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException { JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType()); if (format != null) { if (format.hasPattern()) { final String pattern = format.getPattern(); final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale(); DateTimeFormatter df; if (locale == null) { df = DateTimeFormatter.ofPattern(pattern); } else { df = DateTimeFormatter.ofPattern(pattern, locale); } //Issue #69: For instant serializers/deserializers we need to configure the formatter with //a time zone picked up from JsonFormat annotation, otherwise serialization might not work if (format.hasTimeZone()) { df = df.withZone(DateTimeUtils.toZoneId(format.getTimeZone())); } return withDateFormat(df); } // any use for TimeZone? } return this; } }
protected T _fromDecimal(DeserializationContext context, BigDecimal value) { long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return fromNanoseconds.apply(new FromDecimalArguments( seconds, nanoseconds, getZone(context))); }
protected <BOGUS> BOGUS _reportWrongToken(JsonParser parser, DeserializationContext context, JsonToken... expTypes) throws IOException { // 20-Apr-2016, tatu: No multiple-expected-types handler yet, construct message // here String msg = String.format("Unexpected token (%s), expected one of %s for %s value", parser.getCurrentToken(), Arrays.asList(expTypes).toString(), handledType().getName()); throw JsonMappingException.from(parser, msg); }
@Override protected JsonDeserializer<MonthDay> withDateFormat(DateTimeFormatter dtf) { return new MonthDayDeserializer(dtf); }
@Override protected JsonDeserializer<LocalTime> withDateFormat(DateTimeFormatter formatter) { return new LocalTimeDeserializer(formatter); }
@SuppressWarnings("unchecked") protected static <T> JsonDeserializer<T> createDeserializer(Class<T> type, int typeId) { return (JsonDeserializer<T>) new ThreeTenStringParsableDeserializer(type, typeId); }
@Override protected JsonDeserializer<LocalDateTime> withDateFormat(DateTimeFormatter formatter) { return new LocalDateTimeDeserializer(formatter); }
@Override protected JsonDeserializer<LocalDate> withDateFormat(DateTimeFormatter dtf) { return new LocalDateDeserializer(dtf); }
@Override protected JsonDeserializer<OffsetTime> withDateFormat(DateTimeFormatter dtf) { return new OffsetTimeDeserializer(dtf); }
@Override public Object deserializeWithType(JsonParser parser, DeserializationContext context, TypeDeserializer deserializer) throws IOException { /* This is a nasty kludge right here, working around issues like * [datatype-jsr310#24]. But should work better than not having the work-around. */ JsonToken t = parser.getCurrentToken(); if ((t != null) && t.isScalarValue()) { return deserialize(parser, context); } return deserializer.deserializeTypedFromAny(parser, context); } }
@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) { _rethrowDateTimeException(parser, context, e, string); } } if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { return (MonthDay) parser.getEmbeddedObject(); } throw context.mappingException("Unexpected token (%s), expected VALUE_STRING or VALUE_NUMBER_INT", parser.getCurrentToken()); } }
@Override public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException { JsonToken t = parser.getCurrentToken(); 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) { _rethrowDateTimeException(parser, context, e, string); } } if (t == JsonToken.VALUE_NUMBER_INT) { return Year.of(parser.getIntValue()); } if (t == JsonToken.VALUE_EMBEDDED_OBJECT) { return (Year) parser.getEmbeddedObject(); } throw context.mappingException("Unexpected token (%s), expected VALUE_STRING or VALUE_NUMBER_INT", parser.getCurrentToken()); } }
protected <BOGUS> BOGUS _reportWrongToken(JsonParser parser, DeserializationContext context, JsonToken exp, String unit) throws IOException { throw context.wrongTokenException(parser, exp, String.format("Expected %s for '%s' of %s value", exp.name(), unit, handledType().getName())); }
@SuppressWarnings("unchecked") protected InstantDeserializer(InstantDeserializer<T> base, DateTimeFormatter f) { super((Class<T>) base.handledType(), f); parsedToValue = base.parsedToValue; fromMilliseconds = base.fromMilliseconds; fromNanoseconds = base.fromNanoseconds; adjust = base.adjust; replace0000AsZ = (_formatter == DateTimeFormatter.ISO_INSTANT); _adjustToContextTZOverride = base._adjustToContextTZOverride; }
protected <BOGUS> BOGUS _rethrowDateTimeException(JsonParser p, DeserializationContext context, DateTimeException e0, String value) throws JsonMappingException { JsonMappingException e; if (e0 instanceof DateTimeParseException) { e = context.weirdStringException(value, handledType(), e0.getMessage()); e.initCause(e0); } else { e = JsonMappingException.from(p, String.format("Failed to deserialize %s: (%s) %s", handledType().getName(), e0.getClass().getName(), e0.getMessage()), e0); } throw e; }