Skip to content

Commit ab7f315

Browse files
committed
Merge branch '2.19'
2 parents d33bf6e + 28770b9 commit ab7f315

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

datetime/src/main/java/tools/jackson/datatype/jsr310/deser/InstantDeserializer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929

3030
import com.fasterxml.jackson.annotation.JsonFormat;
3131

32-
import tools.jackson.core.JacksonException;
33-
import tools.jackson.core.JsonParser;
34-
import tools.jackson.core.JsonToken;
35-
import tools.jackson.core.JsonTokenId;
32+
import tools.jackson.core.*;
3633
import tools.jackson.core.io.NumberInput;
3734
import tools.jackson.core.util.JacksonFeatureSet;
3835
import tools.jackson.databind.BeanProperty;
@@ -356,8 +353,13 @@ protected boolean shouldReadTimestampsAsNanoseconds(DeserializationContext conte
356353
protected int _countPeriods(String str)
357354
{
358355
int commas = 0;
359-
for (int i = 0, end = str.length(); i < end; ++i) {
360-
int ch = str.charAt(i);
356+
int i = 0;
357+
int ch = str.charAt(i);
358+
if (ch == '-' || ch == '+') {
359+
++i;
360+
}
361+
for (int end = str.length(); i < end; ++i) {
362+
ch = str.charAt(i);
361363
if (ch < '0' || ch > '9') {
362364
if (ch == '.') {
363365
++commas;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tools.jackson.datatype.jsr310.deser;
2+
3+
import java.time.Instant;
4+
import java.util.Locale;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import tools.jackson.core.json.JsonReadFeature;
9+
import tools.jackson.databind.ObjectReader;
10+
import tools.jackson.databind.exc.InvalidFormatException;
11+
import tools.jackson.databind.json.JsonMapper;
12+
import tools.jackson.datatype.jsr310.JavaTimeFeature;
13+
import tools.jackson.datatype.jsr310.JavaTimeModule;
14+
import tools.jackson.datatype.jsr310.ModuleTestBase;
15+
16+
import static org.junit.jupiter.api.Assertions.*;
17+
18+
// [modules-java8#291] InstantDeserializer fails to parse negative numeric timestamp strings for
19+
// pre-1970 values.
20+
public class InstantDeser291Test
21+
extends ModuleTestBase
22+
{
23+
private final JsonMapper MAPPER = JsonMapper.builder()
24+
.defaultLocale(Locale.ENGLISH)
25+
.addModule(new JavaTimeModule()
26+
.enable(JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS))
27+
.build();
28+
private final ObjectReader READER = MAPPER.readerFor(Instant.class);
29+
30+
private static final Instant INSTANT_3_SEC_AFTER_EPOC = Instant.ofEpochSecond(3);
31+
private static final Instant INSTANT_3_SEC_BEFORE_EPOC = Instant.ofEpochSecond(-3);
32+
33+
private static final String STR_3_SEC = "\"3.000000000\"";
34+
private static final String STR_POSITIVE_3 = "\"+3.000000000\"";
35+
private static final String STR_NEGATIVE_3 = "\"-3.000000000\"";
36+
37+
/**
38+
* Baseline that always succeeds, even before resolution of issue 291
39+
* @throws Exception
40+
*/
41+
@Test
42+
public void testNormalNumericalString() throws Exception {
43+
assertEquals(INSTANT_3_SEC_AFTER_EPOC, READER.readValue(STR_3_SEC));
44+
}
45+
46+
@Test
47+
public void testNegativeNumericalString() throws Exception {
48+
assertEquals(INSTANT_3_SEC_BEFORE_EPOC, READER.readValue(STR_NEGATIVE_3));
49+
}
50+
51+
@Test
52+
public void testAllowedPlusSignNumericalString() throws Exception {
53+
assertEquals(INSTANT_3_SEC_AFTER_EPOC, READER.readValue(STR_POSITIVE_3));
54+
}
55+
}

release-notes/CREDITS-2.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ Joo Hyuk Kim (@JooHyukKim)
209209
`WRITE_DURATIONS_AS_TIMESTAMPS` enabled
210210
(2.19.0)
211211

212+
Kevin Mahon (@Strongbeard)
213+
* Fixed #291: `InstantDeserializer` fails to parse negative numeric timestamp strings
214+
for pre-1970 values
215+
(2.18.4)
216+
212217
Joey Muia (@jmuia)
213218
* Reported #337: Negative `Duration` does not round-trip properly with
214219
`WRITE_DURATIONS_AS_TIMESTAMPS` enabled

release-notes/VERSION-2.x

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ Modules:
2020
is not time-zone aware
2121
(contributed by Henning P)
2222

23-
2.18.3 (not yet released)
23+
2.18.4 (not yet released)
24+
25+
#291: `InstantDeserializer` fails to parse negative numeric timestamp strings
26+
for pre-1970 values
27+
(reported by @advorako)
28+
(fixed by Kevin M)
29+
30+
2.18.3 (28-Feb-2025)
2431

2532
#333: `ZonedDateTime` serialization with `@JsonFormat.pattern` never uses it
2633
while `WRITE_DATES_WITH_ZONE_ID` enabled

0 commit comments

Comments
 (0)