diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java index 5a4065d0..da32c955 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java @@ -55,8 +55,12 @@ public class DurationDeserializer extends JSR310DeserializerBase /** * Since 2.12 + * When set, values will be deserialized using the specified unit. Using this parser will tipically + * override the value specified in {@link DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS} as it is + * considered that the unit set in {@link JsonFormat#pattern()} has precedence since is more specific. + * @see [jackson-modules-java8#184] for more info */ - private DurationPattern _durationPattern; + private DurationUnitParser _durationUnitParser; private DurationDeserializer() { @@ -70,9 +74,9 @@ protected DurationDeserializer(DurationDeserializer base, Boolean leniency) { super(base, leniency); } - protected DurationDeserializer(DurationDeserializer base, DurationPattern durationPattern) { + protected DurationDeserializer(DurationDeserializer base, DurationUnitParser durationUnitParser) { super(base, base._isLenient); - _durationPattern = durationPattern; + _durationUnitParser = durationUnitParser; } @Override @@ -94,7 +98,7 @@ public JsonDeserializer createContextual(DeserializationContext ctxt, } } if (format.hasPattern()) { - deser = DurationPattern.from(format.getPattern()) + deser = DurationUnitParser.from(format.getPattern()) .map(deser::withPattern) .orElse(deser); } @@ -102,7 +106,7 @@ public JsonDeserializer createContextual(DeserializationContext ctxt, return deser; } - private DurationDeserializer withPattern(DurationPattern pattern) { + private DurationDeserializer withPattern(DurationUnitParser pattern) { return new DurationDeserializer(this, pattern); } @@ -113,14 +117,14 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal decValue = parser.getDecimalValue(); - if (_durationPattern != null) { - return _durationPattern.parse(decValue.longValue()); + if (_durationUnitParser != null) { + return _durationUnitParser.parse(decValue.longValue()); } return DecimalUtils.extractSecondsAndNanos(decValue, Duration::ofSeconds); case JsonTokenId.ID_NUMBER_INT: long intValue = parser.getLongValue(); - if (_durationPattern != null) { - return _durationPattern.parse(intValue); + if (_durationUnitParser != null) { + return _durationUnitParser.parse(intValue); } return _fromTimestamp(context, intValue); case JsonTokenId.ID_STRING: @@ -160,8 +164,8 @@ && _isValidTimestampString(value)) { } try { - if (_durationPattern != null) { - return _durationPattern.parse(NumberInput.parseLong(value)); + if (_durationUnitParser != null) { + return _durationUnitParser.parse(NumberInput.parseLong(value)); } return Duration.parse(value); @@ -177,10 +181,10 @@ protected Duration _fromTimestamp(DeserializationContext ctxt, long ts) { return Duration.ofMillis(ts); } - protected static class DurationPattern { + protected static class DurationUnitParser { final TemporalUnit unit; - DurationPattern(TemporalUnit unit) { + DurationUnitParser(TemporalUnit unit) { this.unit = unit; } @@ -188,10 +192,10 @@ Duration parse(long value) { return Duration.of(value, unit); } - static Optional from(String pattern) { + static Optional from(String unit) { return Stream.of(ChronoUnit.values()) - .filter(u -> u.toString().equalsIgnoreCase(pattern)) - .map(DurationPattern::new) + .filter(u -> u.toString().equalsIgnoreCase(unit)) + .map(DurationUnitParser::new) .findFirst(); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternEmptyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserEmptyTest.java similarity index 61% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternEmptyTest.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserEmptyTest.java index dfb89d77..efeb9fda 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternEmptyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserEmptyTest.java @@ -1,30 +1,30 @@ package com.fasterxml.jackson.datatype.jsr310.deser; -import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationPattern; +import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationUnitParser; import org.junit.Test; import static java.util.Optional.empty; import static org.junit.Assert.assertEquals; -public class DurationPatternEmptyTest { +public class DurationUnitParserEmptyTest { @Test public void shouldReturnEmpty_whenNull() { - assertEquals(empty(), DurationPattern.from(null)); + assertEquals(empty(), DurationUnitParser.from(null)); } @Test public void shouldReturnEmpty_whenEmptyString() { - assertEquals(empty(), DurationPattern.from("")); + assertEquals(empty(), DurationUnitParser.from("")); } @Test public void shouldReturnEmpty_whenSpaces() { - assertEquals(empty(), DurationPattern.from(" ")); + assertEquals(empty(), DurationUnitParser.from(" ")); } @Test public void shouldReturnEmpty_whenDoesNotMatchAnyTemporalUnit() { - assertEquals(empty(), DurationPattern.from("DOESNOTMATCH")); + assertEquals(empty(), DurationUnitParser.from("DOESNOTMATCH")); } } \ No newline at end of file diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserTest.java similarity index 91% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternTest.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserTest.java index ec5c2a29..906daa9d 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationPatternTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationUnitParserTest.java @@ -1,6 +1,6 @@ package com.fasterxml.jackson.datatype.jsr310.deser; -import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationPattern; +import com.fasterxml.jackson.datatype.jsr310.deser.DurationDeserializer.DurationUnitParser; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -19,19 +19,19 @@ import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) -public class DurationPatternTest { +public class DurationUnitParserTest { private final String stringPattern; private final TemporalUnit temporalUnit; - public DurationPatternTest(String stringPattern, TemporalUnit temporalUnit) { + public DurationUnitParserTest(String stringPattern, TemporalUnit temporalUnit) { this.stringPattern = stringPattern; this.temporalUnit = temporalUnit; } @Test public void shouldMapToTemporalUnit() { - Optional durationPattern = DurationPattern.from(stringPattern); + Optional durationPattern = DurationUnitParser.from(stringPattern); assertEquals(of(temporalUnit), durationPattern.map(dp -> dp.unit)); }