66
77import com .fasterxml .jackson .annotation .JsonTypeInfo ;
88
9+ import tools .jackson .core .JacksonException ;
10+ import tools .jackson .core .JsonGenerator ;
911import tools .jackson .core .JsonParser ;
1012
1113import tools .jackson .databind .*;
1214import tools .jackson .databind .annotation .JsonDeserialize ;
15+ import tools .jackson .databind .annotation .JsonSerialize ;
1316import tools .jackson .databind .testutil .DatabindTestUtil ;
1417
1518import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -48,14 +51,15 @@ public Value1654UntypedContainer(Value1654... v) {
4851 }
4952 }
5053
51- static class Value1654UsingDeserializerUntypedContainer {
54+ static class Value1654UsingCustomSerDeserUntypedContainer {
5255 @ JsonDeserialize (contentUsing = Value1654Deserializer .class )
56+ @ JsonSerialize (contentUsing = Value1654Serializer .class )
5357 @ JsonTypeInfo (use = JsonTypeInfo .Id .NONE )
5458 public List <Value1654 > values ;
5559
56- protected Value1654UsingDeserializerUntypedContainer () { }
60+ protected Value1654UsingCustomSerDeserUntypedContainer () { }
5761
58- public Value1654UsingDeserializerUntypedContainer (Value1654 ... v ) {
62+ public Value1654UsingCustomSerDeserUntypedContainer (Value1654 ... v ) {
5963 values = Arrays .asList (v );
6064 }
6165 }
@@ -71,6 +75,17 @@ public Value1654 deserialize(JsonParser p, DeserializationContext ctxt) {
7175 }
7276 }
7377
78+
79+ static class Value1654Serializer extends ValueSerializer <Value1654 > {
80+ @ Override
81+ public void serialize (Value1654 value , JsonGenerator gen , SerializationContext ctxt )
82+ throws JacksonException {
83+ gen .writeStartObject (value );
84+ gen .writeNumberProperty ("v" , value .x );
85+ gen .writeEndObject ();
86+ }
87+ }
88+
7489 private final ObjectMapper MAPPER = newJsonMapper ();
7590
7691 // [databind#1654]: no override, default polymorphic type id
@@ -90,9 +105,9 @@ void withoutNoTypeElementOverrideSerAndDeser() throws Exception {
90105 assertEquals (2 , result .values .get (1 ).x );
91106 }
92107
93- // [databind#1654]: override, no polymorphic type id
108+ // [databind#1654]: override, no polymorphic type id, serialization
94109 @ Test
95- void withNoTypeInfoOverrideSer () throws Exception {
110+ void withNoTypeInfoDefaultSer () throws Exception {
96111 Value1654UntypedContainer cont = new Value1654UntypedContainer (
97112 new Value1654 (3 ),
98113 new Value1654 (7 )
@@ -101,28 +116,41 @@ void withNoTypeInfoOverrideSer() throws Exception {
101116 MAPPER .writeValueAsString (cont ));
102117 }
103118
104- // [databind#1654]
119+ // [databind#1654]: override, no polymorphic type id, deserialization
105120 @ Test
106- void withNoTypeInfoDeser () throws Exception {
107- // and then actual failing case
121+ void withNoTypeInfoDefaultDeser () throws Exception {
108122 final String noTypeJson = a2q (
109123 "{'values':[{'x':3},{'x':7}]}"
110124 );
111- Value1654UntypedContainer unResult = MAPPER .readValue (noTypeJson , Value1654UntypedContainer .class );
125+ Value1654UntypedContainer unResult = MAPPER .readValue (noTypeJson ,
126+ Value1654UntypedContainer .class );
112127 assertEquals (2 , unResult .values .size ());
113128 assertEquals (7 , unResult .values .get (1 ).x );
114129 }
115130
116- // [databind#1654]
131+ // [databind#1654]: override, no polymorphic type id, custom serialization
132+ @ Test
133+ void withNoTypeInfoOverrideSer () throws Exception {
134+ Value1654UntypedContainer cont = new Value1654UntypedContainer (
135+ new Value1654 (1 ),
136+ new Value1654 (2 )
137+ );
138+ assertEquals (a2q ("{'values':[{'v':1},{'v':2}]}" ),
139+ MAPPER .writeValueAsString (cont ));
140+ }
141+
142+ // [databind#1654]: override, no polymorphic type id, custom deserialization
117143 @ Test
118144 void withNoTypeInfoOverrideDeser () throws Exception {
119145 // and then actual failing case
120146 final String noTypeJson = a2q (
121147 "{'values':[{'v':3},{'v':7}]}"
122148 );
123- Value1654UsingDeserializerUntypedContainer unResult = MAPPER .readValue (noTypeJson , Value1654UsingDeserializerUntypedContainer .class );
149+ Value1654UsingCustomSerDeserUntypedContainer unResult = MAPPER .readValue (noTypeJson ,
150+ Value1654UsingCustomSerDeserUntypedContainer .class );
124151 assertEquals (2 , unResult .values .size ());
125152 assertEquals (3 , unResult .values .get (0 ).x );
126153 assertEquals (7 , unResult .values .get (1 ).x );
127154 }
155+
128156}
0 commit comments