Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
add Support for JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE
Browse files Browse the repository at this point in the history
  • Loading branch information
joxerTMD authored and xxx committed Jun 28, 2016
1 parent 66644bd commit 7640048
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.fasterxml.jackson.datatype.jsr310.deser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;

import java.io.IOException;
Expand Down Expand Up @@ -82,6 +85,8 @@ public class InstantDeserializer<T extends Temporal>

protected final BiFunction<T, ZoneId, T> adjust;

protected final Boolean adjustToContextTimezoneOverride;

/**
* In case of vanilla `Instant` we seem to need to translate "+0000"
* timezone designator into plain "Z" for some reason; see
Expand All @@ -104,6 +109,7 @@ protected InstantDeserializer(Class<T> supportedType,
this.fromMilliseconds = fromMilliseconds;
this.fromNanoseconds = fromNanoseconds;
this.adjust = adjust == null ? ((d, z) -> d) : adjust;
this.adjustToContextTimezoneOverride = null;
this.replace0000AsZ = replace0000AsZ;
}

Expand All @@ -116,6 +122,19 @@ protected InstantDeserializer(InstantDeserializer<T> base, DateTimeFormatter f)
fromNanoseconds = base.fromNanoseconds;
adjust = base.adjust;
replace0000AsZ = (_formatter == DateTimeFormatter.ISO_INSTANT);
adjustToContextTimezoneOverride = base.adjustToContextTimezoneOverride;
}

@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;
this.adjustToContextTimezoneOverride = adjustToContextTimezoneOverride;
}

@Override
Expand Down Expand Up @@ -173,7 +192,7 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
try {
TemporalAccessor acc = _formatter.parse(string);
value = parsedToValue.apply(acc);
if (context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) {
if (isAdjustToContextTimezone(context)) {
return adjust.apply(value, this.getZone(context));
}
} catch (DateTimeException e) {
Expand All @@ -190,6 +209,27 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I
throw context.mappingException("Expected type float, integer, or string.");
}

@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;
}

private boolean isAdjustToContextTimezone(DeserializationContext context) {
return adjustToContextTimezoneOverride != null ? adjustToContextTimezoneOverride :
context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}

/**
* Helper method to find Strings of form "all digits" and "digits-comma-digits"
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.datatype.jsr310;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectReader;
Expand All @@ -10,6 +11,7 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -44,6 +46,38 @@ public void testDeserializationAsString03() throws Exception
expect(OffsetDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.ofHours(5)), parsed) ;
}

public static class WithContextTimezoneDateFieldBean {
@JsonFormat(shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", with = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
public OffsetDateTime date;
}

@Test
public void testDeserializationWithContextTimezoneFeatureOverride() throws Exception
{
String inputStr = "{\"date\":\"2016-05-13T17:24:40.545+03\"}";
WithContextTimezoneDateFieldBean result = newMapper().setTimeZone(TimeZone.getTimeZone("UTC")).
disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).readValue(inputStr, WithContextTimezoneDateFieldBean.class);
notNull(result);
expect(OffsetDateTime.of(2016, 5, 13, 14, 24, 40, 545000000, ZoneOffset.UTC), result.date);
}

public static class WithoutContextTimezoneDateFieldBean {
@JsonFormat(shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", without = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
public OffsetDateTime date;
}

@Test
public void testDeserializationWithoutContextTimezoneFeatureOverride() throws Exception
{
String inputStr = "{\"date\":\"2016-05-13T17:24:40.545+03\"}";
WithoutContextTimezoneDateFieldBean result = newMapper().setTimeZone(TimeZone.getTimeZone("UTC")).
enable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).readValue(inputStr, WithoutContextTimezoneDateFieldBean.class);
notNull(result);
expect(OffsetDateTime.of(2016, 5, 13, 17, 24, 40, 545000000, ZoneOffset.ofHours(3)), result.date);
}

@Test
public void testBadDeserializationAsString01() throws Throwable
{
Expand Down

0 comments on commit 7640048

Please sign in to comment.