Skip to content

Commit

Permalink
Use flag for diabling fraction
Browse files Browse the repository at this point in the history
  • Loading branch information
jadlers committed Feb 27, 2020
1 parent 71d1778 commit 020dfb0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 020dfb0

Please sign in to comment.