blob: 7c01daa8fcf9ee5132a22254d0d0b3a9cab7c359 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.json.internal
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.*
@OptIn(ExperimentalSerializationApi::class)
internal class JsonTreeReader(
configuration: JsonConfiguration,
private val lexer: AbstractJsonLexer
) {
private val isLenient = configuration.isLenient
private var stackDepth = 0
private fun readObject(): JsonElement = readObjectImpl {
read()
}
private suspend fun DeepRecursiveScope<Unit, JsonElement>.readObject(): JsonElement =
readObjectImpl { callRecursive(Unit) }
private inline fun readObjectImpl(reader: () -> JsonElement): JsonObject {
var lastToken = lexer.consumeNextToken(TC_BEGIN_OBJ)
if (lexer.peekNextToken() == TC_COMMA) lexer.fail("Unexpected leading comma")
val result = linkedMapOf<String, JsonElement>()
while (lexer.canConsumeValue()) {
// Read key and value
val key = if (isLenient) lexer.consumeStringLenient() else lexer.consumeString()
lexer.consumeNextToken(TC_COLON)
val element = reader()
result[key] = element
// Verify the next token
lastToken = lexer.consumeNextToken()
when (lastToken) {
TC_COMMA -> Unit // no-op, can continue with `canConsumeValue` that verifies the token after comma
TC_END_OBJ -> break // `canConsumeValue` can return incorrect result, since it checks token _after_ TC_END_OBJ
else -> lexer.fail("Expected end of the object or comma")
}
}
// Check for the correct ending
if (lastToken == TC_BEGIN_OBJ) { // Case of empty object
lexer.consumeNextToken(TC_END_OBJ)
} else if (lastToken == TC_COMMA) { // Trailing comma
lexer.fail("Unexpected trailing comma")
}
return JsonObject(result)
}
private fun readArray(): JsonElement {
var lastToken = lexer.consumeNextToken()
// Prohibit leading comma
if (lexer.peekNextToken() == TC_COMMA) lexer.fail("Unexpected leading comma")
val result = arrayListOf<JsonElement>()
while (lexer.canConsumeValue()) {
val element = read()
result.add(element)
lastToken = lexer.consumeNextToken()
if (lastToken != TC_COMMA) {
lexer.require(lastToken == TC_END_LIST) { "Expected end of the array or comma" }
}
}
// Check for the correct ending
if (lastToken == TC_BEGIN_LIST) { // Case of empty object
lexer.consumeNextToken(TC_END_LIST)
} else if (lastToken == TC_COMMA) { // Trailing comma
lexer.fail("Unexpected trailing comma")
}
return JsonArray(result)
}
private fun readValue(isString: Boolean): JsonPrimitive {
val string = if (isLenient || !isString) {
lexer.consumeStringLenient()
} else {
lexer.consumeString()
}
if (!isString && string == NULL) return JsonNull
return JsonLiteral(string, isString)
}
fun read(): JsonElement {
return when (val token = lexer.peekNextToken()) {
TC_STRING -> readValue(isString = true)
TC_OTHER -> readValue(isString = false)
TC_BEGIN_OBJ -> {
/*
* If the object has the depth of 200 (an arbitrary "good enough" constant), it means
* that it's time to switch to stackless recursion to avoid StackOverflowError.
* This case is quite rare and specific, so more complex nestings (e.g. through
* the chain of JsonArray and JsonElement) are not supported.
*/
val result = if (++stackDepth == 200) {
readDeepRecursive()
} else {
readObject()
}
--stackDepth
result
}
TC_BEGIN_LIST -> readArray()
else -> lexer.fail("Cannot begin reading element, unexpected token: $token")
}
}
private fun readDeepRecursive(): JsonElement = DeepRecursiveFunction<Unit, JsonElement> {
when (lexer.peekNextToken()) {
TC_STRING -> readValue(isString = true)
TC_OTHER -> readValue(isString = false)
TC_BEGIN_OBJ -> readObject()
TC_BEGIN_LIST -> readArray()
else -> lexer.fail("Can't begin reading element, unexpected token")
}
}.invoke(Unit)
}
|