1+ package jdk .sandbox .java .util .json ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .math .BigDecimal ;
6+ import java .math .BigInteger ;
7+ import java .util .List ;
8+ import java .util .Map ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
12+
13+ public class JsonTypedUntypedTests {
14+
15+ @ Test
16+ void testFromUntypedWithSimpleTypes () {
17+ // Test string
18+ JsonValue jsonString = Json .fromUntyped ("hello" );
19+ assertThat (jsonString ).isInstanceOf (JsonString .class );
20+ assertThat (((JsonString ) jsonString ).value ()).isEqualTo ("hello" );
21+
22+ // Test integer
23+ JsonValue jsonInt = Json .fromUntyped (42 );
24+ assertThat (jsonInt ).isInstanceOf (JsonNumber .class );
25+ assertThat (((JsonNumber ) jsonInt ).toNumber ()).isEqualTo (42 );
26+
27+ // Test long
28+ JsonValue jsonLong = Json .fromUntyped (42L );
29+ assertThat (jsonLong ).isInstanceOf (JsonNumber .class );
30+ assertThat (((JsonNumber ) jsonLong ).toNumber ()).isEqualTo (42L );
31+
32+ // Test double
33+ JsonValue jsonDouble = Json .fromUntyped (3.14 );
34+ assertThat (jsonDouble ).isInstanceOf (JsonNumber .class );
35+ assertThat (((JsonNumber ) jsonDouble ).toNumber ()).isEqualTo (3.14 );
36+
37+ // Test boolean
38+ JsonValue jsonBool = Json .fromUntyped (true );
39+ assertThat (jsonBool ).isInstanceOf (JsonBoolean .class );
40+ assertThat (((JsonBoolean ) jsonBool ).value ()).isTrue ();
41+
42+ // Test null
43+ JsonValue jsonNull = Json .fromUntyped (null );
44+ assertThat (jsonNull ).isInstanceOf (JsonNull .class );
45+ }
46+
47+ @ Test
48+ void testFromUntypedWithBigNumbers () {
49+ // Test BigInteger
50+ BigInteger bigInt = new BigInteger ("123456789012345678901234567890" );
51+ JsonValue jsonBigInt = Json .fromUntyped (bigInt );
52+ assertThat (jsonBigInt ).isInstanceOf (JsonNumber .class );
53+ assertThat (((JsonNumber ) jsonBigInt ).toNumber ()).isEqualTo (bigInt );
54+
55+ // Test BigDecimal
56+ BigDecimal bigDec = new BigDecimal ("123456789012345678901234567890.123456789" );
57+ JsonValue jsonBigDec = Json .fromUntyped (bigDec );
58+ assertThat (jsonBigDec ).isInstanceOf (JsonNumber .class );
59+ assertThat (((JsonNumber ) jsonBigDec ).toNumber ()).isEqualTo (bigDec );
60+ }
61+
62+ @ Test
63+ void testFromUntypedWithCollections () {
64+ // Test List
65+ List <Object > list = List .of ("item1" , 42 , true );
66+ JsonValue jsonArray = Json .fromUntyped (list );
67+ assertThat (jsonArray ).isInstanceOf (JsonArray .class );
68+ JsonArray array = (JsonArray ) jsonArray ;
69+ assertThat (array .values ()).hasSize (3 );
70+ assertThat (((JsonString ) array .values ().get (0 )).value ()).isEqualTo ("item1" );
71+ assertThat (((JsonNumber ) array .values ().get (1 )).toNumber ()).isEqualTo (42 );
72+ assertThat (((JsonBoolean ) array .values ().get (2 )).value ()).isTrue ();
73+
74+ // Test Map
75+ Map <String , Object > map = Map .of ("name" , "John" , "age" , 30 , "active" , true );
76+ JsonValue jsonObject = Json .fromUntyped (map );
77+ assertThat (jsonObject ).isInstanceOf (JsonObject .class );
78+ JsonObject obj = (JsonObject ) jsonObject ;
79+ assertThat (((JsonString ) obj .members ().get ("name" )).value ()).isEqualTo ("John" );
80+ assertThat (((JsonNumber ) obj .members ().get ("age" )).toNumber ()).isEqualTo (30 );
81+ assertThat (((JsonBoolean ) obj .members ().get ("active" )).value ()).isTrue ();
82+ }
83+
84+ @ Test
85+ void testFromUntypedWithNestedStructures () {
86+ Map <String , Object > nested = Map .of (
87+ "user" , Map .of ("name" , "John" , "age" , 30 ),
88+ "scores" , List .of (85 , 92 , 78 ),
89+ "active" , true
90+ );
91+
92+ JsonValue json = Json .fromUntyped (nested );
93+ assertThat (json ).isInstanceOf (JsonObject .class );
94+
95+ JsonObject root = (JsonObject ) json ;
96+ JsonObject user = (JsonObject ) root .members ().get ("user" );
97+ assertThat (((JsonString ) user .members ().get ("name" )).value ()).isEqualTo ("John" );
98+
99+ JsonArray scores = (JsonArray ) root .members ().get ("scores" );
100+ assertThat (scores .values ()).hasSize (3 );
101+ assertThat (((JsonNumber ) scores .values ().get (0 )).toNumber ()).isEqualTo (85 );
102+ }
103+
104+ @ Test
105+ void testFromUntypedWithJsonValue () {
106+ // If input is already a JsonValue, return as-is
107+ JsonString original = JsonString .of ("test" );
108+ JsonValue result = Json .fromUntyped (original );
109+ assertThat (result ).isSameAs (original );
110+ }
111+
112+ @ Test
113+ void testFromUntypedWithInvalidTypes () {
114+ // Test with unsupported type
115+ assertThatThrownBy (() -> Json .fromUntyped (new StringBuilder ("test" )))
116+ .isInstanceOf (IllegalArgumentException .class )
117+ .hasMessageContaining ("StringBuilder is not a recognized type" );
118+ }
119+
120+ @ Test
121+ void testFromUntypedWithNonStringMapKey () {
122+ // Test map with non-string key
123+ Map <Object , Object > invalidMap = Map .of (123 , "value" );
124+ assertThatThrownBy (() -> Json .fromUntyped (invalidMap ))
125+ .isInstanceOf (IllegalArgumentException .class )
126+ .hasMessageContaining ("The key '123' is not a String" );
127+ }
128+
129+ @ Test
130+ void testToUntypedWithSimpleTypes () {
131+ // Test string
132+ Object str = Json .toUntyped (JsonString .of ("hello" ));
133+ assertThat (str ).isEqualTo ("hello" );
134+
135+ // Test number
136+ Object num = Json .toUntyped (JsonNumber .of (42 ));
137+ assertThat (num ).isEqualTo (42L );
138+
139+ // Test boolean
140+ Object bool = Json .toUntyped (JsonBoolean .of (true ));
141+ assertThat (bool ).isEqualTo (true );
142+
143+ // Test null
144+ Object nullVal = Json .toUntyped (JsonNull .of ());
145+ assertThat (nullVal ).isNull ();
146+ }
147+
148+ @ Test
149+ void testToUntypedWithCollections () {
150+ // Test array
151+ JsonArray array = JsonArray .of (List .of (
152+ JsonString .of ("item1" ),
153+ JsonNumber .of (42 ),
154+ JsonBoolean .of (true )
155+ ));
156+ Object result = Json .toUntyped (array );
157+ assertThat (result ).isInstanceOf (List .class );
158+ List <?> list = (List <?>) result ;
159+ assertThat (list ).containsExactly ("item1" , 42L , true );
160+
161+ // Test object
162+ JsonObject obj = JsonObject .of (Map .of (
163+ "name" , JsonString .of ("John" ),
164+ "age" , JsonNumber .of (30 ),
165+ "active" , JsonBoolean .of (true )
166+ ));
167+ Object objResult = Json .toUntyped (obj );
168+ assertThat (objResult ).isInstanceOf (Map .class );
169+ Map <?, ?> map = (Map <?, ?>) objResult ;
170+ assertThat (map .get ("name" )).isEqualTo ("John" );
171+ assertThat (map .get ("age" )).isEqualTo (30L );
172+ assertThat (map .get ("active" )).isEqualTo (true );
173+ }
174+
175+ @ Test
176+ void testRoundTripConversion () {
177+ // Create complex nested structure
178+ Map <String , Object > original = Map .of (
179+ "user" , Map .of (
180+ "name" , "John Doe" ,
181+ "age" , 30 ,
182+ "email" , "john@example.com"
183+ ),
184+ "scores" , List .of (85.5 , 92.0 , 78.3 ),
185+ "active" , true ,
186+ "metadata" , Map .of (
187+ "created" , "2024-01-01" ,
188+ "tags" , List .of ("vip" , "premium" )
189+ )
190+ );
191+
192+ // Convert to JsonValue and back
193+ JsonValue json = Json .fromUntyped (original );
194+ Object reconstructed = Json .toUntyped (json );
195+
196+ // Verify structure is preserved
197+ assertThat (reconstructed ).isInstanceOf (Map .class );
198+ Map <?, ?> resultMap = (Map <?, ?>) reconstructed ;
199+
200+ Map <?, ?> user = (Map <?, ?>) resultMap .get ("user" );
201+ assertThat (user .get ("name" )).isEqualTo ("John Doe" );
202+ assertThat (user .get ("age" )).isEqualTo (30L );
203+
204+ List <?> scores = (List <?>) resultMap .get ("scores" );
205+ assertThat (scores ).containsExactly (85.5 , 92.0 , 78.3 );
206+
207+ Map <?, ?> metadata = (Map <?, ?>) resultMap .get ("metadata" );
208+ List <?> tags = (List <?>) metadata .get ("tags" );
209+ assertThat (tags ).containsExactly ("vip" , "premium" );
210+ }
211+
212+ @ Test
213+ void testToUntypedPreservesOrder () {
214+ // JsonObject should preserve insertion order
215+ JsonObject obj = JsonObject .of (Map .of (
216+ "z" , JsonString .of ("last" ),
217+ "a" , JsonString .of ("first" ),
218+ "m" , JsonString .of ("middle" )
219+ ));
220+
221+ Object result = Json .toUntyped (obj );
222+ assertThat (result ).isInstanceOf (Map .class );
223+
224+ // The order might not be preserved with Map.of(), so let's just verify contents
225+ Map <?, ?> map = (Map <?, ?>) result ;
226+ assertThat (map ).containsEntry ("z" , "last" )
227+ .containsEntry ("a" , "first" )
228+ .containsEntry ("m" , "middle" );
229+ }
230+ }
0 commit comments