Skip to content

Commit 39992d5

Browse files
authored
Fixes #251: problem with buffering of Ion Timestamp value during deserialization (#479)
1 parent 3753cd7 commit 39992d5

File tree

4 files changed

+87
-50
lines changed

4 files changed

+87
-50
lines changed

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/ionvalue/TimestampSerializer.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ protected TimestampSerializer() {
3535
}
3636

3737
@Override
38-
public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
39-
IonGenerator joiGenerator = (IonGenerator) jgen;
40-
joiGenerator.writeValue(value);
38+
public void serialize(Timestamp value, JsonGenerator g, SerializerProvider provider) throws IOException {
39+
if (g instanceof IonGenerator) {
40+
((IonGenerator) g).writeValue(value);
41+
} else {
42+
// Otherwise probably `TokenBuffer`, so
43+
g.writeEmbeddedObject(value);
44+
}
4145
}
4246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.fasterxml.jackson.dataformat.ion;
2+
3+
import org.junit.Test;
4+
5+
import com.amazon.ion.Timestamp;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
import com.fasterxml.jackson.databind.DeserializationFeature;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
import com.fasterxml.jackson.dataformat.ion.ionvalue.IonValueModule;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
17+
// For [dataformats-binary#251]
18+
public class IonTimestampDeser251Test
19+
{
20+
static class MessageWithoutTimestamp {
21+
final String message;
22+
final Integer count;
23+
24+
@JsonCreator
25+
public MessageWithoutTimestamp(@JsonProperty("message") String message,
26+
@JsonProperty("count") Integer count) {
27+
this.message = message;
28+
this.count = count;
29+
}
30+
31+
public String getMessage() {
32+
return message;
33+
}
34+
}
35+
36+
static class MessageWithTimestamp {
37+
final String message;
38+
final Integer count;
39+
40+
public Timestamp timestamp;
41+
42+
@JsonCreator
43+
public MessageWithTimestamp(@JsonProperty("message") String message,
44+
@JsonProperty("count") Integer count) {
45+
this.message = message;
46+
this.count = count;
47+
}
48+
49+
public String getMessage() {
50+
return message;
51+
}
52+
}
53+
54+
private final ObjectMapper ION_MAPPER = IonObjectMapper.builder()
55+
.addModule(new IonValueModule())
56+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
57+
.build();
58+
59+
// [dataformats-binary#251]
60+
@Test
61+
public void testDeserWithIgnoredBufferedTimestamp() throws Exception {
62+
String ion = "{message: \"Hello, world\", timestamp:2021-03-10T01:49:30.242-00:00}";
63+
MessageWithoutTimestamp message = ION_MAPPER.readValue(ion,
64+
MessageWithoutTimestamp.class);
65+
assertNotNull(message);
66+
assertEquals("Hello, world", message.message);
67+
}
68+
69+
@Test
70+
public void testDeserWithBufferedTimestamp() throws Exception {
71+
String ion = "{message: \"Hello, world\", timestamp:2021-03-10T01:49:30.242-00:00}";
72+
MessageWithTimestamp message = ION_MAPPER.readValue(ion,
73+
MessageWithTimestamp.class);
74+
assertNotNull(message);
75+
assertEquals("Hello, world", message.message);
76+
assertNotNull(message.timestamp);
77+
}
78+
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonTimestampDeser251Test.java

-47
This file was deleted.

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Active maintainers:
1616

1717
2.17.0 (not yet released)
1818

19+
#251 (ion) Unable to deserialize Object with unknown `Timestamp` field
20+
(reported by @mgoertzen)
1921
#316 (cbor) Uncaught exception in
2022
`com.fasterxml.jackson.dataformat.cbor.CBORParser._finishShortText`
2123
#392: (cbor, smile) Support `StreamReadConstraints.maxDocumentLength`

0 commit comments

Comments
 (0)