Skip to content

Commit

Permalink
Second part of fix for #117
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jul 15, 2020
1 parent e03a06f commit f974e6c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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.DeserializationContext;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -45,7 +47,6 @@ public JodaDateDeserializerBase<?> withFormat(JacksonJodaDateFormat format) {
public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException
{

// We'll accept either long (timestamp) or array:
if (p.isExpectedStartArrayToken()) {
p.nextToken(); // VALUE_NUMBER_INT
Expand All @@ -55,7 +56,7 @@ public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
p.nextToken(); // VALUE_NUMBER_INT
int day = p.getIntValue();
if (p.nextToken() != JsonToken.END_ARRAY) {
throw ctxt.wrongTokenException(p, JsonToken.END_ARRAY,
throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY,
"after DateMidnight ints");
}
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
Expand All @@ -70,6 +71,12 @@ public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
if (str.length() == 0) { // [JACKSON-360]
return null;
}
// 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 new DateMidnight(NumberInput.parseLong(str));
}
LocalDate local = _format.createParser(ctxt).parseLocalDate(str);
if (local == null) {
return null;
Expand All @@ -80,4 +87,9 @@ public DateMidnight deserialize(JsonParser p, DeserializationContext ctxt)
throw ctxt.wrongTokenException(p, handledType(), JsonToken.START_ARRAY,
"expected JSON Array, Number or String");
}
}

protected DateMidnight _fromTimestamp(DeserializationContext ctxt, long ts) {
return new DateMidnight(ts);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.joda.time.Instant;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -44,6 +44,12 @@ public Instant deserialize(JsonParser p, DeserializationContext ctxt) throws IOE
if (str.length() == 0) {
return getNullValue(ctxt);
}
// 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 new Instant(NumberInput.parseLong(str));
}
// 11-Sep-2018, tatu: `DateTimeDeserializer` allows timezone inclusion in brackets;
// should that be checked here too?
return Instant.parse(str, _format.createParser(ctxt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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.DeserializationContext;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -37,13 +38,18 @@ public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws I
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
String str = p.getText().trim();
return (str.length() == 0) ? null
: _format.createParser(ctxt).parseLocalDate(str);
case JsonTokenId.ID_NUMBER_INT:
{
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new LocalDate(p.getLongValue(), tz);
if (str.length() == 0) {
return getNullValue(ctxt);
}
// 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));
}
return _format.createParser(ctxt).parseLocalDate(str);
case JsonTokenId.ID_NUMBER_INT:
return _fromTimestamp(ctxt, p.getLongValue());
case JsonTokenId.ID_START_ARRAY:
// [yyyy,mm,dd] or ["yyyy","mm","dd"]
int year = p.nextIntValue(-1); // fast speculative case
Expand All @@ -63,7 +69,13 @@ public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws I
}
return new LocalDate(year, month, day);
}
return (LocalDate) ctxt.handleUnexpectedToken(handledType(), p.getCurrentToken(), p,
return (LocalDate) ctxt.handleUnexpectedToken(handledType(), p.currentToken(), p,
"expected String, Number or JSON Array");
}

protected LocalDate _fromTimestamp(DeserializationContext ctxt, long ts) {
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone()
: DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new LocalDate(ts, tz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.joda.time.LocalDateTime;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -37,14 +37,19 @@ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
case JsonTokenId.ID_STRING:
{
String str = p.getText().trim();
return (str.length() == 0) ? null
: _format.createParser(ctxt).parseLocalDateTime(str);
if (str.length() == 0) {
return getNullValue(ctxt);
}
// 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));
}
return _format.createParser(ctxt).parseLocalDateTime(str);
}
case JsonTokenId.ID_NUMBER_INT:
{
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new LocalDateTime(p.getLongValue(), tz);
}
return _fromTimestamp(ctxt, p.getLongValue());
case JsonTokenId.ID_START_ARRAY:
// [yyyy,mm,dd,hh,MM,ss,ms]
JsonToken t = p.nextToken();
Expand Down Expand Up @@ -82,7 +87,13 @@ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt)
throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "after LocalDateTime ints");
default:
}
return (LocalDateTime) ctxt.handleUnexpectedToken(handledType(), p.getCurrentToken(), p,
return (LocalDateTime) ctxt.handleUnexpectedToken(handledType(), p.currentToken(), p,
"expected String, Number or JSON Array");
}

protected LocalDateTime _fromTimestamp(DeserializationContext ctxt, long ts) {
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone()
: DateTimeZone.forTimeZone(ctxt.getTimeZone());
return new LocalDateTime(ts, tz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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.DeserializationContext;
import com.fasterxml.jackson.datatype.joda.cfg.FormatConfig;
import com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat;
Expand Down Expand Up @@ -41,6 +43,12 @@ public LocalTime deserialize(JsonParser p, DeserializationContext ctxt)
if (str.length() == 0) {
return getNullValue(ctxt);
}
// 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 new LocalTime(NumberInput.parseLong(str));
}
return _format.createParser(ctxt).parseLocalTime(str);
default:
}
Expand Down

0 comments on commit f974e6c

Please sign in to comment.