1+ package com.powersync.utils
2+
3+
4+ import kotlinx.serialization.json.*
5+ import kotlin.test.*
6+
7+ class JsonTest {
8+ @Test
9+ fun testNumberToJsonElement () {
10+ val number = JsonParam .Number (42 )
11+ val jsonElement = number.toJsonElement()
12+ assertTrue(jsonElement is JsonPrimitive )
13+ assertEquals(42 , jsonElement.int)
14+ }
15+
16+ @Test
17+ fun testStringToJsonElement () {
18+ val string = JsonParam .String (" test" )
19+ val jsonElement = string.toJsonElement()
20+ assertTrue(jsonElement is JsonPrimitive )
21+ assertEquals(" test" , jsonElement.content)
22+ }
23+
24+ @Test
25+ fun testBooleanToJsonElement () {
26+ val boolean = JsonParam .Boolean (true )
27+ val jsonElement = boolean.toJsonElement()
28+ assertTrue(jsonElement is JsonPrimitive )
29+ assertTrue(jsonElement.boolean)
30+ }
31+
32+ @Test
33+ fun testMapToJsonElement () {
34+ val map = JsonParam .Map (mapOf (
35+ " key1" to JsonParam .String (" value1" ),
36+ " key2" to JsonParam .Number (42 )
37+ ))
38+ val jsonElement = map.toJsonElement()
39+ assertTrue(jsonElement is JsonObject )
40+ assertEquals(" value1" , jsonElement[" key1" ]?.jsonPrimitive?.content)
41+ assertEquals(42 , jsonElement[" key2" ]?.jsonPrimitive?.int)
42+ }
43+
44+ @Test
45+ fun testListToJsonElement () {
46+ val list = JsonParam .Collection (listOf (
47+ JsonParam .String (" item1" ),
48+ JsonParam .Number (42 )
49+ ))
50+ val jsonElement = list.toJsonElement()
51+ assertTrue(jsonElement is JsonArray )
52+ assertEquals(" item1" , jsonElement[0 ].jsonPrimitive.content)
53+ assertEquals(42 , jsonElement[1 ].jsonPrimitive.int)
54+ }
55+
56+ @Test
57+ fun testJsonElementParamToJsonElement () {
58+ val originalJson = buildJsonObject {
59+ put(" key" , " value" )
60+ }
61+ val jsonElementParam = JsonParam .JsonElement (originalJson)
62+ val jsonElement = jsonElementParam.toJsonElement()
63+ assertEquals(originalJson, jsonElement)
64+ }
65+
66+ @Test
67+ fun testNullToJsonElement () {
68+ val nullParam = JsonParam .Null
69+ val jsonElement = nullParam.toJsonElement()
70+ assertTrue(jsonElement is JsonNull )
71+ }
72+
73+ @Test
74+ fun testMapToJsonObject () {
75+ val params = mapOf (
76+ " string" to JsonParam .String (" value" ),
77+ " number" to JsonParam .Number (42 ),
78+ " boolean" to JsonParam .Boolean (true ),
79+ " null" to JsonParam .Null
80+ )
81+ val jsonObject = params.toJsonObject()
82+ assertEquals(" value" , jsonObject[" string" ]?.jsonPrimitive?.content)
83+ assertEquals(42 , jsonObject[" number" ]?.jsonPrimitive?.int)
84+ assertEquals(true , jsonObject[" boolean" ]?.jsonPrimitive?.boolean)
85+ assertTrue(jsonObject[" null" ] is JsonNull )
86+ }
87+
88+ @Test
89+ fun testComplexNestedMapToJsonObject () {
90+ val complexNestedMap = mapOf (
91+ " string" to JsonParam .String (" value" ),
92+ " number" to JsonParam .Number (42 ),
93+ " boolean" to JsonParam .Boolean (true ),
94+ " null" to JsonParam .Null ,
95+ " nestedMap" to JsonParam .Map (mapOf (
96+ " list" to JsonParam .Collection (listOf (
97+ JsonParam .Number (1 ),
98+ JsonParam .String (" two" ),
99+ JsonParam .Boolean (false )
100+ )),
101+ " deeplyNested" to JsonParam .Map (mapOf (
102+ " jsonElement" to JsonParam .JsonElement (buildJsonObject {
103+ put(" key" , " value" )
104+ put(" array" , buildJsonArray {
105+ add(1 )
106+ add(" string" )
107+ add(true )
108+ })
109+ }),
110+ " mixedList" to JsonParam .Collection (arrayListOf (
111+ JsonParam .Number (3.14 ),
112+ JsonParam .Map (mapOf (
113+ " key" to JsonParam .String (" nestedValue" )
114+ )),
115+ JsonParam .Null
116+ )
117+ )
118+ ))
119+ ))
120+ )
121+
122+ val jsonObject = complexNestedMap.toJsonObject()
123+
124+ // Verify top-level elements
125+ assertEquals(" value" , jsonObject[" string" ]?.jsonPrimitive?.content)
126+ assertEquals(42 , jsonObject[" number" ]?.jsonPrimitive?.int)
127+ assertEquals(true , jsonObject[" boolean" ]?.jsonPrimitive?.boolean)
128+ assertTrue(jsonObject[" null" ] is JsonNull )
129+
130+ // Verify nested map
131+ val nestedMap = jsonObject[" nestedMap" ]?.jsonObject
132+ assertNotNull(nestedMap)
133+
134+ // Verify nested list
135+ val nestedList = nestedMap[" list" ]?.jsonArray
136+ assertNotNull(nestedList)
137+ assertEquals(3 , nestedList.size)
138+ assertEquals(1 , nestedList[0 ].jsonPrimitive.int)
139+ assertEquals(" two" , nestedList[1 ].jsonPrimitive.content)
140+ assertEquals(false , nestedList[2 ].jsonPrimitive.boolean)
141+
142+ // Verify deeply nested map
143+ val deeplyNested = nestedMap[" deeplyNested" ]?.jsonObject
144+ assertNotNull(deeplyNested)
145+
146+ // Verify JsonElement
147+ val jsonElement = deeplyNested[" jsonElement" ]?.jsonObject
148+ assertNotNull(jsonElement)
149+ assertEquals(" value" , jsonElement[" key" ]?.jsonPrimitive?.content)
150+ val jsonElementArray = jsonElement[" array" ]?.jsonArray
151+ assertNotNull(jsonElementArray)
152+ assertEquals(3 , jsonElementArray.size)
153+ assertEquals(1 , jsonElementArray[0 ].jsonPrimitive.int)
154+ assertEquals(" string" , jsonElementArray[1 ].jsonPrimitive.content)
155+ assertEquals(true , jsonElementArray[2 ].jsonPrimitive.boolean)
156+
157+ // Verify mixed list
158+ val mixedList = deeplyNested[" mixedList" ]?.jsonArray
159+ assertNotNull(mixedList)
160+ assertEquals(3 , mixedList.size)
161+ assertEquals(3.14 , mixedList[0 ].jsonPrimitive.double)
162+ val nestedMapInList = mixedList[1 ].jsonObject
163+ assertNotNull(nestedMapInList)
164+ assertEquals(" nestedValue" , nestedMapInList[" key" ]?.jsonPrimitive?.content)
165+ assertTrue(mixedList[2 ] is JsonNull )
166+ }
167+ }
0 commit comments