Skip to content

Commit 421c7bc

Browse files
committed
Merge branch '2.19'
2 parents b99ec99 + fa6a27a commit 421c7bc

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

datetime/src/main/java/tools/jackson/datatype/jsr310/ser/InstantSerializerBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public abstract class InstantSerializerBase<T extends Temporal>
5252

5353
protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpochMillis,
5454
ToLongFunction<T> getEpochSeconds, ToIntFunction<T> getNanoseconds,
55-
DateTimeFormatter formatter)
55+
DateTimeFormatter defaultFormat)
5656
{
5757
// Bit complicated, just because we actually want to "hide" default formatter,
5858
// so that it won't accidentally force use of textual presentation
5959
super(supportedType, null);
60-
defaultFormat = formatter;
60+
this.defaultFormat = defaultFormat;
6161
this.getEpochMillis = getEpochMillis;
6262
this.getEpochSeconds = getEpochSeconds;
6363
this.getNanoseconds = getNanoseconds;
@@ -129,7 +129,7 @@ protected JsonToken serializationShape(SerializationContext ctxt) {
129129

130130
protected String formatValue(T value, SerializationContext ctxt)
131131
{
132-
DateTimeFormatter formatter = (_formatter != null) ? _formatter : defaultFormat;
132+
DateTimeFormatter formatter = (_formatter == null) ? defaultFormat :_formatter;
133133
if (formatter != null) {
134134
if (formatter.getZone() == null) { // timezone set if annotated on property
135135
// If the user specified to use the context TimeZone explicitly, and the formatter provided doesn't contain a TZ

datetime/src/main/java/tools/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class JSR310FormattedSerializerBase<T>
4949
protected final Boolean _useNanoseconds;
5050

5151
/**
52-
* Specific format to use, if not default format: non null value
52+
* Specific format to use, if not default format: non-null value
5353
* also indicates that serialization is to be done as JSON String,
5454
* not numeric timestamp, unless {@code #_useTimestamp} is true.
5555
*/

datetime/src/main/java/tools/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public void serialize(ZonedDateTime value, JsonGenerator g, SerializationContext
6666
throws JacksonException
6767
{
6868
if (!useTimestamp(ctxt)) {
69-
if (shouldWriteWithZoneId(ctxt)) {
69+
// [modules-java8#333]: `@JsonFormat` with pattern should override
70+
// `SerializationFeature.WRITE_DATES_WITH_ZONE_ID`
71+
if ((_formatter != null) && (_shape == JsonFormat.Shape.STRING)) {
72+
; // use default handling
73+
} else if (shouldWriteWithZoneId(ctxt)) {
7074
// write with zone
7175
g.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value));
7276
return;
@@ -75,6 +79,19 @@ public void serialize(ZonedDateTime value, JsonGenerator g, SerializationContext
7579
super.serialize(value, g, ctxt);
7680
}
7781

82+
@Override
83+
protected String formatValue(ZonedDateTime value, SerializationContext ctxt) {
84+
String formatted = super.formatValue(value, ctxt);
85+
// [modules-java8#333]: `@JsonFormat` with pattern should override
86+
// `SerializationFeature.WRITE_DATES_WITH_ZONE_ID`
87+
if (_formatter != null && _shape == JsonFormat.Shape.STRING) {
88+
// Why not `if (shouldWriteWithZoneId(provider))` ?
89+
if (Boolean.TRUE.equals(_writeZoneId)) {
90+
formatted += "[" + value.getZone().getId() + "]";
91+
}
92+
}
93+
return formatted;
94+
}
7895
public boolean shouldWriteWithZoneId(SerializationContext ctxt) {
7996
return (_writeZoneId != null) ? _writeZoneId :
8097
ctxt.isEnabled(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);

datetime/src/test/java/tools/jackson/datatype/jsr310/ser/WriteZoneIdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class WriteZoneIdTest extends ModuleTestBase
2121
{
2222
static class DummyClassWithDate {
2323
@JsonFormat(shape = JsonFormat.Shape.STRING,
24-
pattern = "dd-MM-yyyy hh:mm:ss Z",
24+
pattern = "dd-MM-yyyy'T'hh:mm:ss Z",
2525
with = JsonFormat.Feature.WRITE_DATES_WITH_ZONE_ID)
2626
public ZonedDateTime date;
2727

@@ -73,7 +73,7 @@ public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson() throws Exceptio
7373
// 30-Jun-2016, tatu: Exact time seems to vary a bit based on DST, so let's actually
7474
// just verify appending of timezone id itself:
7575
String json = MAPPER.writeValueAsString(input);
76-
if (!json.contains("\"1970-01-01T")) {
76+
if (!json.contains("\"01-01-1970T")) {
7777
Assert.fail("Should contain time prefix, did not: "+json);
7878
}
7979
String match = String.format("[%s]", ZONE_ID_STR);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tools.jackson.datatype.jsr310.ser;
2+
3+
import java.time.ZonedDateTime;
4+
5+
import org.junit.Test;
6+
7+
import com.fasterxml.jackson.annotation.JsonFormat;
8+
9+
import tools.jackson.databind.ObjectMapper;
10+
import tools.jackson.databind.SerializationFeature;
11+
import tools.jackson.datatype.jsr310.ModuleTestBase;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
// [module-java8#333]: ZonedDateTime serialization with @JsonFormat pattern never uses
16+
// while WRITE_DATES_WITH_ZONE_ID enabled #333
17+
public class ZonedDateTimeSerWithJsonFormat333Test
18+
extends ModuleTestBase
19+
{
20+
public static class ContainerWithPattern333 {
21+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss z")
22+
public ZonedDateTime value;
23+
}
24+
25+
public static class ContainerWithoutPattern333 {
26+
@JsonFormat(shape = JsonFormat.Shape.STRING)
27+
public ZonedDateTime value;
28+
}
29+
30+
private final ObjectMapper MAPPER = mapperBuilder().enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID).build();
31+
32+
@Test
33+
public void testJsonFormatOverridesSerialization() throws Exception
34+
{
35+
// ISO-8601 string for ZonedDateTime
36+
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2024-11-15T18:27:06.921054+01:00[Europe/Berlin]");
37+
ContainerWithPattern333 input = new ContainerWithPattern333();
38+
input.value = zonedDateTime;
39+
40+
assertEquals(a2q("{'value':'2024-11-15 18:27:06 CET'}"),
41+
MAPPER.writeValueAsString(input));
42+
}
43+
}

0 commit comments

Comments
 (0)