11package iris.json.plain
22
3+ import iris.json.Configuration
34import iris.json.IrisJson
45import iris.json.JsonEntry
56import iris.json.flow.TokenerString
67import iris.sequence.IrisSequenceCharArray
8+ import java.io.*
9+ import java.net.URL
710import java.util.*
811import kotlin.collections.ArrayList
912import kotlin.math.max
@@ -13,12 +16,32 @@ import kotlin.math.min
1316 * @created 14.04.2020
1417 * @author [Ivan Ivanov](https://vk.com/irisism)
1518 */
16- class IrisJsonParser (source : String ) {
19+ class JsonPlainParser (source : String , private val configuration : Configuration = Configuration .globalConfiguration) {
20+
21+ companion object {
22+
23+ fun parse (url : URL , configuration : Configuration = Configuration .globalConfiguration) =
24+ parse(url.openStream(), configuration)
25+
26+ fun parse (ins : InputStream , configuration : Configuration = Configuration .globalConfiguration) =
27+ parse(reader(InputStreamReader (ins)), configuration)
28+
29+ fun parse (reader : Reader , configuration : Configuration = Configuration .globalConfiguration) =
30+ parse(reader(reader), configuration)
31+
32+ fun parse (file : File , configuration : Configuration = Configuration .globalConfiguration) =
33+ parse(reader(FileReader (file)), configuration)
34+
35+ private fun reader (reader : Reader ) = reader.use { it.readText() }
36+
37+ fun parse (source : String , configuration : Configuration = Configuration .globalConfiguration): IrisJsonItem {
38+ return JsonPlainParser (source, configuration).readItem()
39+ }
40+ }
1741
1842 private var pointer = 0
1943 private val source: CharArray = source.toCharArray()
2044
21-
2245 fun parse (): IrisJsonItem {
2346 return readItem()
2447 }
@@ -34,23 +57,28 @@ class IrisJsonParser(source: String) {
3457 else -> throw IllegalArgumentException (" Character: \" $char \" at $pointer \n " + getPlace())
3558 }
3659
37- if (type == IrisJson .Type .Value ) { // примитивы
38- pointer--
39- val start = pointer
40- val value = readPrimitive()
41- val end = pointer
42- return IrisJsonValue (IrisSequenceCharArray (source, start, end), value)
43- } else if (type == IrisJson .Type .Object ) {
44- return readObject()
45- } else if (type == IrisJson .Type .String ) {
46- val start = pointer
47- readString(char)
48- val end = pointer - 1
49- return IrisJsonString (IrisSequenceCharArray (source, start, end))
50- } else if (type == IrisJson .Type .Array ) {
51- return readArray()
52- } else
53- TODO (" $type not realised yet $pointer \n " + getPlace())
60+ when (type) {
61+ IrisJson .Type .Value -> { // примитивы
62+ pointer--
63+ val start = pointer
64+ val value = readPrimitive()
65+ val end = pointer
66+ return IrisJsonValue (IrisSequenceCharArray (source, start, end), value)
67+ }
68+ IrisJson .Type .Object -> {
69+ return readObject()
70+ }
71+ IrisJson .Type .String -> {
72+ val start = pointer
73+ readString(char)
74+ val end = pointer - 1
75+ return IrisJsonString (IrisSequenceCharArray (source, start, end))
76+ }
77+ IrisJson .Type .Array -> {
78+ return readArray()
79+ }
80+ else -> TODO (" $type not realised yet $pointer \n " + getPlace())
81+ }
5482 }
5583
5684 private fun getPlace (): String {
@@ -59,14 +87,13 @@ class IrisJsonParser(source: String) {
5987
6088 private fun readObject (): IrisJsonObject {
6189
62- // val key = readKey() ?: return IrisJsonObject(ArrayList(0))
63- val key = readKey() ? : return IrisJsonObject (ArrayList (0 ))
90+ val key = readKey() ? : return IrisJsonObject (ArrayList (0 ), configuration)
6491 val value = readValue()
6592
66- val key2 = readKey() ? : return IrisJsonObject (ArrayList <JsonEntry >(1 ).also { it + = key to value })
93+ val key2 = readKey() ? : return IrisJsonObject (ArrayList <JsonEntry >(1 ).also { it + = key to value }, configuration )
6794 val value2 = readValue()
6895
69- val key3 = readKey() ? : return IrisJsonObject (ArrayList <JsonEntry >(2 ).also { it + = key to value; it + = key2 to value2 })
96+ val key3 = readKey() ? : return IrisJsonObject (ArrayList <JsonEntry >(2 ).also { it + = key to value; it + = key2 to value2 }, configuration )
7097 val value3 = readValue()
7198
7299 val entries = LinkedList <JsonEntry >()
@@ -103,7 +130,7 @@ class IrisJsonParser(source: String) {
103130 entries + = key to value
104131 // counter--
105132 } while (pointer < len)
106- return IrisJsonObject (entries)
133+ return IrisJsonObject (entries, configuration )
107134 }
108135
109136 private fun readValue (): IrisJsonItem {
@@ -200,13 +227,13 @@ class IrisJsonParser(source: String) {
200227 var curType = IrisJson .ValueType .Integer
201228 val first = pointer
202229 val len = source.size
203- loop@ do {
230+ do {
204231 when (source[pointer]) {
205232 in ' 0' .. ' 9' -> {}
206233 ' -' -> if (first != pointer) curType = IrisJson .ValueType .Constant
207234 ' .' -> if (curType == IrisJson .ValueType .Integer ) curType = IrisJson .ValueType .Float
208235 in ' a' .. ' z' , in ' A' .. ' Z' -> curType = IrisJson .ValueType .Constant
209- else -> break @loop
236+ else -> break
210237 }
211238 pointer++
212239 } while (pointer < len)
0 commit comments