Skip to content

Commit

Permalink
Partial fix for #117
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 15, 2020
1 parent d5c1cd6 commit e03a06f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 15 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project: jackson-datatype-joda

#116: Improve schema support for DateTimeZone type
(contributed by Joost v-d-W)
#117: Timestamp deserialization not working for CSV, Properties or XML

2.11.1 (25-Jun-2020)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonTokenId;

import com.fasterxml.jackson.core.StreamReadCapability;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
Expand Down Expand Up @@ -48,8 +49,7 @@ public ReadableInstant deserialize(JsonParser p, DeserializationContext ctxt)
DateTimeZone tz;
switch (p.currentTokenId()) {
case JsonTokenId.ID_NUMBER_INT:
tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new DateTime(p.getLongValue(), tz);
return _fromTimestamp(ctxt, p.getLongValue());
case JsonTokenId.ID_STRING:
String str = p.getText().trim();
if (str.length() == 0) {
Expand Down Expand Up @@ -92,10 +92,23 @@ public ReadableInstant deserialize(JsonParser p, DeserializationContext ctxt)
}
return result;
}
// 14-Jul-2020: [datatype-joda#117] Should allow use of "Timestamp as String" for
// some textual formats
if (ctxt.isEnabled(StreamReadCapability.UNTYPED_SCALARS)
&& _isValidTimestampString(str)) {
return _fromTimestamp(ctxt, NumberInput.parseLong(str));
}

// Not sure if it should use timezone or not...
// 15-Sep-2015, tatu: impl of 'createParser()' SHOULD handle all timezone/locale setup
return _format.createParser(ctxt).parseDateTime(str);
}
return _handleNotNumberOrString(p, ctxt);
}

protected DateTime _fromTimestamp(DeserializationContext ctxt, long ts) {
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone()
: DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new DateTime(ts, tz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
case JsonTokenId.ID_STRING:
String str = p.getText().trim();
if (str.length() == 0) {
return null;
return getNullValue(ctxt);
}
// 11-Sep-2018, tatu: `DateTimeDeserializer` allows timezone inclusion in brackets;
// should that be checked here too?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ public Interval deserialize(JsonParser p, DeserializationContext ctxt)
{
if (!p.hasToken(JsonToken.VALUE_STRING)) {
return (Interval) ctxt.handleUnexpectedToken(handledType(),
p.getCurrentToken(), p, "expected JSON String");
p.currentToken(), p, "expected JSON String");
}

String v = p.getText().trim();

/* 17-Nov-2014, tatu: Actually let's start with slash, instead of hyphen, because
* that is the separator for standard functionality...
*/
// 17-Nov-2014, tatu: Actually let's start with slash, instead of hyphen, because
// that is the separator for standard functionality...
int index = v.indexOf('/', 1);
boolean hasSlash = (index > 0);
if (!hasSlash) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.datatype.joda.deser;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -60,4 +61,12 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
}
return this;
}

// @since 2.12
protected boolean _isValidTimestampString(String str) {
// 14-Jul-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) == '-'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public LocalTime deserialize(JsonParser p, DeserializationContext ctxt)
return new LocalTime(p.getLongValue());
case JsonTokenId.ID_STRING:
String str = p.getText().trim();
return (str.length() == 0) ? null
: _format.createParser(ctxt).parseLocalTime(str);
if (str.length() == 0) {
return getNullValue(ctxt);
}
return _format.createParser(ctxt).parseLocalTime(str);
default:
}
// [HH,MM,ss,ms?]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public MonthDay deserialize(final JsonParser p, final DeserializationContext ctx
}
return MonthDay.parse(str, _format.createParser(ctxt));
}
return (MonthDay) ctxt.handleUnexpectedToken(handledType(), p.getCurrentToken(), p,
return (MonthDay) ctxt.handleUnexpectedToken(handledType(), p.currentToken(), p,
"expected JSON String");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ public JodaDateDeserializerBase<?> withFormat(JacksonJodaDateFormat format) {
@Override
public YearMonth deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException
{
JsonToken t = p.getCurrentToken();
if (t == JsonToken.VALUE_STRING) {
if (p.hasToken(JsonToken.VALUE_STRING)) {
String str = p.getText().trim();
if (str.isEmpty()) {
return null;
return getNullValue(ctxt);
}
return YearMonth.parse(str, _format.createParser(ctxt));
}
return (YearMonth) ctxt.handleUnexpectedToken(handledType(), p.getCurrentToken(), p,
return (YearMonth) ctxt.handleUnexpectedToken(handledType(), p.currentToken(), p,
"expected JSON String");
}
}

0 comments on commit e03a06f

Please sign in to comment.