Skip to content
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

Fix #288: consider custom serializer additions via JavaTimeModule #289

Merged
merged 2 commits into from
Nov 21, 2023
Merged
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 @@ -140,7 +140,13 @@ public void setupModule(SetupContext context) {
desers.addDeserializer(YearMonth.class, YearMonthDeserializer.INSTANCE);
desers.addDeserializer(ZoneId.class, JSR310StringParsableDeserializer.ZONE_ID);
desers.addDeserializer(ZoneOffset.class, JSR310StringParsableDeserializer.ZONE_OFFSET);

context.addDeserializers(desers);
// 20-Nov-2023, tatu: [modules-java8#288]: someone may have directly
// added entries, need to add for backwards compatibility
if (_deserializers != null) {
context.addDeserializers(_deserializers);
}

SimpleSerializers sers = new SimpleSerializers();

Expand Down Expand Up @@ -169,11 +175,21 @@ public void setupModule(SetupContext context) {
sers.addSerializer(ZoneOffset.class, new ToStringSerializer(ZoneOffset.class));

context.addSerializers(sers);
// 20-Nov-2023, tatu: [modules-java8#288]: someone may have directly
// added entries, need to add for backwards compatibility
if (_serializers != null) {
context.addSerializers(_serializers);
}

// key serializers
SimpleSerializers keySers = new SimpleSerializers();
keySers.addSerializer(ZonedDateTime.class, ZonedDateTimeKeySerializer.INSTANCE);
context.addKeySerializers(keySers);
// 20-Nov-2023, tatu: [modules-java8#288]: someone may have directly
// added entries, need to add for backwards compatibility
if (_keySerializers != null) {
context.addKeySerializers(_keySerializers);
}

// key deserializers
SimpleKeyDeserializers keyDesers = new SimpleKeyDeserializers();
Expand All @@ -191,7 +207,13 @@ public void setupModule(SetupContext context) {
keyDesers.addDeserializer(ZonedDateTime.class, ZonedDateTimeKeyDeserializer.INSTANCE);
keyDesers.addDeserializer(ZoneId.class, ZoneIdKeyDeserializer.INSTANCE);
keyDesers.addDeserializer(ZoneOffset.class, ZoneOffsetKeyDeserializer.INSTANCE);

context.addKeyDeserializers(keyDesers);
// 20-Nov-2023, tatu: [modules-java8#288]: someone may have directly
// added entries, need to add for backwards compatibility
if (_keyDeserializers != null) {
context.addKeyDeserializers(_keyDeserializers);
}

context.addValueInstantiators(new ValueInstantiators.Base() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;

Expand All @@ -40,38 +43,33 @@ static class LDTWrapper {
public LDTWrapper(LocalDateTime v) { value = v; }
}

private final ObjectMapper mapper = newMapper();
private final static ObjectMapper MAPPER = newMapper();

@Test
public void testSerializationAsTimestamp01() throws Exception
{
LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43);
String value = mapper.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", "[1986,1,17,15,43]", value);
assertEquals("The value is not correct.", "[1986,1,17,15,43]",
MAPPER.writeValueAsString(time));
}

@Test
public void testSerializationAsTimestamp02() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57);
String value = mapper.writeValueAsString(time);
String value = MAPPER.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", "[2013,8,21,9,22,57]", value);
}

@Test
public void testSerializationAsTimestamp03Nanosecond() throws Exception
{
LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57);

ObjectMapper m = newMapper().enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.enable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
String value = MAPPER.writer()
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)
.writeValueAsString(time);
assertEquals("The value is not correct.", "[2013,8,21,9,22,0,57]", value);
}

Expand All @@ -83,7 +81,6 @@ public void testSerializationAsTimestamp03Millisecond() throws Exception
ObjectMapper m = newMapper().disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", "[2013,8,21,9,22,0,0]", value);
}

Expand All @@ -96,7 +93,6 @@ public void testSerializationAsTimestamp04Nanosecond() throws Exception
.enable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", "[2005,11,5,22,31,5,829837]", value);
}

Expand All @@ -109,7 +105,6 @@ public void testSerializationAsTimestamp04Millisecond() throws Exception
.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", "[2005,11,5,22,31,5,422]", value);
}

Expand Down Expand Up @@ -157,7 +152,7 @@ public void testSerializationWithFormatOverride() throws Exception
{
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 999000);
assertEquals(a2q("{'value':'2005-11-05A22:31:05'}"),
mapper.writeValueAsString(new LDTWrapper(time)));
MAPPER.writeValueAsString(new LDTWrapper(time)));

ObjectMapper m = mapperBuilder().withConfigOverride(LocalDateTime.class,
cfg -> cfg.setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd'X'HH:mm")))
Expand All @@ -176,7 +171,6 @@ public void testSerializationWithTypeInfo01() throws Exception
m.addMixIn(Temporal.class, MockObjectConfiguration.class);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.",
"[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value);
}
Expand All @@ -192,7 +186,6 @@ public void testSerializationWithTypeInfo02() throws Exception
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.",
"[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value);
}
Expand All @@ -206,8 +199,20 @@ public void testSerializationWithTypeInfo03() throws Exception
LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837);
String value = m.writeValueAsString(time);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.",
"[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value);
}

// [modules-java8#288]
@Test
public void testExplicitConstructionWithPattern() throws Exception
{
ObjectMapper mapper = JsonMapper.builder()
.addModule(new JavaTimeModule()
.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("MM/dd/yyyy")))
).build();
LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43);
assertEquals(q("01/17/1986"), mapper.writeValueAsString(time));
}
}
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Modules:
=== Releases ===
------------------------------------------------------------------------

2.16.1 (not yet released)

#288: `LocalDateTime` serialization issue with custom-configured
`LocalDateTimeSerializer`
(reported by @mklinkj)

2.16.0 (15-Nov-2023)

#263: Add `JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_TIMESTAMPS` to allow parsing
Expand Down