Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Yang committed Jul 15, 2015
1 parent 713d6c5 commit 6da8f42
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fasterxml.jackson.datatype.joda.cfg;

import org.joda.time.DateTimeZone;
import org.joda.time.format.*;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.ISOPeriodFormat;

/**
* Simple container class that holds both default formatter information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ public DateTimeFormatter createParser(DeserializationContext ctxt)
}
return formatter;
}

/**
* Differentiate if TimeZone is specified by caller
* @return true if TimeZone is specified by caller; false otherwise.
*/
public boolean isTimezoneExplicit() {
return _explicitTimezone;
}

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import org.joda.time.DateMidnight;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;

import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -36,6 +37,7 @@ public JodaDateDeserializerBase<?> withFormat(JacksonJodaDateFormat format) {
public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{

// We'll accept either long (timestamp) or array:
if (p.isExpectedStartArrayToken()) {
p.nextToken(); // VALUE_NUMBER_INT
Expand All @@ -48,7 +50,9 @@ public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY,
"after DateMidnight ints");
}
return new DateMidnight(year, month, day);
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());

return new DateMidnight(year, month, day, tz);
}
switch (p.getCurrentToken()) {
case VALUE_NUMBER_INT:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.fasterxml.jackson.datatype.joda.deser;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
import java.io.IOException;

import org.joda.time.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.ReadableDateTime;
import org.joda.time.ReadableInstant;

import java.io.IOException;
import java.util.TimeZone;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;

/**
* Basic deserializer for {@link ReadableDateTime} and its subtypes.
Expand Down Expand Up @@ -42,10 +46,10 @@ public ReadableDateTime deserialize(JsonParser p, DeserializationContext ctxt)
{
JsonToken t = p.getCurrentToken();

DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());

if (t == JsonToken.VALUE_NUMBER_INT) {
TimeZone tz = ctxt.getTimeZone();
DateTimeZone dtz = (tz == null) ? DateTimeZone.UTC : DateTimeZone.forTimeZone(tz);
return new DateTime(p.getLongValue(), dtz);
return new DateTime(p.getLongValue(), tz);
}
if (t == JsonToken.VALUE_STRING) {
String str = p.getText().trim();
Expand All @@ -60,7 +64,6 @@ public ReadableDateTime deserialize(JsonParser p, DeserializationContext ctxt)
String tzId = (ix2 < ix)
? str.substring(ix+1)
: str.substring(ix+1, ix2);
DateTimeZone tz;
try {
tz = DateTimeZone.forID(tzId);
} catch (IllegalArgumentException e) {
Expand Down Expand Up @@ -88,15 +91,4 @@ public ReadableDateTime deserialize(JsonParser p, DeserializationContext ctxt)
}
throw ctxt.mappingException(handledType());
}

private static boolean _allDigits(String str)
{
for (int i = 0, len = str.length(); i < len; ++i) {
int c = str.charAt(i);
if (c > '9' | c < '0') {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import java.io.IOException;

import org.joda.time.DateTimeZone;
import org.joda.time.Interval;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;

import org.joda.time.*;

public class IntervalDeserializer extends JodaDateDeserializerBase<Interval>
{
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -64,7 +64,8 @@ public Interval deserialize(JsonParser jsonParser, DeserializationContext ctxt)
throw JsonMappingException.from(jsonParser,
"Failed to parse number from '"+str+"' (full source String '"+v+"') to construct "+handledType().getName());
}
DateTimeZone tz = _format.getTimeZone();

DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
if (tz != null) {
if (!tz.equals(result.getStart().getZone())) {
result = new Interval(result.getStartMillis(), result.getEndMillis(), tz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import org.joda.time.LocalTime;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.io.IOException;
import java.util.TimeZone;

import org.joda.time.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.joda.time.DateTimeZone;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.SerializerProvider;

public class DateTimeZoneSerializer extends JodaSerializerBase<DateTimeZone>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import org.joda.time.Duration;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;

import org.joda.time.*;
import org.joda.time.Instant;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializationFeature;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fasterxml.jackson.datatype.joda.ser;

import com.fasterxml.jackson.core.JsonGenerator;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.joda.time.LocalTime;

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.fasterxml.jackson.datatype.joda;

import java.io.IOException;
import java.util.TimeZone;

import org.joda.time.DateMidnight;
import org.joda.time.DateTimeZone;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand All @@ -29,12 +31,51 @@ public AlternateFormat(DateMidnight v) {
}
}

static class FormattedDateMidnight {
@JsonFormat(timezone="EST")
public DateMidnight dateMidnight;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

public void testDateMidnightDeserWithTimeZone() throws IOException
{
MAPPER.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
// couple of acceptable formats, so:
DateMidnight date = MAPPER.readValue("[2001,5,25]", DateMidnight.class);
assertEquals(2001, date.getYear());
assertEquals(5, date.getMonthOfYear());
assertEquals(25, date.getDayOfMonth());

DateMidnight date2 = MAPPER.readValue(quote("2005-07-13"), DateMidnight.class);
assertEquals(2005, date2.getYear());
assertEquals(7, date2.getMonthOfYear());
assertEquals(13, date2.getDayOfMonth());

// since 1.6.1, for [JACKSON-360]
assertNull(MAPPER.readValue(quote(""), DateMidnight.class));


MAPPER.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
// couple of acceptable formats, so:
date = MAPPER.readValue("[2001,5,25]", DateMidnight.class);
assertEquals(2001, date.getYear());
assertEquals(5, date.getMonthOfYear());
assertEquals(25, date.getDayOfMonth());

date2 = MAPPER.readValue(quote("2005-07-13"), DateMidnight.class);
assertEquals(2005, date2.getYear());
assertEquals(7, date2.getMonthOfYear());
assertEquals(13, date2.getDayOfMonth());

// since 1.6.1, for [JACKSON-360]
assertNull(MAPPER.readValue(quote(""), DateMidnight.class));
}

public void testDateMidnightDeser() throws IOException
{
// couple of acceptable formats, so:
Expand Down Expand Up @@ -85,4 +126,27 @@ public void testCustomFormat() throws Exception
assertNotNull(output.value);
assertEquals(inputDate, output.value);
}

public void testWithTimeZoneOverride() throws Exception
{
ObjectMapper mapper = jodaMapper();

DateMidnight date = mapper.readValue("[2001,5,25]", DateMidnight.class);
FormattedDateMidnight input = new FormattedDateMidnight();
input.dateMidnight = date;
String json = mapper.writeValueAsString(input);

FormattedDateMidnight result = mapper.readValue(json, FormattedDateMidnight.class);
assertNotNull(result);

// Ensure timezone sticks:
DateMidnight resultMidnight = result.dateMidnight;
assertEquals(2001, resultMidnight.getYear());
assertEquals(5, resultMidnight.getMonthOfYear());
assertEquals(25, resultMidnight.getDayOfMonth());

DateTimeZone resultTz = resultMidnight.getZone();
// Is this stable enough for testing?
assertEquals("America/New_York", resultTz.getID());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import java.io.IOException;

import org.joda.time.*;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class DateTimeTest extends JodaTestBase
{
Expand Down Expand Up @@ -48,6 +53,11 @@ public Beanie(@JsonProperty("jodaDateTime") DateTime jodaDateTime,
this.javaUtilDate = javaUtilDate;
}
}

static class FormattedDateTime {
@JsonFormat(timezone="EST")
public DateTime dateTime;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "@class")
private static interface TypeInfoMixIn {
Expand Down Expand Up @@ -159,4 +169,28 @@ public void testCustomFormat() throws Exception
// Timezone may (and most likely will) differ so...
assertEquals(inputDate.getMillis(), output.date.getMillis());
}

public void testWithTimeZoneOverride() throws Exception
{
ObjectMapper mapper = jodaMapper();

DateTime date = mapper.readValue(quote("2014-01-20T08:59:01.000-0500"), DateTime.class);

FormattedDateTime input = new FormattedDateTime();
input.dateTime = date;
String json = mapper.writeValueAsString(input);

FormattedDateTime result = mapper.readValue(json, FormattedDateTime.class);
assertNotNull(result);

// Ensure timezone sticks:
DateTime resultMidnight = result.dateTime;
assertEquals(2014, resultMidnight.getYear());
assertEquals(1, resultMidnight.getMonthOfYear());
assertEquals(20, resultMidnight.getDayOfMonth());

DateTimeZone resultTz = resultMidnight.getZone();
// Is this stable enough for testing?
assertEquals("America/New_York", resultTz.getID());
}
}
Loading

0 comments on commit 6da8f42

Please sign in to comment.