1212
1313package org .eclipse .yasson .serializers ;
1414
15+ import java .io .StringReader ;
1516import java .util .List ;
1617import java .util .Map ;
1718import java .util .Optional ;
19+ import java .util .function .Supplier ;
1820
21+ import jakarta .json .Json ;
22+ import jakarta .json .JsonArray ;
23+ import jakarta .json .JsonObject ;
24+ import jakarta .json .JsonReader ;
1925import jakarta .json .bind .Jsonb ;
2026import jakarta .json .bind .JsonbBuilder ;
27+ import jakarta .json .bind .JsonbException ;
2128import jakarta .json .bind .annotation .JsonbTypeSerializer ;
2229import jakarta .json .bind .serializer .JsonbSerializer ;
2330import jakarta .json .bind .serializer .SerializationContext ;
@@ -66,35 +73,43 @@ public void serialize(final TestInterface obj, final JsonGenerator generator, fi
6673
6774 // Container classes for testing
6875 public static class MapContainer {
69- public Map <String , TestInterface > map ;
76+ public final Map <String , TestInterface > map ;
77+ public final Map <?, ?> questionKeyMap ;
78+ public final Map <String , ?> questionValueMap ;
7079
71- public MapContainer (Map <String , TestInterface > map ) {
80+ public MapContainer (final Map <String , TestInterface > map , final Map <?, ?> questionKeyMap , final Map < String , ?> questionValueMap ) {
7281 this .map = map ;
82+ this .questionKeyMap = questionKeyMap ;
83+ this .questionValueMap = questionValueMap ;
7384 }
7485 }
7586
7687 public static class ListContainer {
77- public List <TestInterface > list ;
88+ public final List <TestInterface > list ;
89+ public final List <?> questionList ;
7890
79- public ListContainer (List <TestInterface > list ) {
91+ public ListContainer (final List <TestInterface > list , final List <?> questionList ) {
8092 this .list = list ;
93+ this .questionList = questionList ;
8194 }
8295 }
8396
8497 public static class ArrayContainer {
85- public TestInterface [] array ;
98+ public final TestInterface [] array ;
8699
87100 public ArrayContainer (TestInterface [] array ) {
88101 this .array = array ;
89102 }
90103 }
91104
105+ @ SuppressWarnings ("OptionalUsedAsFieldOrParameterType" )
92106 public static class OptionalContainer {
93- @ SuppressWarnings ( "OptionalUsedAsFieldOrParameterType" )
94- public Optional <TestInterface > optional ;
107+ public final Optional < TestInterface > optional ;
108+ public final Optional <?> questionOptional ;
95109
96- public OptionalContainer (Optional <TestInterface > optional ) {
110+ public OptionalContainer (final Optional <TestInterface > optional , final Optional <?> questionOptional ) {
97111 this .optional = optional ;
112+ this .questionOptional = questionOptional ;
98113 }
99114 }
100115
@@ -118,31 +133,55 @@ public void testTypeSerializerOnMapValues() {
118133 final MapContainer container = new MapContainer (Map .of (
119134 "key1" , new TestImpl ("value1" ),
120135 "key2" , new TestImpl ("value2" )
121- ));
136+ ), Map .of ("qKey1" , "value1" , "qKey2" , "value2" ),
137+ Map .of ("key1" , "qValue1" , "key2" , "qValue2" )
138+ );
139+
140+ final JsonObject json = toJsonObject (container );
141+ final JsonObject map = json .getJsonObject ("map" );
142+ final JsonObject questionKeyMap = json .getJsonObject ("questionKeyMap" );
143+ final JsonObject questionValueMap = json .getJsonObject ("questionValueMap" );
144+
145+ Supplier <String > errorMessage = () -> String .format ("Expected value not found in %s" , map );
146+ Assertions .assertEquals ("SERIALIZED:value1" , map .getString ("key1" ), errorMessage );
147+ Assertions .assertEquals ("SERIALIZED:value2" , map .getString ("key2" ), errorMessage );
122148
123- final String json = jsonb .toJson (container );
124149
125- Assertions .assertTrue (json .contains ("\" key1\" :\" SERIALIZED:value1\" " ),
126- "Expected serialized value1 but got: " + json );
127- Assertions .assertTrue (json .contains ("\" key2\" :\" SERIALIZED:value2\" " ),
128- "Expected serialized value2 but got: " + json );
150+ errorMessage = () -> String .format ("Expected value not found in %s" , questionKeyMap );
151+ Assertions .assertEquals ("value1" , questionKeyMap .getString ("qKey1" ), errorMessage );
152+ Assertions .assertEquals ("value2" , questionKeyMap .getString ("qKey2" ), errorMessage );
153+
154+
155+ errorMessage = () -> String .format ("Expected value not found in %s" , questionValueMap );
156+ Assertions .assertEquals ("qValue1" , questionValueMap .getString ("key1" ), errorMessage );
157+ Assertions .assertEquals ("qValue2" , questionValueMap .getString ("key2" ), errorMessage );
129158 }
130159
131160 @ Test
132161 public void testTypeSerializerOnListElements () {
133162 final ListContainer container = new ListContainer (List .of (
134163 new TestImpl ("value1" ),
135164 new TestImpl ("value2" )
136- ));
165+ ), List . of ( "qValue1" , "qValue2" ) );
137166
138- final String json = jsonb .toJson (container );
167+ final JsonObject json = toJsonObject (container );
168+ final JsonArray list = json .getJsonArray ("list" );
169+ final JsonArray questionList = json .getJsonArray ("questionList" );
139170
140- Assertions .assertEquals ("{\" list\" :[\" SERIALIZED:value1\" ,\" SERIALIZED:value2\" ]}" , json );
171+ Supplier <String > errorMessage = () -> String .format ("Expected value not found in %s" , list );
172+ Assertions .assertEquals (2 , list .size (), () -> String .format ("Expected a size of 2 in %s" , list ));
173+ Assertions .assertEquals ("SERIALIZED:value1" , list .getString (0 ), errorMessage );
174+ Assertions .assertEquals ("SERIALIZED:value2" , list .getString (1 ), errorMessage );
175+
176+ errorMessage = () -> String .format ("Expected value not found in %s" , questionList );
177+ Assertions .assertEquals (2 , questionList .size (), () -> String .format ("Expected a size of 2 in %s" , questionList ));
178+ Assertions .assertEquals ("qValue1" , questionList .getString (0 ), errorMessage );
179+ Assertions .assertEquals ("qValue2" , questionList .getString (1 ), errorMessage );
141180 }
142181
143182 @ Test
144183 public void testTypeSerializerOnArrayElements () {
145- final ArrayContainer container = new ArrayContainer (new TestInterface []{
184+ final ArrayContainer container = new ArrayContainer (new TestInterface [] {
146185 new TestImpl ("value1" ),
147186 new TestImpl ("value2" )
148187 });
@@ -154,10 +193,21 @@ public void testTypeSerializerOnArrayElements() {
154193
155194 @ Test
156195 public void testTypeSerializerOnOptionalValue () {
157- final OptionalContainer container = new OptionalContainer (Optional .of (new TestImpl ("value1" )));
196+ final OptionalContainer container = new OptionalContainer (Optional .of (new TestImpl ("value1" )), Optional . of ( "value2" ) );
158197
159- final String json = jsonb . toJson (container );
198+ final JsonObject json = toJsonObject (container );
160199
161- Assertions .assertEquals ("{\" optional\" :\" SERIALIZED:value1\" }" , json );
200+ Assertions .assertEquals ("SERIALIZED:value1" , json .getString ("optional" ));
201+ Assertions .assertEquals ("value2" , json .getString ("questionOptional" ));
202+ }
203+
204+ private JsonObject toJsonObject (final Object object ) throws JsonbException {
205+ final String value = jsonb .toJson (object );
206+ try (
207+ StringReader reader = new StringReader (value );
208+ JsonReader jsonReader = Json .createReader (reader )
209+ ) {
210+ return jsonReader .readObject ();
211+ }
162212 }
163213}
0 commit comments