Skip to content

Commit aa8fb2b

Browse files
committed
Presence of kotlin-reflect library is now unnecessary.
1 parent fa7d94d commit aa8fb2b

22 files changed

+312
-126
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Speed improvement is achieved by idea of Proxy pattern, where objects are create
1212
#### Deserialization
1313
🔥 New feature (v0.5). Deserialization to objects.
1414

15-
Full source code on [iris/json/test/serialization.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/src/iris/json/test/serialization.kt)
15+
Full source code on [iris/json/test/serialization.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/test/iris/json/test/serialization.kt)
1616
````kotlin
1717
val item = IrisJsonParser("""{"id": 3,
1818
|"person1": {"name": "Akbar", "age": 35, "cashAmount": 12200.12, "property": {"name": "Домик в деревне"}},
@@ -89,7 +89,7 @@ println("To Double: " + res.find("object.message.attachments[0].wall.id").asDoub
8989
## Performance test
9090

9191
#### Array of 50 elements
92-
Test code is in [iris/json/test/performance_array.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/src/iris/json/test/performance_array.kt) file.
92+
Test code is in [iris/json/test/performance_array.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/test/iris/json/test/performance_array.kt) file.
9393

9494
Test JSON file is in [test_array.json](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/test_array.json) file.
9595

@@ -114,7 +114,7 @@ POJO: 10
114114

115115
#### Complex json-tree structure
116116

117-
Test code is in [iris/json/test/performance_object_tree.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/src/iris/json/test/performance_object_tree.kt) file.
117+
Test code is in [iris/json/test/performance_object_tree.kt](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/test/iris/json/test/performance_object_tree.kt) file.
118118

119119
Test JSON file is in [test.json](https://github.com/iris2iris/iris-json-parser-kotlin/blob/master/test.json) file.
120120

src/iris/json/JsonArray.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ package iris.json
55
* @author [Ivan Ivanov](https://vk.com/irisism)
66
*/
77
interface JsonArray: JsonItem, Iterable<JsonItem> {
8-
fun getList(): Collection<JsonItem>
8+
fun getList(): List<JsonItem>
9+
val size: Int
10+
fun isEmpty(): Boolean
11+
fun isNotEmpty(): Boolean
912
}

src/iris/json/JsonEncoder.kt

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

3-
import iris.json.flow.JsonFlowParser
4-
53
/**
64
* @created 26.09.2020
75
* @author [Ivan Ivanov](https://vk.com/irisism)
@@ -156,32 +154,4 @@ object JsonEncoder {
156154
}
157155
sb.append(']')
158156
}
159-
}
160-
161-
fun main() {
162-
val text = """{
163-
"glossary": {
164-
"title": "example glossary",
165-
"GlossDiv": {
166-
"title": "S",
167-
"GlossList": {
168-
"GlossEntry": {
169-
"ID": "SGML",
170-
"SortAs": "SGML",
171-
"GlossTerm": "Standard Generalized Markup Language",
172-
"Acronym": "SGML",
173-
"Abbrev": "ISO 8879:1986",
174-
"GlossDef": {
175-
"para": "A meta-markup language, used to create markup languages such as DocBook.",
176-
"GlossSeeAlso": ["GML", "XML"]
177-
},
178-
"GlossSee": "markup"
179-
}
180-
}
181-
}
182-
}
183-
}"""
184-
val el = JsonFlowParser.start(text).obj()
185-
val res = JsonEncoder.encode(el)
186-
print(res)
187157
}

src/iris/json/JsonItem.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ interface JsonItem {
1111
operator fun get(ind: Int): JsonItem
1212
operator fun get(key: String): JsonItem
1313

14+
operator fun set(ind: Int, value: JsonItem): JsonItem
15+
operator fun set(key: String, value: JsonItem): JsonItem
1416
operator fun set(ind: Int, value: Any?): JsonItem
1517
operator fun set(key: String, value: Any?): JsonItem
1618

src/iris/json/flow/FlowArray.kt

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

33
import iris.json.JsonArray
44
import iris.json.JsonItem
5-
import iris.json.proxy.JsonProxyUtil
65

76
/**
87
* @created 20.09.2020
@@ -100,20 +99,37 @@ class FlowArray(tokener: Tokener) : FlowItem(tokener), JsonArray {
10099
return get(ind)
101100
}
102101

103-
override fun getList(): Collection<JsonItem> {
102+
override fun getList(): List<JsonItem> {
104103
parse()
105104
return items
106105
}
107106

108-
override fun set(ind: Int, value: Any?): JsonItem {
109-
items[ind] = JsonProxyUtil.wrap(value)
107+
override val size: Int
108+
get() = getList().size
109+
110+
override fun isEmpty(): Boolean {
111+
if (isDone) return items.isEmpty()
112+
if (items.isNotEmpty()) return false
113+
val res = parseNextAndAdd()
114+
return res == null
115+
}
116+
117+
override fun isNotEmpty(): Boolean {
118+
if (isDone) return items.isNotEmpty()
119+
if (items.isNotEmpty()) return true
120+
val res = parseNextAndAdd()
121+
return res != null
122+
}
123+
124+
override fun set(ind: Int, value: JsonItem): JsonItem {
125+
items[ind] = value
110126
val obj = this.obj
111127
if (obj != null)
112-
obj[ind] = value
128+
obj[ind] = value.obj()
113129
return this
114130
}
115131

116-
override fun set(key: String, value: Any?): JsonItem {
132+
override fun set(key: String, value: JsonItem): JsonItem {
117133
return set(key.toInt(), value)
118134
}
119135

src/iris/json/flow/FlowObject.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import iris.json.JsonEntry
44
import iris.json.JsonItem
55
import iris.json.JsonObject
66
import iris.json.plain.IrisJsonNull
7-
import iris.json.proxy.JsonProxyUtil
87
import java.util.*
98

109
/**
@@ -83,22 +82,21 @@ class FlowObject(tokener: Tokener) : FlowItem(tokener), JsonObject {
8382
return res
8483
}
8584

86-
override fun set(key: String, value: Any?): JsonItem {
85+
override fun set(key: String, value: JsonItem): JsonItem {
8786
val el = entries
8887
val index = el.indexOfFirst { it.first == key }
89-
val wrapValue = JsonProxyUtil.wrap(value)
9088
if (index == -1)
91-
el += JsonEntry(key, wrapValue)
89+
el += JsonEntry(key, value)
9290
else
93-
el[index] = JsonEntry(key, wrapValue)
91+
el[index] = JsonEntry(key, value)
9492
val obj = obj
9593
if (obj != null) {
96-
obj[key] = value
94+
obj[key] = value.obj()
9795
}
9896
return this
9997
}
10098

101-
override fun set(ind: Int, value: Any?): JsonItem {
99+
override fun set(ind: Int, value: JsonItem): JsonItem {
102100
return set(ind.toString(), value)
103101
}
104102

src/iris/json/flow/TokenerString.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TokenerString(source: String) : Tokener {
1616
const val CR = '\r'.toInt()
1717
}
1818

19-
private val source = source.toCharArray()
19+
val source = source.toCharArray()
2020
var pointer: Int = 0
2121

2222
override fun nextChar(): Char {

src/iris/json/plain/IrisJsonArray.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package iris.json.plain
22

33
import iris.json.JsonArray
44
import iris.json.JsonItem
5-
import iris.json.proxy.JsonProxyUtil
65

76
/**
87
* @created 14.04.2020
98
* @author [Ivan Ivanov](https://vk.com/irisism)
109
*/
11-
class IrisJsonArray(private val items: List<IrisJsonItem>) : IrisJsonItem(), JsonArray {
10+
class IrisJsonArray(private val items: List<JsonItem>) : IrisJsonItem(), JsonArray {
1211

1312
override fun <A : Appendable> joinTo(buffer: A): A {
1413
buffer.append('[')
@@ -17,28 +16,35 @@ class IrisJsonArray(private val items: List<IrisJsonItem>) : IrisJsonItem(), Jso
1716
return buffer
1817
}
1918

20-
override fun get(ind: Int): IrisJsonItem {
19+
override fun get(ind: Int): JsonItem {
2120
return items[ind]
2221
}
2322

24-
override fun get(key: String): IrisJsonItem {
23+
override fun get(key: String): JsonItem {
2524
val ind = key.toInt()
2625
return get(ind)
2726
}
2827

29-
override fun getList(): Collection<JsonItem> {
28+
override fun getList(): List<JsonItem> {
3029
return items
3130
}
3231

33-
override fun set(ind: Int, value: Any?): JsonItem {
34-
(items as MutableList<Any?>)[ind] = JsonProxyUtil.wrap(value)
32+
override val size: Int
33+
get() = getList().size
34+
35+
override fun isEmpty() = items.isEmpty()
36+
37+
override fun isNotEmpty() = items.isNotEmpty()
38+
39+
override fun set(ind: Int, value: JsonItem): JsonItem {
40+
(items as MutableList<Any?>)[ind] = value
3541
val obj = this.obj
3642
if (obj != null)
37-
obj[ind] = value
43+
obj[ind] = value.obj()
3844
return this
3945
}
4046

41-
override fun set(key: String, value: Any?): JsonItem {
47+
override fun set(key: String, value: JsonItem): JsonItem {
4248
return set(key.toInt(), value)
4349
}
4450

@@ -58,11 +64,11 @@ class IrisJsonArray(private val items: List<IrisJsonItem>) : IrisJsonItem(), Jso
5864

5965
override fun iterable() = this
6066

61-
override fun iterator(): Iterator<IrisJsonItem> {
67+
override fun iterator(): Iterator<JsonItem> {
6268
return Iter()
6369
}
6470

65-
private inner class Iter : Iterator<IrisJsonItem> {
71+
private inner class Iter : Iterator<JsonItem> {
6672

6773
private val size = items.size
6874
private var pointer = 0
@@ -71,7 +77,7 @@ class IrisJsonArray(private val items: List<IrisJsonItem>) : IrisJsonItem(), Jso
7177
return pointer < size
7278
}
7379

74-
override fun next(): IrisJsonItem {
80+
override fun next(): JsonItem {
7581
val item = items[pointer]
7682
pointer++
7783
return item

src/iris/json/plain/IrisJsonItem.kt

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

33
import iris.json.JsonItem
4+
import iris.json.proxy.JsonProxyUtil
45

56
/**
67
* @created 14.04.2020
@@ -16,59 +17,63 @@ abstract class IrisJsonItem() : JsonItem {
1617
return when (val obj = obj()) {
1718
is Int -> obj
1819
is Number -> obj.toInt()
19-
else -> null
20+
else -> obj.toString().toIntOrNull()
2021
}
2122
}
2223

2324
override fun asInt(): Int {
2425
return when (val obj = obj()) {
2526
is Int -> obj
26-
else -> (obj() as Number).toInt()
27+
is Number -> obj.toInt()
28+
else -> obj.toString().toInt()
2729
}
2830
}
2931

3032
override fun asLongOrNull(): Long? {
3133
return when (val obj = obj()) {
3234
is Long -> obj
3335
is Number -> obj.toLong()
34-
else -> null
36+
else -> obj.toString().toLongOrNull()
3537
}
3638
}
3739

3840
override fun asLong(): Long {
3941
return when (val obj = obj()) {
4042
is Long -> obj
41-
else -> (obj() as Number).toLong()
43+
is Number -> obj.toLong()
44+
else -> obj.toString().toLong()
4245
}
4346
}
4447

4548
override fun asDoubleOrNull(): Double? {
4649
return when (val obj = obj()) {
4750
is Double -> obj
4851
is Number -> obj.toDouble()
49-
else -> null
52+
else -> obj.toString().toDoubleOrNull()
5053
}
5154
}
5255

5356
override fun asDouble(): Double {
5457
return when (val obj = obj()) {
5558
is Double -> obj
56-
else -> (obj() as Number).toDouble()
59+
is Number -> obj.toDouble()
60+
else -> obj.toString().toDouble()
5761
}
5862
}
5963

6064
override fun asFloatOrNull(): Float? {
6165
return when (val obj = obj()) {
6266
is Float -> obj
6367
is Number -> obj.toFloat()
64-
else -> null
68+
else -> obj.toString().toFloatOrNull()
6569
}
6670
}
6771

6872
override fun asFloat(): Float {
6973
return when (val obj = obj()) {
7074
is Float -> obj
71-
else -> (obj() as Number).toFloat()
75+
is Number -> obj.toFloat()
76+
else -> obj.toString().toFloat()
7277
}
7378
}
7479

@@ -102,11 +107,11 @@ abstract class IrisJsonItem() : JsonItem {
102107
}
103108

104109
override fun asStringOrNull(): String? {
105-
return obj() as String?
110+
return obj()?.toString()
106111
}
107112

108113
override fun asString(): String {
109-
return obj() as String
114+
return obj()?.toString()!!
110115
}
111116

112117
override fun find(tree: Array<String>): JsonItem {
@@ -131,14 +136,22 @@ abstract class IrisJsonItem() : JsonItem {
131136
return find(tree.replace('[', '.').replace("]", "").replace(' ', '.').split('.'))
132137
}
133138

134-
override fun set(ind: Int, value: Any?): JsonItem {
139+
override fun set(ind: Int, value: JsonItem): JsonItem {
135140
throw IllegalStateException("Set operation is not available")
136141
}
137142

138-
override fun set(key: String, value: Any?): JsonItem {
143+
override fun set(key: String, value: JsonItem): JsonItem {
139144
throw IllegalStateException("Set operation is not available")
140145
}
141146

147+
override fun set(ind: Int, value: Any?): JsonItem {
148+
return set(ind, JsonProxyUtil.wrap(value))
149+
}
150+
151+
override fun set(key: String, value: Any?): JsonItem {
152+
return set(key, JsonProxyUtil.wrap(value))
153+
}
154+
142155
override fun equals(other: Any?): Boolean {
143156
return when (other) {
144157
null -> false

0 commit comments

Comments
 (0)