Skip to content

Commit

Permalink
feature : add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim committed Dec 5, 2024
1 parent e5a162f commit 122dbb1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ protected JSR310FormattedSerializerBase<?> withFeatures(Boolean writeZoneId, Boo
public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider provider)
throws IOException
{
// [modules-java8#333]: `@JsonFormat` with pattern should override `SerializationFeature.WRITE_DATES_WITH_ZONE_ID`
if (_formatter != null && _shape == JsonFormat.Shape.STRING) {
super.serialize(value, g, provider);
return;
}
if (!useTimestamp(provider)) {
if (shouldWriteWithZoneId(provider)) {
// write with zone
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.fasterxml.jackson.datatype.jsr310.ser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
import org.junit.Test;

import java.time.ZonedDateTime;

import static org.junit.Assert.assertEquals;

// [module-java8#333] : ZonedDateTime serialization with @JsonFormat pattern never uses it while WRITE_DATES_WITH_ZONE_ID enabled #333
public class ZonedDateTimeSerializationWithJsonFormat333Test
extends ModuleTestBase
{

public static class ContainerWithPattern333 {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss z")
public ZonedDateTime value;
}

public static class ContainerWithoutPattern333 {
@JsonFormat(shape = JsonFormat.Shape.STRING)
public ZonedDateTime value;
}

private final ObjectMapper MAPPER = mapperBuilder().enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID).build();

@Test
public void testJsonFormatOverridesSerialization() throws Exception
{
// ISO-8601 string for ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2024-11-15T18:27:06.921054+01:00[Europe/Berlin]");
ContainerWithPattern333 input = new ContainerWithPattern333();
input.value = zonedDateTime;

String json = MAPPER.writeValueAsString(input);

assertEquals(a2q("{'value':'2024-11-15 18:27:06 CET'}"), json);
}

}

0 comments on commit 122dbb1

Please sign in to comment.