-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
* test: add initial test case * fix: fix problems in test case for issue #1 * feat: add test cases for timestamps part and nanoseconds part related to issue #1 * create a false test cases #1 * Use flag for diabling fraction * Use flag for diabling fraction * Added the flag for DurationSerializer classes * Removed test cases that are not relevant to the added Serialization Feature * Only keep requirement specific tests Some tests were set up to get an understanding of the code. They should not be kept and included in the pull request Co-authored-by: Nagisa <[email protected]> Co-authored-by: Jacob Adlers <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,13 +81,16 @@ protected abstract JSR310FormattedSerializerBase<?> withFormat(DateTimeFormatter | |
public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException | ||
{ | ||
if (useTimestamp(provider)) { | ||
if (useNanoseconds(provider)) { | ||
if (withoutFraction(provider)) { | ||
generator.writeNumber(getEpochMillis.applyAsLong(value) / 1000); | ||
This comment has been minimized.
Sorry, something went wrong.
kupci
|
||
return; | ||
} else if (useNanoseconds(provider)) { | ||
generator.writeNumber(DecimalUtils.toBigDecimal( | ||
getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value) | ||
)); | ||
return; | ||
} | ||
generator.writeNumber(getEpochMillis.applyAsLong(value)); | ||
generator.writeNumber(getEpochMillis.applyAsLong(value)); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jadlers
Author
Collaborator
|
||
return; | ||
} | ||
String str; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
package com.fasterxml.jackson.datatype.jsr310.ser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonGetter; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; | ||
|
@@ -173,4 +176,58 @@ public void testSerializationWithTypeInfo03() throws Exception | |
assertEquals("The value is not correct.", | ||
"[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); | ||
} | ||
|
||
/** | ||
* When {@link | ||
* com.fasterxml.jackson.code.jackson-databind.WRITE_TIMESTAMPS_WITHOUT_FRACTION} | ||
* is enabled no fraction is added to the serialized string. | ||
* | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testSerializationWithFractionFlag() throws Exception | ||
{ | ||
class TempClass { | ||
@JsonProperty("registered_at") | ||
@JsonFormat(shape = JsonFormat.Shape.NUMBER) | ||
private Instant registeredAt; | ||
|
||
public TempClass(long seconds, int nanos) { | ||
this.registeredAt = Instant.ofEpochSecond(seconds, nanos); | ||
} | ||
} | ||
|
||
String value = MAPPER.writer() | ||
.with(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION) | ||
.writeValueAsString(new TempClass(1420324047L, 123456)); | ||
assertEquals("The value should not include any decimals.", "{\"registered_at\":1420324047}", value); | ||
} | ||
|
||
/** | ||
* When both WRITE_TIMESTAMPS_WITHOUT_FRACTION and | ||
* WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS from {@link | ||
* com.fasterxml.jackson.code.jackson-databind} are enabled the former one | ||
This comment has been minimized.
Sorry, something went wrong.
kupci
|
||
* has priority. | ||
* | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testSerializationFractionPriority() throws Exception | ||
{ | ||
class TempClass { | ||
@JsonProperty("registered_at") | ||
@JsonFormat(shape = JsonFormat.Shape.NUMBER) | ||
private Instant registeredAt; | ||
|
||
public TempClass(long seconds, int nanos) { | ||
this.registeredAt = Instant.ofEpochSecond(seconds, nanos); | ||
} | ||
} | ||
|
||
String value = MAPPER.writer() | ||
.with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) | ||
.with(SerializationFeature.WRITE_TIMESTAMPS_WITHOUT_FRACTION) | ||
.writeValueAsString(new TempClass(1420324047L, 123456)); | ||
assertEquals("The value does not include any decimals.", "{\"registered_at\":1420324047}", value); | ||
} | ||
} |
how about
useEpochSeconds
instead ofwithoutFraction
, to be a little more consistent with existing code/naming conventions?