1+ package dev.hossain.json5kt
2+
3+ /* *
4+ * Represents a JSON5 value as described in the JSON5 specification.
5+ * JSON5 values can be objects, arrays, strings, numbers, booleans, or null.
6+ *
7+ * @see <a href="https://spec.json5.org/">JSON5 Specification</a>
8+ */
9+ sealed class JSON5Value {
10+
11+ /* *
12+ * Represents a JSON5 object, which is a collection of name/value pairs.
13+ *
14+ * @property value The map of property names to JSON5 values
15+ * @see <a href="https://spec.json5.org/#objects">JSON5 Objects</a>
16+ */
17+ data class Object (val value : Map <kotlin.String , JSON5Value >) : JSON5Value() {
18+ override fun toString (): kotlin.String = value.toString()
19+ }
20+
21+ /* *
22+ * Represents a JSON5 array, which is an ordered sequence of JSON5 values.
23+ *
24+ * @property value The list of JSON5 values
25+ * @see <a href="https://spec.json5.org/#arrays">JSON5 Arrays</a>
26+ */
27+ data class Array (val value : List <JSON5Value >) : JSON5Value() {
28+ override fun toString (): kotlin.String = value.toString()
29+ }
30+
31+ /* *
32+ * Represents a JSON5 string value.
33+ *
34+ * @property value The string value
35+ * @see <a href="https://spec.json5.org/#strings">JSON5 Strings</a>
36+ */
37+ data class String (val value : kotlin.String ) : JSON5Value() {
38+ override fun toString (): kotlin.String = " \" $value \" "
39+ }
40+
41+ /* *
42+ * Represents a JSON5 number value, which can be an integer, float, hex, infinity, or NaN.
43+ *
44+ * @see <a href="https://spec.json5.org/#numbers">JSON5 Numbers</a>
45+ */
46+ sealed class Number : JSON5Value () {
47+ /* *
48+ * Represents an integer number in JSON5.
49+ *
50+ * @property value The integer value
51+ */
52+ data class Integer (val value : Long ) : Number() {
53+ override fun toString (): kotlin.String = value.toString()
54+ }
55+
56+ /* *
57+ * Represents a decimal number in JSON5.
58+ *
59+ * @property value The decimal value
60+ */
61+ data class Decimal (val value : Double ) : Number() {
62+ override fun toString (): kotlin.String = value.toString()
63+ }
64+
65+ /* *
66+ * Represents a hexadecimal number in JSON5 (0x prefix).
67+ *
68+ * @property value The integer value represented by the hex
69+ */
70+ data class Hexadecimal (val value : Long ) : Number() {
71+ override fun toString (): kotlin.String = " 0x${value.toString(16 )} "
72+ }
73+
74+ /* *
75+ * Represents positive infinity in JSON5.
76+ */
77+ object PositiveInfinity : Number() {
78+ override fun toString (): kotlin.String = " Infinity"
79+ }
80+
81+ /* *
82+ * Represents negative infinity in JSON5.
83+ */
84+ object NegativeInfinity : Number() {
85+ override fun toString (): kotlin.String = " -Infinity"
86+ }
87+
88+ /* *
89+ * Represents NaN (Not a Number) in JSON5.
90+ */
91+ object NaN : Number() {
92+ override fun toString (): kotlin.String = " NaN"
93+ }
94+ }
95+
96+ /* *
97+ * Represents a JSON5 boolean value.
98+ *
99+ * @property value The boolean value
100+ * @see <a href="https://spec.json5.org/#primitives">JSON5 Primitives</a>
101+ */
102+ data class Boolean (val value : kotlin.Boolean ) : JSON5Value() {
103+ override fun toString (): kotlin.String = value.toString()
104+ }
105+
106+ /* *
107+ * Represents a JSON5 null value.
108+ *
109+ * @see <a href="https://spec.json5.org/#primitives">JSON5 Primitives</a>
110+ */
111+ object Null : JSON5Value() {
112+ override fun toString (): kotlin.String = " null"
113+ }
114+
115+ /* *
116+ * Helper methods to convert primitive Kotlin types to JSON5Value objects.
117+ */
118+ companion object {
119+ /* *
120+ * Creates a JSON5Value from a Kotlin object.
121+ *
122+ * @param value The Kotlin object to convert
123+ * @return The corresponding JSON5Value
124+ * @throws IllegalArgumentException if the value type is not supported
125+ */
126+ fun from (value : Any? ): JSON5Value {
127+ return when (value) {
128+ null -> Null
129+ is Map <* , * > -> {
130+ val jsonMap = mutableMapOf< kotlin.String , JSON5Value > ()
131+ value.forEach { (k, v) ->
132+ if (k is kotlin.String ) {
133+ jsonMap[k] = from(v)
134+ }
135+ }
136+ Object (jsonMap)
137+ }
138+ is List <* > -> {
139+ val jsonList = value.map { from(it) }
140+ Array (jsonList)
141+ }
142+ is kotlin.String -> String (value)
143+ is Int -> Number .Integer (value.toLong())
144+ is Long -> Number .Integer (value)
145+ is Float -> Number .Decimal (value.toDouble())
146+ is Double -> {
147+ when {
148+ value.isNaN() -> Number .NaN
149+ value.isInfinite() && value > 0 -> Number .PositiveInfinity
150+ value.isInfinite() && value < 0 -> Number .NegativeInfinity
151+ else -> Number .Decimal (value)
152+ }
153+ }
154+ is kotlin.Boolean -> Boolean (value)
155+ else -> throw IllegalArgumentException (" Unsupported type: ${value::class .java} " )
156+ }
157+ }
158+ }
159+ }
0 commit comments