Skip to content

Fix #92 DateTime serialization result is not same as java8-moule's ZonedDateTime #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ public JacksonJodaDateFormat withFormat(String format) {
if (_locale != null) {
formatter = formatter.withLocale(_locale);
}

// [datatype-joda#98] Since 2.19.1, fix `@JsonFormat.timezone` not taking effect
// [If a timezone was explicitly set earlier, retain it on the new formatter
if (_explicitTimezone && _jdkTimezone != null) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(_jdkTimezone));
}
return new JacksonJodaDateFormat(this, formatter);
}

Expand Down Expand Up @@ -236,7 +242,22 @@ public DateTimeFormatter rawFormatter() {
return _formatter;
}

/**
* @deprecated since 2.19.1 Use {@link #createFormatter(SerializerProvider, DateTimeZone)} instead
*/
public DateTimeFormatter createFormatter(SerializerProvider ctxt)
{
return createFormatter(ctxt, null);
}

/**
* Creates a formatter with the specified timezone from the value if any.
*
* [dataformat-joda#92] DateTime serialization result is not same as Java 8 ZonedDateTime
*
* @since 2.19.1
*/
public DateTimeFormatter createFormatter(SerializerProvider ctxt, DateTimeZone valueTimeZone)
{
DateTimeFormatter formatter = createFormatterWithLocale(ctxt);
if (!_explicitTimezone) {
Expand All @@ -245,6 +266,9 @@ public DateTimeFormatter createFormatter(SerializerProvider ctxt)
formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
}
}
if (valueTimeZone != null && !valueTimeZone.equals(_jdkTimezone)) {
formatter = formatter.withZone(valueTimeZone);
}
return formatter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void serialize(DateTime value, JsonGenerator gen, SerializerProvider prov
if (numeric) {
gen.writeNumber(value.getMillis());
} else {
gen.writeString(_format.createFormatter(provider).print(value));
gen.writeString(_format.createFormatter(provider, value.getZone()).print(value));
}
} else {
// and then as per [datatype-joda#44], optional TimeZone inclusion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.fasterxml.jackson.datatype.joda.ser;

import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaTestBase;

import static org.junit.jupiter.api.Assertions.assertEquals;

// [dataformat-joda#92] DateTime serialization result is not same as Java 8 ZonedDateTime
public class DateTimeOwnZoneSerialization92Test
extends JodaTestBase
{
private final ObjectMapper MAPPER = mapperWithModuleBuilder().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();

@Test
public void dateTimeShouldRetainItsOwnZone() throws Exception {
DateTime jodaZonedDateTime = new DateTime(2023, 10, 1, 12, 2, 3, 123, DateTimeZone.forID("Asia/Shanghai"));

// with WRITE_DATES_WITH_CONTEXT_TIME_ZONE
assertEquals("\"2023-10-01T12:02:03.123+08:00\"",
MAPPER.writer()
.with(TimeZone.getTimeZone("UTC"))
.with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
.writeValueAsString(jodaZonedDateTime));

// without WRITE_DATES_WITH_CONTEXT_TIME_ZONE
assertEquals("\"2023-10-01T12:02:03.123+08:00\"",
MAPPER.writer()
.with(TimeZone.getTimeZone("UTC"))
.without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
.writeValueAsString(jodaZonedDateTime));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.fasterxml.jackson.datatype.joda.ser;

import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.joda.JodaTestBase;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JsonFormatTimeZoneWithPattern98Test extends JodaTestBase {
static class Wrapper3Z<T> {
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZZ",
timezone = "Europe/Budapest" // +01:00 in winter
)
public T value;
Wrapper3Z(T v) { value = v; }
}

static class Wrapper2Z<T> {
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS ZZ",
timezone = "Europe/Budapest"
)
public T value;
Wrapper2Z(T v) { value = v; }
}

static class Wrapper1Z<T> {
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS Z",
timezone = "Europe/Budapest" // +01:00 in winter
)
public T value;
Wrapper1Z(T v) { value = v; }
}

private final ObjectMapper MAPPER = mapperWithModuleBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.build();

@Test
public void patternShouldNotEraseTimeZone()
throws Exception
{
// DateTime already in Europe/Budapest zone (no shift)
_testSerialization(
"{\"value\":\"2018-01-01T12:01:02.003 Europe/Budapest\"}",
new Wrapper3Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Budapest")))));
// DateTime in UTC, should shift +1h
_testSerialization(
"{\"value\":\"2018-01-01T13:01:02.003 Europe/Budapest\"}",
new Wrapper3Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")))));
// LocalDate
_testSerialization(
"{\"value\":\"2018-01-01T��:��:��.000 \"}",
new Wrapper3Z<>(new LocalDate(2018,1,1)));
// LocalTime
_testSerialization(
"{\"value\":\"����-��-��T12:01:02.003 \"}",
new Wrapper3Z<>(new LocalTime(12,1,2,3)));
// LocalDateTime
_testSerialization(
"{\"value\":\"2018-01-01T12:01:02.003 \"}",
new Wrapper3Z<>(new LocalDateTime(2018,1,1,12,1,2,3)));
}

@Test
public void patternShouldNotEraseTimeZoneWithZZ()
throws Exception
{
// DateTime already in Europe/Budapest zone (no shift)
_testSerialization(
"{\"value\":\"2018-01-01T12:01:02.003 +01:00\"}",
new Wrapper2Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Budapest")))));
// DateTime in UTC, should shift +1h
_testSerialization(
"{\"value\":\"2018-01-01T13:01:02.003 +01:00\"}",
new Wrapper2Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")))));
}

@Test
public void patternShouldNotEraseTimeZoneWithZ()
throws Exception
{
// DateTime already in Europe/Budapest zone (no shift)
_testSerialization(
"{\"value\":\"2018-01-01T12:01:02.003 +0100\"}",
new Wrapper1Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Budapest")))));
// DateTime in UTC, should shift +1h
_testSerialization(
"{\"value\":\"2018-01-01T13:01:02.003 +0100\"}",
new Wrapper1Z<>(new DateTime(2018,1,1,12,1,2,3,
DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")))));
}

private void _testSerialization(String expectedJson, Object wrapper) throws Exception {
String actual = MAPPER.writeValueAsString(wrapper);
assertEquals(expectedJson, actual);
}

}
Loading