@@ -25,6 +25,13 @@ package at.syntaxerror.json5
2525
2626import kotlinx.serialization.json.JsonArray
2727import kotlinx.serialization.json.JsonObject
28+ import kotlinx.serialization.json.JsonPrimitive
29+ import kotlinx.serialization.json.booleanOrNull
30+ import kotlinx.serialization.json.contentOrNull
31+ import kotlinx.serialization.json.doubleOrNull
32+ import kotlinx.serialization.json.floatOrNull
33+ import kotlinx.serialization.json.intOrNull
34+ import kotlinx.serialization.json.longOrNull
2835
2936/* *
3037 * A utility class for serializing [JSONObjects][DecodeJson5Object] and [JSONArrays][DecodeJson5Array]
@@ -36,9 +43,10 @@ class JSONStringify(
3643 private val options : JSONOptions
3744) {
3845
39- private val quoteToken = if (options.quoteSingle) ' \' ' else ' "'
40- private val emptyString = " $quoteToken$quoteToken "
41- private val indentFactor = options.indentFactor
46+ private val quoteToken: Char get() = if (options.quoteSingle) ' \' ' else ' "'
47+ private val emptyString: String get() = " $quoteToken$quoteToken "
48+ private val indentFactor: UInt get() = options.indentFactor
49+ private val isIndented: Boolean get() = indentFactor > 0u
4250
4351 /* *
4452 * Converts a JSONObject into its string representation. The indentation factor enables
@@ -47,7 +55,8 @@ class JSONStringify(
4755 * characters.
4856 *
4957 * `indentFactor = 2`:
50- * ```
58+ *
59+ * ```json
5160 * {
5261 * "key0": "value0",
5362 * "key1": {
@@ -59,7 +68,7 @@ class JSONStringify(
5968 *
6069 * `indentFactor = 0`:
6170 *
62- * ```
71+ * ```json
6372 * {"key0":"value0","key1":{"nested":123},"key2":false}
6473 * ```
6574 */
@@ -68,7 +77,6 @@ class JSONStringify(
6877 indent : String = "",
6978 ): String {
7079 val childIndent = indent + " " .repeat(indentFactor.toInt())
71- val isIndented = indentFactor > 0u
7280
7381 val sb = StringBuilder ()
7482 sb.append(' {' )
@@ -99,7 +107,8 @@ class JSONStringify(
99107 *
100108 *
101109 * `indentFactor = 2`:
102- * ```
110+ *
111+ * ```json
103112 * [
104113 * "value",
105114 * {
@@ -110,7 +119,8 @@ class JSONStringify(
110119 * ```
111120 *
112121 * `indentFactor = 0`:
113- * ```
122+ *
123+ * ```json
114124 * ["value",{"nested":123},false]
115125 * ```
116126 */
@@ -119,7 +129,6 @@ class JSONStringify(
119129 indent : String = "",
120130 ): String {
121131 val childIndent = indent + " " .repeat(indentFactor.toInt())
122- val isIndented = indentFactor > 0u
123132
124133 val sb = StringBuilder ()
125134 sb.append(' [' )
@@ -144,18 +153,21 @@ class JSONStringify(
144153 indent : String ,
145154 ): String {
146155 return when (value) {
147- null -> " null"
148- is JsonObject -> encodeObject(value, indent)
149- is JsonArray -> encodeArray(value, indent)
150- is String -> escapeString(value)
151- is Double -> {
156+ null -> " null"
157+
158+ is JsonObject -> encodeObject(value, indent)
159+ is JsonArray -> encodeArray(value, indent)
160+ is JsonPrimitive -> encode(value.anyOrNull, indent)
161+
162+ is String -> escapeString(value)
163+ is Double -> {
152164 when {
153165 ! options.allowNaN && value.isNaN() -> throw JSONException (" Illegal NaN in JSON" )
154166 ! options.allowInfinity && value.isInfinite() -> throw JSONException (" Illegal Infinity in JSON" )
155167 else -> value.toString()
156168 }
157169 }
158- else -> value.toString()
170+ else -> value.toString()
159171 }
160172 }
161173
@@ -172,6 +184,7 @@ class JSONStringify(
172184 ) { c: Char ->
173185
174186 val formattedChar: String? = when (c) {
187+ in listOf (' \' ' , ' \" ' ) -> if (c == quoteToken) " \\ $quoteToken " else " $c "
175188 quoteToken -> " \\ $quoteToken "
176189 in Json5EscapeSequence .escapableChars -> Json5EscapeSequence .asEscapedString(c)
177190 else -> when (c.category) {
@@ -181,12 +194,22 @@ class JSONStringify(
181194 CharCategory .CONTROL ,
182195 CharCategory .PRIVATE_USE ,
183196 CharCategory .SURROGATE ,
184- CharCategory .UNASSIGNED -> String .format(" \\ u%04X" , c)
197+ CharCategory .UNASSIGNED -> String .format(" \\ u%04X" , c.code )
185198 else -> null
186199 }
187200 }
188201 formattedChar ? : c.toString()
189202 }
190203 }
191204 }
205+
206+ companion object {
207+ private val JsonPrimitive .anyOrNull: Any?
208+ get() = intOrNull
209+ ? : longOrNull
210+ ? : doubleOrNull
211+ ? : floatOrNull
212+ ? : booleanOrNull
213+ ? : contentOrNull
214+ }
192215}
0 commit comments