Skip to content

Commit

Permalink
Add feature toggle to read numeric strings as numeric timestamps
Browse files Browse the repository at this point in the history
There is an apparent inconsistency in the way Jackson de-serializes numbers
that are shaped as a string into an instant.

 * When not providing a custom date format, quoted numbers are treated as
   epoch milis/nanos
 * When not providing a custom date, quoted numbers are assumed to be handled
   by the pattern.

There is however no way to construct a pattern that handles both ISO dates
and epoch milis/nanos. This feature toggle allow numeric strings to be read
as numeric timestamps.

Fixes FasterXML#263
  • Loading branch information
mpkorstanje committed Nov 12, 2023
1 parent 734973b commit d93b065
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ protected T _fromString(JsonParser p, DeserializationContext ctxt,
// handled like "regular" empty (same as pre-2.12)
return _fromEmptyString(p, ctxt, string);
}
// only check for other parsing modes if we are using default formatter
// only check for other parsing modes if we are using default formatter or explicitly asked to
if (_formatter == DateTimeFormatter.ISO_INSTANT ||
_formatter == DateTimeFormatter.ISO_OFFSET_DATE_TIME ||
_formatter == DateTimeFormatter.ISO_ZONED_DATE_TIME) {
_formatter == DateTimeFormatter.ISO_ZONED_DATE_TIME ||
ctxt.isEnabled(DeserializationFeature.READ_NUMERIC_STRINGS_AS_DATE_TIMESTAMP)) {
// 22-Jan-2016, [datatype-jsr310#16]: Allow quoted numbers too
int dots = _countPeriods(string);
if (dots >= 0) { // negative if not simple number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,18 @@ public void testCustomPatternWithAnnotations() throws Exception
assertEquals(input.value.toInstant(), result.value.toInstant());
}

@Test
public void testCustomPatternWithNumericTimestamp() throws Exception
{
String input = a2q("{'value':'3.141592653'}");

Wrapper result = MAPPER.readerFor(Wrapper.class)
.with(DeserializationFeature.READ_NUMERIC_STRINGS_AS_DATE_TIMESTAMP)
.readValue(input);

assertEquals(Instant.ofEpochSecond(3L, 141592653L), result.value.toInstant());
}

@Test
public void testNumericCustomPatternWithAnnotations() throws Exception
{
Expand Down

0 comments on commit d93b065

Please sign in to comment.