diff --git a/ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/TimestampSerializer.java b/ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/TimestampSerializer.java index 4fbd1aa07..a90ec490f 100644 --- a/ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/TimestampSerializer.java +++ b/ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/TimestampSerializer.java @@ -35,8 +35,12 @@ protected TimestampSerializer() { } @Override - public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider provider) throws IOException { - IonGenerator joiGenerator = (IonGenerator) jgen; - joiGenerator.writeValue(value); + public void serialize(Timestamp value, JsonGenerator g, SerializerProvider provider) throws IOException { + if (g instanceof IonGenerator) { + ((IonGenerator) g).writeValue(value); + } else { + // Otherwise probably `TokenBuffer`, so + g.writeEmbeddedObject(value); + } } } diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/IonTimestampDeser251Test.java similarity index 50% rename from ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java rename to ion/src/test/java/com/fasterxml/jackson/dataformat/ion/IonTimestampDeser251Test.java index a06fab028..06b9578da 100644 --- a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/IonTimestampDeser251Test.java @@ -1,7 +1,8 @@ -package com.fasterxml.jackson.dataformat.ion.failing; +package com.fasterxml.jackson.dataformat.ion; import org.junit.Test; +import com.amazon.ion.Timestamp; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -15,12 +16,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; @@ -38,10 +57,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`