@@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
10
10
fun main () {
11
11
// Test serialization and deserialization of Employee model
12
12
testEmployeeSerialization()
13
-
13
+
14
14
// Run README sample code validation tests
15
15
println (" \n === README Sample Code Validation ===" )
16
16
testBasicParsingAndStringifying()
@@ -23,13 +23,14 @@ fun main() {
23
23
}
24
24
25
25
private fun testSampleJson5Files () {
26
- val json5files = listOf (
27
- " simple-object.json5" ,
28
- " array-example.json5" ,
29
- " numeric-formats.json5" ,
30
- " string-and-identifiers.json5" ,
31
- " root-string.json5"
32
- )
26
+ val json5files =
27
+ listOf (
28
+ " simple-object.json5" ,
29
+ " array-example.json5" ,
30
+ " numeric-formats.json5" ,
31
+ " string-and-identifiers.json5" ,
32
+ " root-string.json5" ,
33
+ )
33
34
34
35
json5files.forEach { fileName ->
35
36
println (" \n === Processing file: $fileName ===" )
@@ -53,7 +54,6 @@ private fun testSampleJson5Files() {
53
54
}
54
55
}
55
56
56
-
57
57
fun testEmployeeSerialization () {
58
58
val fileName = " employee-example.json5"
59
59
val resourceStream = object {}.javaClass.getResourceAsStream(" /$fileName " )
@@ -80,17 +80,18 @@ fun testEmployeeSerialization() {
80
80
*/
81
81
fun testBasicParsingAndStringifying () {
82
82
println (" \n --- Testing Basic Parsing and Stringifying ---" )
83
-
83
+
84
84
try {
85
85
// Parse JSON5 to strongly-typed JSON5Value objects
86
- val json5 = """
87
- {
88
- // Configuration for my app
89
- name: 'MyApp',
90
- version: 2,
91
- features: ['auth', 'analytics',], // trailing comma
92
- }
93
- """ .trimIndent()
86
+ val json5 =
87
+ """
88
+ {
89
+ // Configuration for my app
90
+ name: 'MyApp',
91
+ version: 2,
92
+ features: ['auth', 'analytics',], // trailing comma
93
+ }
94
+ """ .trimIndent()
94
95
95
96
val parsed = JSON5 .parse(json5)
96
97
println (" ✓ Parsed JSON5 successfully: $parsed " )
@@ -101,23 +102,25 @@ fun testBasicParsingAndStringifying() {
101
102
val name = parsed.value[" name" ] as ? JSON5Value .String
102
103
val version = parsed.value[" version" ] as ? JSON5Value .Number
103
104
val features = parsed.value[" features" ] as ? JSON5Value .Array
104
-
105
+
105
106
println (" ✓ App name: ${name?.value} " ) // "MyApp"
106
- println (" ✓ Version: ${(version as ? JSON5Value .Number .Integer )?.value ? : (version as ? JSON5Value .Number .Decimal )?.value?.toInt()} " ) // 2
107
+ println (
108
+ " ✓ Version: ${(version as ? JSON5Value .Number .Integer )?.value ? : (version as ? JSON5Value .Number .Decimal )?.value?.toInt()} " ,
109
+ ) // 2
107
110
println (" ✓ Features: ${features?.value?.map { (it as JSON5Value .String ).value }} " ) // ["auth", "analytics"]
108
111
}
109
112
else -> println (" Parsed value is not an object" )
110
113
}
111
114
112
115
// Stringify Kotlin objects to JSON5
113
- val data = mapOf (
114
- " name" to " MyApp" ,
115
- " version" to 2 ,
116
- " enabled" to true
117
- )
116
+ val data =
117
+ mapOf (
118
+ " name" to " MyApp" ,
119
+ " version" to 2 ,
120
+ " enabled" to true ,
121
+ )
118
122
val json5String = JSON5 .stringify(data)
119
123
println (" ✓ Stringified to JSON5: $json5String " )
120
-
121
124
} catch (e: Exception ) {
122
125
println (" ⚠️ Error in basic parsing and stringifying test: ${e.message} " )
123
126
e.printStackTrace()
@@ -129,47 +132,48 @@ fun testBasicParsingAndStringifying() {
129
132
*/
130
133
fun testKotlinxSerializationIntegration () {
131
134
println (" \n --- Testing kotlinx.serialization Integration ---" )
132
-
135
+
133
136
try {
134
137
@Serializable
135
138
data class Config (
136
139
val appName : String ,
137
140
val version : Int ,
138
141
val features : List <String >,
139
- val settings : Map <String , String >
142
+ val settings : Map <String , String >,
140
143
)
141
144
142
145
// Serialize to JSON5
143
- val config = Config (
144
- appName = " MyApp" ,
145
- version = 2 ,
146
- features = listOf (" auth" , " analytics" ),
147
- settings = mapOf (" theme" to " dark" , " lang" to " en" )
148
- )
146
+ val config =
147
+ Config (
148
+ appName = " MyApp" ,
149
+ version = 2 ,
150
+ features = listOf (" auth" , " analytics" ),
151
+ settings = mapOf (" theme" to " dark" , " lang" to " en" ),
152
+ )
149
153
150
154
val json5 = JSON5 .encodeToString(Config .serializer(), config)
151
155
println (" ✓ Serialized to JSON5: $json5 " )
152
156
153
157
// Deserialize from JSON5 (with comments and formatting)
154
- val json5WithComments = """
155
- {
156
- // Application configuration
157
- appName: 'MyApp',
158
- version: 2, // current version
159
- features: [
160
- 'auth',
161
- 'analytics', // trailing comma OK
162
- ],
163
- settings: {
164
- theme: 'dark',
165
- lang: 'en',
158
+ val json5WithComments =
159
+ """
160
+ {
161
+ // Application configuration
162
+ appName: 'MyApp',
163
+ version: 2, // current version
164
+ features: [
165
+ 'auth',
166
+ 'analytics', // trailing comma OK
167
+ ],
168
+ settings: {
169
+ theme: 'dark',
170
+ lang: 'en',
171
+ }
166
172
}
167
- }
168
- """ .trimIndent()
173
+ """ .trimIndent()
169
174
170
175
val decoded = JSON5 .decodeFromString(Config .serializer(), json5WithComments)
171
176
println (" ✓ Deserialized from JSON5 with comments: $decoded " )
172
-
173
177
} catch (e: Exception ) {
174
178
println (" ⚠️ Error in kotlinx.serialization integration test: ${e.message} " )
175
179
e.printStackTrace()
@@ -181,21 +185,24 @@ fun testKotlinxSerializationIntegration() {
181
185
*/
182
186
fun testAdvancedFeatures () {
183
187
println (" \n --- Testing Advanced Features ---" )
184
-
188
+
185
189
try {
186
190
// JSON5 supports various number formats
187
- val numbers = JSON5 .parse("""
188
- {
189
- hex: 0xDECAF,
190
- leadingDot: .8675309,
191
- trailingDot: 8675309.,
192
- positiveSign: +1,
193
- scientific: 6.02e23,
194
- infinity: Infinity,
195
- negativeInfinity: -Infinity,
196
- notANumber: NaN
197
- }
198
- """ .trimIndent())
191
+ val numbers =
192
+ JSON5 .parse(
193
+ """
194
+ {
195
+ hex: 0xDECAF,
196
+ leadingDot: .8675309,
197
+ trailingDot: 8675309.,
198
+ positiveSign: +1,
199
+ scientific: 6.02e23,
200
+ infinity: Infinity,
201
+ negativeInfinity: -Infinity,
202
+ notANumber: NaN
203
+ }
204
+ """ .trimIndent(),
205
+ )
199
206
200
207
println (" ✓ Parsed numbers JSON5 successfully: $numbers " )
201
208
@@ -205,25 +212,30 @@ fun testAdvancedFeatures() {
205
212
val hex = numbers.value[" hex" ] as ? JSON5Value .Number
206
213
val infinity = numbers.value[" infinity" ] as ? JSON5Value .Number .PositiveInfinity
207
214
val nan = numbers.value[" notANumber" ] as ? JSON5Value .Number .NaN
208
-
209
- println (" ✓ Hex value: ${(hex as ? JSON5Value .Number .Hexadecimal )?.value ? : (hex as ? JSON5Value .Number .Decimal )?.value?.toLong()} " ) // 912559
215
+
216
+ println (
217
+ " ✓ Hex value: ${(hex as ? JSON5Value .Number .Hexadecimal )?.value ? : (hex as ? JSON5Value .Number .Decimal )?.value?.toLong()} " ,
218
+ ) // 912559
210
219
println (" ✓ Is infinity: ${infinity != null } " ) // true
211
220
println (" ✓ Is NaN: ${nan != null } " ) // true
212
221
}
213
222
else -> println (" Numbers value is not an object" )
214
223
}
215
224
216
225
// Multi-line strings and comments
217
- val complex = JSON5 .parse("""
218
- {
219
- multiLine: "This is a \
220
- multi-line string",
221
- /* Block comment
222
- spanning multiple lines */
223
- singleQuoted: 'Can contain "double quotes"',
224
- unquoted: 'keys work too'
225
- }
226
- """ .trimIndent())
226
+ val complex =
227
+ JSON5 .parse(
228
+ """
229
+ {
230
+ multiLine: "This is a \
231
+ multi-line string",
232
+ /* Block comment
233
+ spanning multiple lines */
234
+ singleQuoted: 'Can contain "double quotes"',
235
+ unquoted: 'keys work too'
236
+ }
237
+ """ .trimIndent(),
238
+ )
227
239
228
240
println (" ✓ Parsed complex JSON5 successfully: $complex " )
229
241
@@ -232,13 +244,12 @@ fun testAdvancedFeatures() {
232
244
is JSON5Value .Object -> {
233
245
val multiLine = complex.value[" multiLine" ] as ? JSON5Value .String
234
246
val singleQuoted = complex.value[" singleQuoted" ] as ? JSON5Value .String
235
-
247
+
236
248
println (" ✓ Multi-line: ${multiLine?.value} " )
237
249
println (" ✓ Single quoted: ${singleQuoted?.value} " )
238
250
}
239
251
else -> println (" Complex value is not an object" )
240
252
}
241
-
242
253
} catch (e: Exception ) {
243
254
println (" ⚠️ Error in advanced features test: ${e.message} " )
244
255
e.printStackTrace()
@@ -250,7 +261,7 @@ fun testAdvancedFeatures() {
250
261
*/
251
262
fun testMigrationCompatibility () {
252
263
println (" \n --- Testing Migration Compatibility ---" )
253
-
264
+
254
265
try {
255
266
// New API - Type-safe approach (recommended)
256
267
val result = JSON5 .parse(""" {"key": "value"}""" )
@@ -263,8 +274,8 @@ fun testMigrationCompatibility() {
263
274
}
264
275
265
276
// Alternative: Convert to raw objects when needed
266
- fun JSON5Value.toRawObject (): Any? {
267
- return when (this ) {
277
+ fun JSON5Value.toRawObject (): Any? =
278
+ when (this ) {
268
279
is JSON5Value .Null -> null
269
280
is JSON5Value .Boolean -> this .value
270
281
is JSON5Value .String -> this .value
@@ -277,15 +288,13 @@ fun testMigrationCompatibility() {
277
288
is JSON5Value .Object -> this .value.mapValues { it.value.toRawObject() }
278
289
is JSON5Value .Array -> this .value.map { it.toRawObject() }
279
290
}
280
- }
281
291
282
292
// Using the helper for compatibility
283
293
val rawResult = JSON5 .parse(""" {"key": "value"}""" ).toRawObject()
284
294
val map = rawResult as Map <String , Any ?>
285
295
println (" ✓ Compatibility helper result: ${map[" key" ]} " ) // "value"
286
-
287
296
} catch (e: Exception ) {
288
297
println (" ⚠️ Error in migration compatibility test: ${e.message} " )
289
298
e.printStackTrace()
290
299
}
291
- }
300
+ }
0 commit comments