Skip to content

Commit

Permalink
Merge branch '2.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 30, 2020
2 parents da24bc6 + 36ace7c commit 23e3cec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.core.StreamReadCapability;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down Expand Up @@ -84,12 +86,8 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
case JsonTokenId.ID_NUMBER_FLOAT:
BigDecimal value = parser.getDecimalValue();
return DecimalUtils.extractSecondsAndNanos(value, Duration::ofSeconds);

case JsonTokenId.ID_NUMBER_INT:
if(context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) {
return Duration.ofSeconds(parser.getLongValue());
}
return Duration.ofMillis(parser.getLongValue());
return _fromTimestamp(context, parser.getLongValue());
case JsonTokenId.ID_STRING:
return _fromString(parser, context, parser.getText());
// 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML)
Expand All @@ -108,21 +106,35 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT);
}

protected Duration _fromString(JsonParser parser, DeserializationContext context,
String string) throws IOException
protected Duration _fromString(JsonParser parser, DeserializationContext ctxt,
String value) throws IOException
{
string = string.trim();
if (string.length() == 0) {
value = value.trim();
if (value.length() == 0) {
if (!isLenient()) {
return _failForNotLenient(parser, context, JsonToken.VALUE_STRING);
return _failForNotLenient(parser, ctxt, JsonToken.VALUE_STRING);
}
return null;
}
// 30-Sep-2020: Should allow use of "Timestamp as String" for
// some textual formats
if (ctxt.isEnabled(StreamReadCapability.UNTYPED_SCALARS)
&& _isValidTimestampString(value)) {
return _fromTimestamp(ctxt, NumberInput.parseLong(value));
}

try {
return Duration.parse(string);
return Duration.parse(value);
} catch (DateTimeException e) {
// null format -> "default formatter"
return _handleDateTimeFormatException(context, e, null, string);
return _handleDateTimeFormatException(ctxt, e, null, value);
}
}

protected Duration _fromTimestamp(DeserializationContext ctxt, long ts) {
if (ctxt.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) {
return Duration.ofSeconds(ts);
}
return Duration.ofMillis(ts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.core.io.NumberInput;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
Expand Down Expand Up @@ -93,6 +95,14 @@ public Object deserializeWithType(JsonParser parser, DeserializationContext cont
return typeDeserializer.deserializeTypedFromAny(parser, context);
}

// @since 2.12
protected boolean _isValidTimestampString(String str) {
// 30-Sep-2020, tatu: Need to support "numbers as Strings" for data formats
// that only have String values for scalars (CSV, Properties, XML)
// NOTE: we do allow negative values, but has to fit in 64-bits:
return _isIntNumber(str) && NumberInput.inLongRange(str, (str.charAt(0) == '-'));
}

protected <BOGUS> BOGUS _reportWrongToken(DeserializationContext context,
JsonToken exp, String unit) throws IOException
{
Expand Down

0 comments on commit 23e3cec

Please sign in to comment.