From 020dfb0f30c22be72ad65600cca1cfa57e8991c4 Mon Sep 17 00:00:00 2001 From: Jacob Adlers Date: Thu, 27 Feb 2020 10:23:11 +0100 Subject: [PATCH] Use flag for diabling fraction --- .../datatype/jsr310/ser/InstantSerializerBase.java | 10 +++++++--- .../jsr310/ser/JSR310FormattedSerializerBase.java | 5 +++++ .../jackson/datatype/jsr310/ser/InstantSerTest.java | 5 +++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index c9b9286d..a8c4743d 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -82,9 +82,13 @@ public void serialize(T value, JsonGenerator generator, SerializerProvider provi { if (useTimestamp(provider)) { if (useNanoseconds(provider)) { - generator.writeNumber(DecimalUtils.toBigDecimal( - getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value) - )); + if (withoutFraction(provider)) { + generator.writeNumber(getEpochMillis.applyAsLong(value) / 1000); + } else { + generator.writeNumber(DecimalUtils.toBigDecimal( + getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value) + )); + } return; } generator.writeNumber(getEpochMillis.applyAsLong(value)); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java index 45396c9e..77093768 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java @@ -240,4 +240,9 @@ protected boolean useNanoseconds(SerializerProvider provider) { return (provider != null) && provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); } + + protected boolean withoutFraction(SerializerProvider provider) { + return (provider != null) + && provider.isEnabled(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION); + } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java index db8cc116..f9b93eba 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java @@ -191,8 +191,9 @@ public TempClass(long seconds, int nanos) { } String value = MAPPER.writer() - .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .writeValueAsString(new TempClass(1420324047, 123456)); + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .with(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION) + .writeValueAsString(new TempClass(1420324047, 123456)); assertEquals("The decimals should be deprecated.", "{\"registered_at\":1420324047}", value); }