add json module json

This commit is contained in:
tursom 2020-11-04 14:53:47 +08:00
parent 9e9cd7695c
commit a9015bcdb4
2 changed files with 13 additions and 25 deletions

View File

@ -1,3 +0,0 @@
dependencies {
compile project(":")
}

View File

@ -12,11 +12,11 @@ object Json {
val content = JsonParseContent(json)
val parse = parse(content)
jumpWhitespace(content)
if (content.index != json.length) throw JsonFormatException(json)
if (content.index != json.length) throw JsonFormatException("$json[${json[content.index]}] remain characters")
return parse
}
internal data class JsonParseContent(inline val json: String, inline var index: Int = 0)
internal data class JsonParseContent(val json: String, var index: Int = 0)
private fun parse(content: JsonParseContent): Any? {
jumpWhitespace(content)
@ -31,14 +31,13 @@ object Json {
}
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseNull(content: JsonParseContent) = if (content.json.startsWith("null", content.index)) {
private fun parseNull(content: JsonParseContent) = if (content.json.startsWith("null", content.index)) {
content.index += 4
null
} else throw JsonFormatException(content)
@Suppress("ControlFlowWithEmptyBody", "NOTHING_TO_INLINE")
private inline fun parseBoolean(content: JsonParseContent) = when {
@Suppress("ControlFlowWithEmptyBody")
private fun parseBoolean(content: JsonParseContent) = when {
content.json.startsWith("true", content.index) -> {
content.index += 4
true
@ -50,24 +49,20 @@ object Json {
else -> throw JsonFormatException(content)
}
@Suppress("NOTHING_TO_INLINE")
private inline fun jumpWhitespaceLoopCondition(json: String, index: Int) = index < json.length && json[index] in " \t\r\n"
private fun jumpWhitespaceLoopCondition(json: String, index: Int) = index < json.length && json[index] in " \t\r\n"
@Suppress("NOTHING_TO_INLINE")
private inline fun jumpWhitespace(content: JsonParseContent) {
private fun jumpWhitespace(content: JsonParseContent) {
@Suppress("ControlFlowWithEmptyBody")
if (jumpWhitespaceLoopCondition(content.json, content.index)) while (jumpWhitespaceLoopCondition(content.json, ++content.index));
}
@Suppress("NOTHING_TO_INLINE")
private inline fun charToInt(char: Char): Int {
private fun charToInt(char: Char): Int {
val indexOf = char - '0'
if (indexOf < 0 || indexOf > 9) throw JsonFormatException("$char is not an number")
return indexOf
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseInt(content: JsonParseContent): Number {
private fun parseInt(content: JsonParseContent): Number {
var number = charToInt(content.json[content.index]).toLong()
while (++content.index < content.json.length && content.json[content.index] in '0'..'9') {
number = number * 10 + charToInt(content.json[content.index])
@ -75,8 +70,7 @@ object Json {
return if (number <= Int.MAX_VALUE) number.toInt() else number
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseNumber(content: JsonParseContent): Number {
private fun parseNumber(content: JsonParseContent): Number {
val negative = content.json[content.index] == '-'
if (negative || content.json[content.index] == '+') content.index++
var number: Number = when (content.json[content.index]) {
@ -112,8 +106,7 @@ object Json {
} else number
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseString(content: JsonParseContent): String {
private fun parseString(content: JsonParseContent): String {
if (content.json[content.index++] != '"') throw JsonFormatException("string not begin with '\"'")
val builder = StringBuilder()
while (content.index < content.json.length) when (content.json[content.index]) {
@ -146,8 +139,7 @@ object Json {
throw JsonFormatException(content)
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseObj(content: JsonParseContent): Map<String, Any?> {
private fun parseObj(content: JsonParseContent): Map<String, Any?> {
if (content.json[content.index++] != '{') throw JsonFormatException(content)
jumpWhitespace(content)
if (content.json[content.index] == '}') {
@ -171,8 +163,7 @@ object Json {
return map
}
@Suppress("NOTHING_TO_INLINE")
private inline fun parseArray(content: JsonParseContent): List<Any?> {
private fun parseArray(content: JsonParseContent): List<Any?> {
if (content.json[content.index++] != '[') throw JsonFormatException(content)
jumpWhitespace(content)
if (content.json[content.index] == ']') {