Skip to content

Commit f782c49

Browse files
committed
fixup! fixup! Support for jsonformat in duration deserializer based on Duration::of(long,TemporalUnit). ref FasterXML#184
1 parent 05c231f commit f782c49

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

Diff for: datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ && _isValidTimestampString(value)) {
156156
return _fromTimestamp(ctxt, NumberInput.parseLong(value));
157157
}
158158

159-
if (_durationPattern != null) {
160-
return _durationPattern.parse(NumberInput.parseLong(value));
161-
}
162-
163159
try {
160+
if (_durationPattern != null) {
161+
return _durationPattern.parse(NumberInput.parseLong(value));
162+
}
163+
164164
return Duration.parse(value);
165-
} catch (DateTimeException e) {
166-
return _handleDateTimeException(ctxt, e, value);
165+
} catch (NumberFormatException | DateTimeException e) {
166+
return _handleWeirdStringValue(ctxt, e, value);
167167
}
168168
}
169169

Diff for: datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DeserializerBase.java

+7
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ protected <BOGUS> BOGUS _reportWrongToken(JsonParser parser, DeserializationCont
141141
@SuppressWarnings("unchecked")
142142
protected <R> R _handleDateTimeException(DeserializationContext context,
143143
DateTimeException e0, String value) throws JsonMappingException
144+
{
145+
return _handleWeirdStringValue(context, e0, value);
146+
}
147+
148+
@SuppressWarnings("unchecked")
149+
protected <R, E extends RuntimeException> R _handleWeirdStringValue(DeserializationContext context,
150+
E e0, String value) throws JsonMappingException
144151
{
145152
try {
146153
return (R) context.handleWeirdStringValue(handledType(), value,

Diff for: datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserTest.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import java.math.BigInteger;
44
import java.time.Duration;
5-
import java.time.LocalDateTime;
65
import java.time.temporal.TemporalAmount;
76
import java.util.Map;
87

98
import com.fasterxml.jackson.annotation.JsonFormat;
10-
import com.fasterxml.jackson.annotation.OptBoolean;
119
import com.fasterxml.jackson.core.type.TypeReference;
1210
import org.junit.Test;
1311

@@ -21,6 +19,7 @@
2119
import com.fasterxml.jackson.databind.JsonMappingException;
2220
import com.fasterxml.jackson.databind.ObjectMapper;
2321
import com.fasterxml.jackson.databind.ObjectReader;
22+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
2423
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
2524
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
2625
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
@@ -433,7 +432,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception {
433432
}
434433

435434
@Test
436-
public void shouldDeserializeInHours_whenValueIsString() throws Exception {
435+
public void shouldDeserializeInHours_whenUnitAsPattern_andValueIsString() throws Exception {
437436
ObjectMapper mapper = newMapper();
438437
ObjectReader reader = mapper.readerFor(MAP_TYPE_REF);
439438

@@ -442,8 +441,18 @@ public void shouldDeserializeInHours_whenValueIsString() throws Exception {
442441
assertEquals(Duration.ofHours(25), wrapper.value);
443442
}
444443

444+
@Test(expected = InvalidFormatException.class)
445+
public void shouldHandleException_whenUsingUnitAsPattern_andValueIsString() throws Exception {
446+
ObjectMapper mapper = newMapper();
447+
ObjectReader reader = mapper.readerFor(MAP_TYPE_REF);
448+
449+
Wrapper wrapper = reader.readValue("{\"value\":\"FAIL\"}", Wrapper.class);
450+
451+
assertEquals(Duration.ofHours(25), wrapper.value);
452+
}
453+
445454
@Test
446-
public void shouldDeserializeInHours_whenValueIsInteger() throws Exception {
455+
public void shouldDeserializeInHours_whenUnitAsPattern_andValueIsInteger() throws Exception {
447456
ObjectMapper mapper = newMapper();
448457
ObjectReader reader = mapper.readerFor(MAP_TYPE_REF);
449458

@@ -453,7 +462,7 @@ public void shouldDeserializeInHours_whenValueIsInteger() throws Exception {
453462
}
454463

455464
@Test
456-
public void shouldDeserializeInHours_whenValueIsFloat() throws Exception {
465+
public void shouldDeserializeInHours_whenUnitAsPattern_andValueIsFloat() throws Exception {
457466
ObjectMapper mapper = newMapper();
458467
ObjectReader reader = mapper.readerFor(MAP_TYPE_REF);
459468

0 commit comments

Comments
 (0)