fix json; v0.13

This commit is contained in:
alexander.nutz
2024-04-03 07:47:48 +02:00
parent 25086a3c7e
commit 469d5fcb8c
4 changed files with 46 additions and 5 deletions

View File

@@ -12,8 +12,8 @@ object JSON {
}
val jsonString = parser {
it.require("\"")
?.untilRequire("\"") { str -> Str(str) }
it.stringWithEscape()
?.mapSecond { Str(it) }
}
val jsonArray = parser {
@@ -30,6 +30,8 @@ object JSON {
val jsonBool = parser { it.require("true")?.to(Bool(true)) } or
parser { it.require("false")?.to(Bool(false)) }
val jsonNull = parser { it.require("null")?.to(Nul()) }
val jsonObj = parser {
it.require("{")
?.array(",") { elem ->
@@ -47,7 +49,7 @@ object JSON {
}
init {
jsonElement = (jsonArray or jsonNum or jsonString or jsonObj or jsonBool).trim()
jsonElement = (jsonArray or jsonNum or jsonString or jsonObj or jsonBool or jsonNull).trim()
}
interface Element {
@@ -62,6 +64,7 @@ object JSON {
fun isStr() = this is Str
fun isObj() = this is Obj
fun isBool() = this is Bool
fun isNul() = this is Nul
}
data class Array(val value: List<Element>): Element {
@@ -90,6 +93,8 @@ object JSON {
value.toString()
}
class Nul: Element
fun parse(string: String): Element? =
jsonElement(Parsable(string))?.second
}