Skip to content

Commit 4e1905b

Browse files
committed
Added toJsonString and appendToJsonString
1 parent 3154322 commit 4e1905b

25 files changed

+100
-98
lines changed

src/iris/json/IrisJson.kt

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/iris/json/JsonItem.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ interface JsonItem {
1717
operator fun set(key: String, value: Any?): JsonItem
1818

1919
fun obj(): Any?
20-
fun <A: Appendable>joinTo(buffer: A): A
20+
fun <A: Appendable>appendToJsonString(buffer: A): A
21+
fun toJsonString(): String
2122

2223
fun iterable() : Iterable<JsonItem>
2324

@@ -68,6 +69,8 @@ interface JsonItem {
6869
fun find(tree: String): JsonItem
6970
}
7071

72+
inline fun<T : JsonItem> JsonItem.cast(): T = this as T
73+
7174
inline fun <reified T>JsonItem.asObject(): T {
7275
val deserializer = DeserializerFactory.getDeserializer(typeOf<T>())
7376
return deserializer.deserialize(this)

src/iris/json/Util.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ package iris.json
66
*/
77
object Util {
88

9+
enum class Type {
10+
Object
11+
, Array
12+
, String
13+
, Value
14+
, Null
15+
}
16+
17+
enum class ValueType {
18+
Integer,
19+
Float,
20+
Constant // among them: true, false, null
21+
}
22+
23+
924
private val digitRange = '0'..'9'
1025
private val alphaRange = 'a'..'z'
1126
private val alphaUpperRange = 'A'..'Z'

src/iris/json/flow/FlowArray.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FlowArray(private val parser: JsonFlowParser) : FlowItem(parser.tokener),
1111

1212
private val items = mutableListOf<JsonItem>()
1313

14-
override fun <A : Appendable> joinTo(buffer: A): A {
14+
override fun <A : Appendable> appendToJsonString(buffer: A): A {
1515
buffer.append('[')
1616
items.joinTo(buffer)
1717
buffer.append(']')

src/iris/json/flow/FlowItem.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ import iris.json.plain.IrisJsonItem
99
abstract class FlowItem(protected val tokener: Tokener) : IrisJsonItem() {
1010
abstract fun parse()
1111
fun contentSource(start: Int = 0 , end: Int = 0) = tokener.getSourceSequence(start, end)
12+
13+
override fun toJsonString(): String {
14+
return appendToJsonString(StringBuilder()).toString()
15+
}
1216
}

src/iris/json/flow/FlowObject.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class FlowObject(private val parser: JsonFlowParser) : FlowItem(parser.tokener),
122122
}
123123
}
124124

125-
override fun <A : Appendable> joinTo(buffer: A): A {
125+
override fun <A : Appendable> appendToJsonString(buffer: A): A {
126126
parse()
127127
buffer.append("{")
128128
var firstDone = false
@@ -134,7 +134,7 @@ class FlowObject(private val parser: JsonFlowParser) : FlowItem(parser.tokener),
134134
buffer.append("\"")
135135
buffer.append(entry.first)
136136
buffer.append("\": ")
137-
entry.second.joinTo(buffer)
137+
entry.second.appendToJsonString(buffer)
138138

139139
}
140140
buffer.append('}')

src/iris/json/flow/FlowString.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FlowString(tokener: Tokener, val quote: Char) : FlowItem(tokener), JsonStr
1313

1414
private var data: CharSequence? = null
1515

16-
override fun <A : Appendable> joinTo(buffer: A): A {
16+
override fun <A : Appendable> appendToJsonString(buffer: A): A {
1717
parse()
1818
buffer.append('"')
1919
(data as? IrisSequence)?.joinTo(buffer) ?: buffer.append(data)

src/iris/json/flow/FlowValue.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package iris.json.flow
22

3-
import iris.json.IrisJson
43
import iris.json.JsonValue
4+
import iris.json.Util.ValueType
55
import iris.json.plain.IrisJsonItem
66
import iris.json.plain.IrisJsonNull
77
import iris.sequence.*
@@ -14,7 +14,7 @@ class FlowValue(tokener: Tokener) : FlowItem(tokener), JsonValue {
1414

1515
private var data: Tokener.PrimitiveData? = null
1616

17-
override fun <A : Appendable> joinTo(buffer: A): A {
17+
override fun <A : Appendable> appendToJsonString(buffer: A): A {
1818
parse()
1919
buffer.append(data!!.sequence)
2020
return buffer
@@ -33,14 +33,14 @@ class FlowValue(tokener: Tokener) : FlowItem(tokener), JsonValue {
3333
val data = data!!
3434
val s = data.sequence
3535
return when (data.type) {
36-
IrisJson.ValueType.Constant -> when (s) {
37-
"null" -> null
38-
"true", "1" -> true
39-
"false", "0" -> false
40-
else -> s.toString()
41-
}
42-
IrisJson.ValueType.Integer -> s.toLong()
43-
IrisJson.ValueType.Float -> s.toDouble()
36+
ValueType.Constant -> when (s) {
37+
"null" -> null
38+
"true", "1" -> true
39+
"false", "0" -> false
40+
else -> s.toString()
41+
}
42+
ValueType.Integer -> s.toLong()
43+
ValueType.Float -> s.toDouble()
4444
else -> throw IllegalArgumentException("No argument: ${data.type}")
4545
}
4646
}

src/iris/json/flow/JsonFlowParser.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package iris.json.flow
22

33
import iris.json.Configuration
4-
import iris.json.IrisJson
54
import iris.json.Util
65
import java.io.*
76
import java.net.URL
@@ -37,20 +36,20 @@ class JsonFlowParser(val tokener: Tokener, val configuration: Configuration) {
3736
val source = this.tokener
3837
val char = source.nextChar()
3938
val type = when {
40-
Util.isDigitOrAlpha(char) || char == '-' -> IrisJson.Type.Value
41-
char == '{' -> IrisJson.Type.Object
42-
char == '[' -> IrisJson.Type.Array
43-
char == '"' || char == '\'' -> IrisJson.Type.String
39+
Util.isDigitOrAlpha(char) || char == '-' -> Util.Type.Value
40+
char == '{' -> Util.Type.Object
41+
char == '[' -> Util.Type.Array
42+
char == '"' || char == '\'' -> Util.Type.String
4443
else -> throw source.exception("Character: \"$char\"")
4544
}
4645

4746
return when (type) {
48-
IrisJson.Type.Value -> {
47+
Util.Type.Value -> {
4948
source.back(); FlowValue(source)
5049
}
51-
IrisJson.Type.Object -> FlowObject(this)
52-
IrisJson.Type.String -> FlowString(source, char)
53-
IrisJson.Type.Array -> FlowArray(this)
50+
Util.Type.Object -> FlowObject(this)
51+
Util.Type.String -> FlowString(source, char)
52+
Util.Type.Array -> FlowArray(this)
5453
else -> throw source.exception("$type not realised yet")
5554
}
5655
}

src/iris/json/flow/Tokener.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package iris.json.flow
22

3-
import iris.json.IrisJson
3+
import iris.json.Util
44

55
/**
66
* @created 20.09.2020
@@ -20,7 +20,7 @@ interface Tokener {
2020

2121
fun readPrimitive(): PrimitiveData
2222

23-
class PrimitiveData(val sequence: CharSequence, val type: IrisJson.ValueType)
23+
class PrimitiveData(val sequence: CharSequence, val type: Util.ValueType)
2424

2525
fun back()
2626

0 commit comments

Comments
 (0)