mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 22:02:27 +00:00
Improve Lua and JSON serialization
This commit is contained in:
parent
3027d67037
commit
e3cd476a9b
20 changed files with 1041 additions and 476 deletions
|
@ -51,50 +51,6 @@ assert(EscapeParam("?hello&there<>") == "%3Fhello%26there%3C%3E")
|
|||
assert(DecodeLatin1("hello\xff\xc0") == "helloÿÀ")
|
||||
assert(EncodeLatin1("helloÿÀ") == "hello\xff\xc0")
|
||||
|
||||
assert(EncodeLua(nil) == "nil")
|
||||
assert(EncodeLua(0) == "0")
|
||||
assert(EncodeLua(3.14) == "3.14")
|
||||
assert(EncodeLua({2, 1}) == "{2, 1}")
|
||||
|
||||
assert(EncodeJson(nil) == "null")
|
||||
assert(EncodeJson(0) == "0")
|
||||
assert(EncodeJson(3.14) == "3.14")
|
||||
assert(EncodeJson({2, 1}) == "[2,1]")
|
||||
|
||||
assert(EncodeLua(" [\"new\nline\"] ") == "\" [\\\"new\\nline\\\"] \"")
|
||||
|
||||
-- EncodeLua() permits serialization of cyclic data structures
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
assert(string.match(EncodeLua(x), "{2, 1, \"cyclic@0x%x+\"}"))
|
||||
|
||||
-- EncodeLua() sorts table entries
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeLua(x) == '{a="a", b="b", c="c"}')
|
||||
|
||||
-- EncodeJson() doesn't permit serialization of cyclic data structures
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
json, err = EncodeJson(x)
|
||||
assert(not json)
|
||||
assert(err == "won't serialize cyclic lua table")
|
||||
|
||||
-- pass the parser to itself lool
|
||||
json, err = EncodeJson(EncodeJson)
|
||||
assert(not json)
|
||||
assert(err == "unsupported lua type")
|
||||
|
||||
-- EncodeJson() sorts table entries
|
||||
-- JSON always requires quotes around key names
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeJson(x) == '{"a":"a","b":"b","c":"c"}')
|
||||
|
||||
url = ParseUrl("https://jart:pass@redbean.dev/2.0.html?x&y=z#frag")
|
||||
assert(url.scheme == "https")
|
||||
assert(url.user == "jart")
|
||||
|
@ -168,17 +124,3 @@ assert(Uncompress(Compress("hello")) == "hello")
|
|||
assert(Compress("hello") == "\x05\x86\xa6\x106x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15")
|
||||
assert(Compress("hello", 0) == "\x05\x86\xa6\x106x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15")
|
||||
assert(Compress("hello", 0, true) == "x\x01\x01\x05\x00\xfa\xffhello\x06,\x02\x15")
|
||||
|
||||
----------------------------------------------------------------------------------------------------
|
||||
-- benchmarks
|
||||
|
||||
function LuaSerialization()
|
||||
EncodeLua({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
function JsonSerialization()
|
||||
EncodeJson({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
print("LuaSerialization", Benchmark(LuaSerialization))
|
||||
print("JsonSerialization", Benchmark(JsonSerialization))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue