Skip to content

Commit 7d4943c

Browse files
authored
Merge pull request #41 from hossain-khan/copilot/fix-40
Remove deprecated parseToAny API and migrate to type-safe parse() method
2 parents e0a7c2f + 50a477f commit 7d4943c

File tree

6 files changed

+252
-164
lines changed

6 files changed

+252
-164
lines changed

README.md

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You can generate a personal access token at [GitHub > Settings > Developer setti
6262
```kotlin
6363
import dev.hossain.json5kt.JSON5
6464

65-
// Parse JSON5 to Kotlin objects
65+
// Parse JSON5 to strongly-typed JSON5Value objects
6666
val json5 = """
6767
{
6868
// Configuration for my app
@@ -73,7 +73,20 @@ val json5 = """
7373
"""
7474

7575
val 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
7992
val 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
152219
val 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

lib/src/main/kotlin/dev/hossain/json5kt/JSON5.kt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ object JSON5 {
2525
return JSON5Value.from(result)
2626
}
2727

28-
/**
29-
* Parses a JSON5 string into a raw Kotlin object without type conversion.
30-
*
31-
* @param text JSON5 text to parse
32-
* @return The parsed value (Map, List, String, Number, Boolean, or null)
33-
* @throws JSON5Exception if the input is invalid JSON5
34-
*/
35-
fun parseToAny(text: String): Any? {
36-
return JSON5Parser.parse(text)
37-
}
38-
3928
/**
4029
* Parses a JSON5 string into a strongly-typed JSON5Value, with a reviver function.
4130
*
@@ -49,18 +38,6 @@ object JSON5 {
4938
return JSON5Value.from(result)
5039
}
5140

52-
/**
53-
* Parses a JSON5 string into a raw Kotlin object without type conversion, with a reviver function.
54-
*
55-
* @param text JSON5 text to parse
56-
* @param reviver A function that transforms the parsed values
57-
* @return The parsed value (Map, List, String, Number, Boolean, or null)
58-
* @throws JSON5Exception if the input is invalid JSON5
59-
*/
60-
fun parseToAny(text: String, reviver: (key: String, value: Any?) -> Any?): Any? {
61-
return JSON5Parser.parse(text, reviver)
62-
}
63-
6441
/**
6542
* Serializes a Kotlin object to a JSON5 string.
6643
*

0 commit comments

Comments
 (0)