Skip to content

Commit e5b0adb

Browse files
committed
Add feature toggle to read numeric strings as numeric timestamps
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
1 parent 5112d3a commit e5b0adb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,11 @@ protected T _fromString(JsonParser p, DeserializationContext ctxt,
257257
// handled like "regular" empty (same as pre-2.12)
258258
return _fromEmptyString(p, ctxt, string);
259259
}
260-
// only check for other parsing modes if we are using default formatter
260+
// only check for other parsing modes if we are using default formatter or explicitly asked to
261261
if (_formatter == DateTimeFormatter.ISO_INSTANT ||
262262
_formatter == DateTimeFormatter.ISO_OFFSET_DATE_TIME ||
263-
_formatter == DateTimeFormatter.ISO_ZONED_DATE_TIME) {
263+
_formatter == DateTimeFormatter.ISO_ZONED_DATE_TIME ||
264+
ctxt.isEnabled(DeserializationFeature.READ_NUMERIC_STRINGS_AS_DATE_TIMESTAMP)) {
264265
// 22-Jan-2016, [datatype-jsr310#16]: Allow quoted numbers too
265266
int dots = _countPeriods(string);
266267
if (dots >= 0) { // negative if not simple number

datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,18 @@ public void testCustomPatternWithAnnotations() throws Exception
926926
assertEquals(input.value.toInstant(), result.value.toInstant());
927927
}
928928

929+
@Test
930+
public void testCustomPatternWithNumericTimestamp() throws Exception
931+
{
932+
String input = a2q("{'value':'3.141592653'}");
933+
934+
Wrapper result = MAPPER.readerFor(Wrapper.class)
935+
.with(DeserializationFeature.READ_NUMERIC_STRINGS_AS_DATE_TIMESTAMP)
936+
.readValue(input);
937+
938+
assertEquals(Instant.ofEpochSecond(3L, 141592653L), result.value.toInstant());
939+
}
940+
929941
@Test
930942
public void testNumericCustomPatternWithAnnotations() throws Exception
931943
{

0 commit comments

Comments
 (0)