Skip to content

Commit c89a98a

Browse files
committed
Fix #824
1 parent 332532a commit c89a98a

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Project: jackson-databind
6565
#812: Java 8 breaks Class-value annotation properties, wrt generics: need to work around
6666
#813: Add support for new property of `@JsonProperty.access` to support
6767
read-only/write-only use cases
68+
#824: Contextual `TimeZone` changes don't take effect wrt `java.util.Date`,
69+
`java.util.Calendar` serialization
6870
- Remove old cglib compatibility tests; cause problems in Eclipse
6971

7072
2.5.4 (not yet released)

src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -1004,14 +1004,14 @@ public final void defaultSerializeDateValue(long timestamp, JsonGenerator jgen)
10041004
* Note: date here means "full" date, that is, date AND time, as per
10051005
* Java convention (and not date-only values like in SQL)
10061006
*/
1007-
public final void defaultSerializeDateValue(Date date, JsonGenerator jgen)
1007+
public final void defaultSerializeDateValue(Date date, JsonGenerator gen)
10081008
throws IOException
10091009
{
10101010
// [JACKSON-87]: Support both numeric timestamps and textual
10111011
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
1012-
jgen.writeNumber(date.getTime());
1012+
gen.writeNumber(date.getTime());
10131013
} else {
1014-
jgen.writeString(_dateFormat().format(date));
1014+
gen.writeString(_dateFormat().format(date));
10151015
}
10161016
}
10171017

@@ -1020,13 +1020,13 @@ public final void defaultSerializeDateValue(Date date, JsonGenerator jgen)
10201020
* based on {@link SerializationFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
10211021
* value (and if using textual representation, configured date format)
10221022
*/
1023-
public void defaultSerializeDateKey(long timestamp, JsonGenerator jgen)
1023+
public void defaultSerializeDateKey(long timestamp, JsonGenerator gen)
10241024
throws IOException
10251025
{
10261026
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
1027-
jgen.writeFieldName(String.valueOf(timestamp));
1027+
gen.writeFieldName(String.valueOf(timestamp));
10281028
} else {
1029-
jgen.writeFieldName(_dateFormat().format(new Date(timestamp)));
1029+
gen.writeFieldName(_dateFormat().format(new Date(timestamp)));
10301030
}
10311031
}
10321032

@@ -1035,12 +1035,12 @@ public void defaultSerializeDateKey(long timestamp, JsonGenerator jgen)
10351035
* based on {@link SerializationFeature#WRITE_DATE_KEYS_AS_TIMESTAMPS}
10361036
* value (and if using textual representation, configured date format)
10371037
*/
1038-
public void defaultSerializeDateKey(Date date, JsonGenerator jgen) throws IOException
1038+
public void defaultSerializeDateKey(Date date, JsonGenerator gen) throws IOException
10391039
{
10401040
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
1041-
jgen.writeFieldName(String.valueOf(date.getTime()));
1041+
gen.writeFieldName(String.valueOf(date.getTime()));
10421042
} else {
1043-
jgen.writeFieldName(_dateFormat().format(date));
1043+
gen.writeFieldName(_dateFormat().format(date));
10441044
}
10451045
}
10461046

@@ -1225,13 +1225,17 @@ protected final DateFormat _dateFormat()
12251225
if (_dateFormat != null) {
12261226
return _dateFormat;
12271227
}
1228-
/* 24-Feb-2012, tatu: At this point, all timezone configuration
1229-
* should have occured, with respect to default dateformat
1230-
* and timezone configuration. But we still better clone
1231-
* an instance as formatters may be stateful.
1228+
/* At this point, all timezone configuration should have occured, with respect
1229+
* to default dateformat configuration. But we still better clone
1230+
* an instance as formatters are stateful, not thread-safe.
12321231
*/
12331232
DateFormat df = _config.getDateFormat();
12341233
_dateFormat = df = (DateFormat) df.clone();
1234+
// 11-Jun-2015, tatu: Plus caller may have actually changed default TimeZone to use
1235+
TimeZone tz = getTimeZone();
1236+
if (tz != df.getTimeZone()) {
1237+
df.setTimeZone(tz);
1238+
}
12351239
return df;
12361240
}
12371241
}

src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
* be freely shared and used without synchronization.
2222
*/
2323
public final class BaseSettings
24-
implements java.io.Serializable // since 2.1
24+
implements java.io.Serializable
2525
{
26-
// for 2.1.0:
27-
private static final long serialVersionUID = 4939673998947122190L;
26+
// for 2.6
27+
private static final long serialVersionUID = 1L;
2828

2929
/*
3030
/**********************************************************
@@ -231,9 +231,10 @@ public BaseSettings withDateFormat(DateFormat df) {
231231
if (_dateFormat == df) {
232232
return this;
233233
}
234+
TimeZone tz = (df == null) ? _timeZone : df.getTimeZone();
234235
return new BaseSettings(_classIntrospector, _annotationIntrospector, _visibilityChecker, _propertyNamingStrategy, _typeFactory,
235236
_typeResolverBuilder, df, _handlerInstantiator, _locale,
236-
_timeZone, _defaultBase64);
237+
tz, _defaultBase64);
237238
}
238239

239240
public BaseSettings withHandlerInstantiator(HandlerInstantiator hi) {

src/main/java/com/fasterxml/jackson/databind/ser/std/DateSerializer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ protected long _timestamp(Date value) {
4242
}
4343

4444
@Override
45-
public void serialize(Date value, JsonGenerator gen, SerializerProvider provider)
46-
throws IOException, JsonGenerationException
45+
public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException
4746
{
4847
if (_asTimestamp(provider)) {
4948
gen.writeNumber(_timestamp(value));

src/main/java/com/fasterxml/jackson/databind/util/StdDateFormat.java

+5
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public static DateFormat getRFC1123Format(TimeZone tz) {
238238
/**********************************************************
239239
*/
240240

241+
@Override // since 2.6
242+
public TimeZone getTimeZone() {
243+
return _timezone;
244+
}
245+
241246
@Override
242247
public void setTimeZone(TimeZone tz)
243248
{

src/test/java/com/fasterxml/jackson/databind/ser/DateSerializationTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void testTimeZoneInBean() throws IOException
130130
String json = MAPPER.writeValueAsString(new TimeZoneBean("PST"));
131131
assertEquals("{\"tz\":\"PST\"}", json);
132132
}
133-
133+
134134
// [JACKSON-648]: (re)configuring via ObjectWriter
135135
public void testDateUsingObjectWriter() throws IOException
136136
{
@@ -205,6 +205,12 @@ public void testWithTimeZoneOverride() throws Exception
205205
mapper.setLocale(Locale.FRANCE);
206206
json = mapper.writeValueAsString(new Date(0));
207207
assertEquals(quote("1969-12-31/16:00 PST"), json);
208+
209+
// Also: should be able to dynamically change timezone:
210+
ObjectWriter w = mapper.writer();
211+
w = w.with(TimeZone.getTimeZone("EST"));
212+
json = w.writeValueAsString(new Date(0));
213+
assertEquals(quote("1969-12-31/19:00 EST"), json);
208214
}
209215
}
210216

0 commit comments

Comments
 (0)