Skip to content

Fix InstantSerializer ignoring the JsonFormat shape #242

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

Merged
merged 1 commit into from
Apr 30, 2022
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 @@ -40,9 +40,18 @@ protected InstantSerializer() {
null);
}

@Deprecated // since 2.14
protected InstantSerializer(InstantSerializer base,
Boolean useTimestamp, DateTimeFormatter formatter) {
this(base, useTimestamp, null, formatter);
this(base, useTimestamp, base._useNanoseconds, formatter);
}

/**
* @since 2.14
*/
protected InstantSerializer(InstantSerializer base, Boolean useTimestamp,
DateTimeFormatter formatter, JsonFormat.Shape shape) {
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
}

protected InstantSerializer(InstantSerializer base,
Expand All @@ -53,7 +62,7 @@ protected InstantSerializer(InstantSerializer base,
@Override
protected JSR310FormattedSerializerBase<Instant> withFormat(Boolean useTimestamp,
DateTimeFormatter formatter, JsonFormat.Shape shape) {
return new InstantSerializer(this, useTimestamp, formatter);
return new InstantSerializer(this, useTimestamp, formatter, shape);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,21 @@ protected InstantSerializerBase(Class<T> supportedType, ToLongFunction<T> getEpo
protected InstantSerializerBase(InstantSerializerBase<T> base,
Boolean useTimestamp, DateTimeFormatter dtf)
{
this(base, useTimestamp, null, dtf);
this(base, useTimestamp, base._useNanoseconds, dtf);
}

protected InstantSerializerBase(InstantSerializerBase<T> base,
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter dtf)
{
super(base, useTimestamp, useNanoseconds, dtf, null);
this(base, useTimestamp, useNanoseconds, dtf, base._shape);
}

/**
* @since 2.14
*/
protected InstantSerializerBase(InstantSerializerBase<T> base, Boolean useTimestamp,
Boolean useNanoseconds, DateTimeFormatter dtf, JsonFormat.Shape shape) {
super(base, useTimestamp, useNanoseconds, dtf, shape);
defaultFormat = base.defaultFormat;
getEpochMillis = base.getEpochMillis;
getEpochSeconds = base.getEpochSeconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,30 @@ protected OffsetDateTimeSerializer() {
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}

@Deprecated // since 2.14
protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base,
Boolean useTimestamp, DateTimeFormatter formatter) {
this(base, useTimestamp, null, formatter);
this(base, useTimestamp, base._useNanoseconds, formatter);
}

protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base,
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter) {
super(base, useTimestamp, useNanoseconds, formatter);
}

/**
* @since 2.14
*/
public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimestamp,
DateTimeFormatter formatter, JsonFormat.Shape shape) {
super(base, useTimestamp, base._useNanoseconds, formatter, shape);
}

@Override
protected JSR310FormattedSerializerBase<?> withFormat(Boolean useTimestamp,
DateTimeFormatter formatter, JsonFormat.Shape shape)
{
return new OffsetDateTimeSerializer(this, useTimestamp, formatter);
return new OffsetDateTimeSerializer(this, useTimestamp, formatter, shape);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,23 @@ public ZonedDateTimeSerializer(DateTimeFormatter formatter) {

protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
Boolean useTimestamp, DateTimeFormatter formatter, Boolean writeZoneId) {
this(base, useTimestamp, null, formatter, writeZoneId);
this(base, useTimestamp, base._useNanoseconds, formatter, base._shape, writeZoneId);
}

@Deprecated // since 2.14
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter,
Boolean writeZoneId) {
super(base, useTimestamp, useNanoseconds, formatter);
this(base, useTimestamp, useNanoseconds, formatter, base._shape, writeZoneId);
}

/**
* @since 2.14
*/
protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base,
Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter,
JsonFormat.Shape shape, Boolean writeZoneId) {
super(base, useTimestamp, useNanoseconds, formatter, shape);
_writeZoneId = writeZoneId;
}

Expand All @@ -51,7 +61,8 @@ protected JSR310FormattedSerializerBase<?> withFormat(
Boolean useTimestamp,
DateTimeFormatter formatter,
JsonFormat.Shape shape) {
return new ZonedDateTimeSerializer(this, useTimestamp, formatter, _writeZoneId);
return new ZonedDateTimeSerializer(this, useTimestamp, _useNanoseconds, formatter,
shape, _writeZoneId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@

package com.fasterxml.jackson.datatype.jsr310.ser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils;
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;

import org.junit.Test;

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

public class InstantSerTest extends ModuleTestBase
{
Expand Down Expand Up @@ -171,5 +171,15 @@ public void testSerializationWithTypeInfo03() throws Exception
"[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value);
}

private static class Pojo1 {
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
public Instant t1 = Instant.parse("2022-04-27T12:00:00Z");
public Instant t2 = t1;
}

@Test
public void testShapeInt() throws JsonProcessingException {
String json1 = newMapper().writeValueAsString(new Pojo1());
assertEquals("{\"t1\":1651060800000,\"t2\":1651060800.000000000}", json1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -259,4 +260,16 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff()
// We expect to have the date written with the ZoneId Z3
assertEquals("The value is incorrect", "\"" + FORMATTER.format(date) + "\"", value);
}

private static class Pojo1 {
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
public OffsetDateTime t1 = OffsetDateTime.parse("2022-04-27T12:00:00+02:00");
public OffsetDateTime t2 = t1;
}

@Test
public void testShapeInt() throws JsonProcessingException {
String json1 = newMapper().writeValueAsString(new Pojo1());
assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -947,6 +948,18 @@ public void testInstantPriorToEpochIsEqual() throws Exception
assertEquals(original, deserialized);
}

private static class Pojo1 {
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
public ZonedDateTime t1 = ZonedDateTime.parse("2022-04-27T12:00:00+02:00[Europe/Paris]");
public ZonedDateTime t2 = t1;
}

@Test
public void testShapeInt() throws JsonProcessingException {
String json1 = newMapper().writeValueAsString(new Pojo1());
assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1);
}

private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual)
{
assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.",
Expand Down