Skip to content

Commit

Permalink
Issue FasterXML#21: DateTimeSerializer use the configured time zone
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobler Patrick BIT committed Sep 2, 2013
1 parent 91f25c0 commit 31f47e5
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public final class DateTimeSerializer
extends JodaSerializerBase<DateTime>
{
public DateTimeSerializer() { super(DateTime.class); }

private DateTimeFormatter format = null;

@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
if (provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
jgen.writeNumber(value.getMillis());
} else {
jgen.writeString(value.toString());
jgen.writeString(getDateTimeFormatter(provider).print(value));
}
}

Expand All @@ -32,4 +37,11 @@ public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type ty
return createSchemaNode(provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
? "number" : "string", true);
}

private DateTimeFormatter getDateTimeFormatter(SerializerProvider provider) {
if (format == null) {
format = ISODateTimeFormat.dateTime().withZone(DateTimeZone.forTimeZone(provider.getTimeZone()));
}
return format;
}
}

0 comments on commit 31f47e5

Please sign in to comment.