diff --git a/ion/src/main/java/tools/jackson/dataformat/ion/ionvalue/TimestampSerializer.java b/ion/src/main/java/tools/jackson/dataformat/ion/ionvalue/TimestampSerializer.java index fe1aa4edb..5072efb65 100644 --- a/ion/src/main/java/tools/jackson/dataformat/ion/ionvalue/TimestampSerializer.java +++ b/ion/src/main/java/tools/jackson/dataformat/ion/ionvalue/TimestampSerializer.java @@ -31,9 +31,12 @@ protected TimestampSerializer() { } @Override - public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider provider) - { - IonGenerator joiGenerator = (IonGenerator) jgen; - joiGenerator.writeValue(value); + public void serialize(Timestamp value, JsonGenerator g, SerializerProvider provider) { + if (g instanceof IonGenerator) { + ((IonGenerator) g).writeValue(value); + } else { + // Otherwise probably `TokenBuffer`, so + g.writeEmbeddedObject(value); + } } } diff --git a/ion/src/test/java/tools/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java b/ion/src/test/java/tools/jackson/dataformat/ion/IonTimestampDeser251Test.java similarity index 50% rename from ion/src/test/java/tools/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java rename to ion/src/test/java/tools/jackson/dataformat/ion/IonTimestampDeser251Test.java index aa4df4d97..452521180 100644 --- a/ion/src/test/java/tools/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java +++ b/ion/src/test/java/tools/jackson/dataformat/ion/IonTimestampDeser251Test.java @@ -1,7 +1,8 @@ -package tools.jackson.dataformat.ion.failing; +package tools.jackson.dataformat.ion; import org.junit.Test; +import com.amazon.ion.Timestamp; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; @@ -17,12 +18,30 @@ // For [dataformats-binary#251] public class IonTimestampDeser251Test { - static class Message { + static class MessageWithoutTimestamp { final String message; final Integer count; @JsonCreator - public Message(@JsonProperty("message") String message, + public MessageWithoutTimestamp(@JsonProperty("message") String message, + @JsonProperty("count") Integer count) { + this.message = message; + this.count = count; + } + + public String getMessage() { + return message; + } + } + + static class MessageWithTimestamp { + final String message; + final Integer count; + + public Timestamp timestamp; + + @JsonCreator + public MessageWithTimestamp(@JsonProperty("message") String message, @JsonProperty("count") Integer count) { this.message = message; this.count = count; @@ -40,10 +59,21 @@ public String getMessage() { // [dataformats-binary#251] @Test - public void testTimestampDeserWithBuffering() throws Exception { + public void testDeserWithIgnoredBufferedTimestamp() throws Exception { + String ion = "{message: \"Hello, world\", timestamp:2021-03-10T01:49:30.242-00:00}"; + MessageWithoutTimestamp message = ION_MAPPER.readValue(ion, + MessageWithoutTimestamp.class); + assertNotNull(message); + assertEquals("Hello, world", message.message); + } + + @Test + public void testDeserWithBufferedTimestamp() throws Exception { String ion = "{message: \"Hello, world\", timestamp:2021-03-10T01:49:30.242-00:00}"; - Message message = ION_MAPPER.readValue(ion, Message.class); + MessageWithTimestamp message = ION_MAPPER.readValue(ion, + MessageWithTimestamp.class); assertNotNull(message); assertEquals("Hello, world", message.message); + assertNotNull(message.timestamp); } } diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index fb2b57f8d..41472b95a 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -16,6 +16,8 @@ Active maintainers: 2.17.0 (not yet released) +#251 (ion) Unable to deserialize Object with unknown `Timestamp` field + (reported by @mgoertzen) #316 (cbor) Uncaught exception in `com.fasterxml.jackson.dataformat.cbor.CBORParser._finishShortText` #392: (cbor, smile) Support `StreamReadConstraints.maxDocumentLength`