@@ -62,7 +62,7 @@ You can generate a personal access token at [GitHub > Settings > Developer setti
6262``` kotlin
6363import dev.hossain.json5kt.JSON5
6464
65- // Parse JSON5 to Kotlin objects
65+ // Parse JSON5 to strongly-typed JSON5Value objects
6666val json5 = """
6767{
6868 // Configuration for my app
@@ -73,7 +73,20 @@ val json5 = """
7373"""
7474
7575val parsed = JSON5 .parse(json5)
76- // Returns: Map<String, Any?>
76+ // Returns: JSON5Value.Object
77+
78+ // Access values in a type-safe way
79+ when (parsed) {
80+ is JSON5Value .Object -> {
81+ val name = parsed.value[" name" ] as ? JSON5Value .String
82+ val version = parsed.value[" version" ] as ? JSON5Value .Number .Integer
83+ val features = parsed.value[" features" ] as ? JSON5Value .Array
84+
85+ println (" App name: ${name?.value} " ) // "MyApp"
86+ println (" Version: ${version?.value} " ) // 2
87+ println (" Features: ${features?.value?.map { (it as JSON5Value .String ).value }} " ) // ["auth", "analytics"]
88+ }
89+ }
7790
7891// Stringify Kotlin objects to JSON5
7992val data = mapOf (
@@ -85,6 +98,47 @@ val json5String = JSON5.stringify(data)
8598// Returns: {name:'MyApp',version:2,enabled:true}
8699```
87100
101+ ### Migration from parseToAny (Deprecated)
102+
103+ If you were previously using the deprecated ` parseToAny ` method, here's how to migrate:
104+
105+ ``` kotlin
106+ // Old API (deprecated and removed)
107+ // val result = JSON5.parseToAny("""{"key": "value"}""")
108+ // val map = result as Map<String, Any?>
109+
110+ // New API - Type-safe approach (recommended)
111+ val result = JSON5 .parse(""" {"key": "value"}""" )
112+ when (result) {
113+ is JSON5Value .Object -> {
114+ val key = result.value[" key" ] as ? JSON5Value .String
115+ println (key?.value) // "value"
116+ }
117+ }
118+
119+ // Alternative: Convert to raw objects when needed
120+ fun JSON5Value.toRawObject (): Any? {
121+ return when (this ) {
122+ is JSON5Value .Null -> null
123+ is JSON5Value .Boolean -> this .value
124+ is JSON5Value .String -> this .value
125+ is JSON5Value .Number .Integer -> this .value.toDouble()
126+ is JSON5Value .Number .Decimal -> this .value
127+ is JSON5Value .Number .Hexadecimal -> this .value.toDouble()
128+ is JSON5Value .Number .PositiveInfinity -> Double .POSITIVE_INFINITY
129+ is JSON5Value .Number .NegativeInfinity -> Double .NEGATIVE_INFINITY
130+ is JSON5Value .Number .NaN -> Double .NaN
131+ is JSON5Value .Object -> this .value.mapValues { it.value.toRawObject() }
132+ is JSON5Value .Array -> this .value.map { it.toRawObject() }
133+ }
134+ }
135+
136+ // Using the helper for compatibility
137+ val rawResult = JSON5 .parse(""" {"key": "value"}""" ).toRawObject()
138+ val map = rawResult as Map <String , Any ?>
139+ println (map[" key" ]) // "value"
140+ ```
141+
88142### Integration with kotlinx.serialization
89143
90144``` kotlin
@@ -148,6 +202,19 @@ val numbers = JSON5.parse("""
148202}
149203""" )
150204
205+ // Access different number types
206+ when (numbers) {
207+ is JSON5Value .Object -> {
208+ val hex = numbers.value[" hex" ] as ? JSON5Value .Number .Hexadecimal
209+ val infinity = numbers.value[" infinity" ] as ? JSON5Value .Number .PositiveInfinity
210+ val nan = numbers.value[" notANumber" ] as ? JSON5Value .Number .NaN
211+
212+ println (" Hex value: ${hex?.value} " ) // 912559
213+ println (" Is infinity: ${infinity != null } " ) // true
214+ println (" Is NaN: ${nan != null } " ) // true
215+ }
216+ }
217+
151218// Multi-line strings and comments
152219val complex = JSON5 .parse("""
153220{
@@ -159,6 +226,17 @@ multi-line string",
159226 unquoted: 'keys work too'
160227}
161228""" )
229+
230+ // Working with the parsed result
231+ when (complex) {
232+ is JSON5Value .Object -> {
233+ val multiLine = complex.value[" multiLine" ] as ? JSON5Value .String
234+ val singleQuoted = complex.value[" singleQuoted" ] as ? JSON5Value .String
235+
236+ println (" Multi-line: ${multiLine?.value} " )
237+ println (" Single quoted: ${singleQuoted?.value} " )
238+ }
239+ }
162240```
163241
164242## Building the Project
0 commit comments