mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +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
|
@ -130,7 +130,14 @@ void FreeBulk(void) {
|
|||
}
|
||||
}
|
||||
|
||||
void MallocFree(void) {
|
||||
char *volatile p;
|
||||
p = malloc(16);
|
||||
free(p);
|
||||
}
|
||||
|
||||
BENCH(bulk_free, bench) {
|
||||
EZBENCH2("free(malloc(16))", donothing, MallocFree());
|
||||
EZBENCH2("free() bulk", BulkFreeBenchSetup(), FreeBulk());
|
||||
EZBENCH2("bulk_free()", BulkFreeBenchSetup(),
|
||||
bulk_free(bulk, ARRAYLEN(bulk)));
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
|
@ -24,60 +25,69 @@
|
|||
#include "libc/testlib/testlib.h"
|
||||
#include "net/http/escape.h"
|
||||
|
||||
char *o;
|
||||
size_t y;
|
||||
|
||||
void TearDownOnce(void) {
|
||||
free(o);
|
||||
o = 0;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
char *escapejs(const char *s) {
|
||||
char *p;
|
||||
size_t n;
|
||||
p = EscapeJsStringLiteral(s, strlen(s), &n);
|
||||
p = EscapeJsStringLiteral(&o, &y, s, strlen(s), &n);
|
||||
ASSERT_EQ(strlen(p), n);
|
||||
return p;
|
||||
}
|
||||
|
||||
TEST(EscapeJsStringLiteral, test) {
|
||||
EXPECT_STREQ("", gc(escapejs("")));
|
||||
EXPECT_STREQ("\\u00ff", gc(escapejs("\377")));
|
||||
EXPECT_STREQ("", escapejs(""));
|
||||
EXPECT_STREQ("\\u00ff", escapejs("\377"));
|
||||
EXPECT_STREQ("\\u00ff\\u0080\\u0080\\u0080\\u0080",
|
||||
gc(escapejs("\377\200\200\200\200")));
|
||||
escapejs("\377\200\200\200\200"));
|
||||
EXPECT_STREQ("\\u0001\\u0002\\u0003 \\u0026\\u003d\\u003c\\u003e\\/",
|
||||
gc(escapejs("\1\2\3 &=<>/")));
|
||||
escapejs("\1\2\3 &=<>/"));
|
||||
}
|
||||
|
||||
TEST(EscapeJsStringLiteral, testUcs2) {
|
||||
EXPECT_STREQ("\\u00d0\\u263b", gc(escapejs("Ð☻")));
|
||||
EXPECT_STREQ("\\u00d0\\u263b", escapejs("Ð☻"));
|
||||
}
|
||||
|
||||
TEST(EscapeJsStringLiteral, testAstralPlanes) {
|
||||
EXPECT_STREQ("\\ud800\\udf30\\ud800\\udf30", gc(escapejs("𐌰𐌰")));
|
||||
EXPECT_STREQ("\\ud800\\udf30\\ud800\\udf30", escapejs("𐌰𐌰"));
|
||||
}
|
||||
|
||||
TEST(EscapeJsStringLiteral, testBrokenUnicode_sparesInnocentCharacters) {
|
||||
EXPECT_STREQ("\\u00e1YO", gc(escapejs("\xE1YO")));
|
||||
EXPECT_STREQ("\\u00e1YO", escapejs("\xE1YO"));
|
||||
}
|
||||
|
||||
void makefile1(void) {
|
||||
FILE *f;
|
||||
char *p;
|
||||
size_t n;
|
||||
p = EscapeJsStringLiteral(kHyperion, kHyperionSize, &n);
|
||||
p = EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, &n);
|
||||
f = fopen("/tmp/a", "wb");
|
||||
fwrite(p, n, 1, f);
|
||||
fclose(f);
|
||||
free(p);
|
||||
}
|
||||
|
||||
void makefile2(void) {
|
||||
int fd;
|
||||
char *p;
|
||||
size_t n;
|
||||
p = EscapeJsStringLiteral(kHyperion, kHyperionSize, &n);
|
||||
p = EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, &n);
|
||||
fd = creat("/tmp/a", 0644);
|
||||
write(fd, p, n);
|
||||
close(fd);
|
||||
free(p);
|
||||
}
|
||||
|
||||
BENCH(EscapeJsStringLiteral, bench) {
|
||||
EZBENCH2("escapejs", donothing,
|
||||
free(EscapeJsStringLiteral(kHyperion, kHyperionSize, 0)));
|
||||
EZBENCH2("escapejs tiny", donothing,
|
||||
EscapeJsStringLiteral(&o, &y, "hello", 5, 0));
|
||||
EZBENCH2("escapejs book", donothing,
|
||||
EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, 0));
|
||||
EZBENCH2("makefile1", donothing, makefile1());
|
||||
EZBENCH2("makefile2", donothing, makefile2());
|
||||
}
|
||||
|
|
127
test/tool/net/encodejson_test.lua
Normal file
127
test/tool/net/encodejson_test.lua
Normal file
|
@ -0,0 +1,127 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
assert(EncodeJson(nil) == "null")
|
||||
assert(EncodeJson(true) == "true")
|
||||
assert(EncodeJson(false) == "false")
|
||||
assert(EncodeJson(0) == "0")
|
||||
assert(EncodeJson(0.0) == "0")
|
||||
assert(EncodeJson(3.14) == "3.14")
|
||||
assert(EncodeJson(0/0) == "null")
|
||||
assert(EncodeJson(math.huge) == "null")
|
||||
assert(EncodeJson(123.456e-789) == '0')
|
||||
assert(EncodeJson(9223372036854775807) == '9223372036854775807')
|
||||
assert(EncodeJson(-9223372036854775807 - 1) == '-9223372036854775808')
|
||||
assert(EncodeJson({2, 1}) == "[2,1]")
|
||||
assert(EncodeJson({3, 2}) == "[3,2]")
|
||||
assert(EncodeJson({[0]=false}) == "[]")
|
||||
assert(EncodeJson({[1]=123, [2]=456}) == "[123,456]")
|
||||
assert(EncodeJson({[1]=123, [3]=456}) == "[123]")
|
||||
assert(EncodeJson({["hi"]=1, [1]=2}) == "[2]")
|
||||
assert(EncodeJson({[1]=2, ["hi"]=1}) == "[2]")
|
||||
assert(EncodeJson({[3]=3, [1]=3}) == "[3]")
|
||||
assert(EncodeJson("hello") == "\"hello\"")
|
||||
assert(EncodeJson("\x00") == "\"\\u0000\"")
|
||||
assert(EncodeJson("\t") == "\"\\t\"")
|
||||
assert(EncodeJson("→") == "\"\\u2192\"")
|
||||
assert(EncodeJson("𐌰") == "\"\\ud800\\udf30\"")
|
||||
assert(EncodeJson("\t") == [["\t"]])
|
||||
assert(EncodeJson("\r") == [["\r"]])
|
||||
assert(EncodeJson("\n") == [["\n"]])
|
||||
assert(EncodeJson("\f") == [["\f"]])
|
||||
assert(EncodeJson("\"") == [["\""]])
|
||||
assert(EncodeJson("\'") == [["\'"]])
|
||||
assert(EncodeJson("\\") == [["\\"]])
|
||||
|
||||
val, err = EncodeJson({[3]=3, [2]=3})
|
||||
assert(val == nil)
|
||||
assert(err == 'json objects must only use string keys')
|
||||
|
||||
val, err = EncodeJson({[{[{[3]=2}]=2}]=2})
|
||||
assert(val == nil)
|
||||
assert(err == 'json objects must only use string keys')
|
||||
|
||||
val, err = EncodeJson(EncodeJson)
|
||||
assert(val == nil)
|
||||
assert(err == "unsupported lua type")
|
||||
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
val, err = EncodeJson(x)
|
||||
assert(val == nil)
|
||||
assert(err == "won't serialize cyclic lua table")
|
||||
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeJson(x) == '{"a":"a","b":"b","c":"c"}')
|
||||
|
||||
assert(EncodeJson({{{{{{{{{{{},{{{{},{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}) ==
|
||||
'[[[[[[[[[[{},[[[{},[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]')
|
||||
val, err = EncodeJson({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})
|
||||
assert(val == nil)
|
||||
assert(err == 'table has great depth')
|
||||
|
||||
-- 63 objects
|
||||
assert(EncodeJson(
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}) ==
|
||||
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
|
||||
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
|
||||
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
|
||||
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":0}}"..
|
||||
"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"..
|
||||
"}}}}}}}}}}}}}")
|
||||
|
||||
-- 64 objects
|
||||
res, err = EncodeJson(
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})
|
||||
assert(res == nil)
|
||||
assert(err == "table has great depth")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- benchmark nanos ticks
|
||||
-- JsonEncArray 366 1134
|
||||
-- JsonEncUnsort 754 2336
|
||||
-- JsonEncObject 1208 3742
|
||||
|
||||
function JsonEncArray()
|
||||
EncodeJson({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
function JsonEncObject()
|
||||
EncodeJson({yo=2, bye=1, there=10, sup=3, hi="hello"})
|
||||
end
|
||||
|
||||
UNSORT = {sorted=false}
|
||||
function JsonEncUnsort()
|
||||
EncodeJson({yo=2, bye=1, there=10, sup=3, hi="hello"}, UNSORT)
|
||||
end
|
||||
|
||||
function bench()
|
||||
print("JsonEncArray", Benchmark(JsonEncArray))
|
||||
print("JsonEncUnsort", Benchmark(JsonEncUnsort))
|
||||
print("JsonEncObject", Benchmark(JsonEncObject))
|
||||
end
|
120
test/tool/net/encodelua_test.lua
Normal file
120
test/tool/net/encodelua_test.lua
Normal file
|
@ -0,0 +1,120 @@
|
|||
-- Copyright 2022 Justine Alexandra Roberts Tunney
|
||||
--
|
||||
-- Permission to use, copy, modify, and/or distribute this software for
|
||||
-- any purpose with or without fee is hereby granted, provided that the
|
||||
-- above copyright notice and this permission notice appear in all copies.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
-- WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
-- WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
-- AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
-- DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
-- PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
-- PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
assert(EncodeLua(nil) == "nil")
|
||||
assert(EncodeLua(true) == "true")
|
||||
assert(EncodeLua(false) == "false")
|
||||
assert(EncodeLua(0) == "0")
|
||||
assert(EncodeLua(0.0) == "0.")
|
||||
assert(EncodeLua(3.14) == "3.14")
|
||||
assert(EncodeLua(0/0) == "0/0")
|
||||
assert(EncodeLua(123.456e-789) == '0.')
|
||||
assert(EncodeLua(9223372036854775807) == '9223372036854775807')
|
||||
assert(EncodeLua(-9223372036854775807 - 1) == '-9223372036854775807 - 1')
|
||||
assert(EncodeLua(7000) == '7000')
|
||||
assert(EncodeLua(0x100) == '0x0100')
|
||||
assert(EncodeLua(0x10000) == '0x00010000')
|
||||
assert(EncodeLua(0x100000000) == '0x0000000100000000')
|
||||
assert(EncodeLua(math.huge) == "math.huge")
|
||||
assert(EncodeLua({2, 1}) == "{2, 1}")
|
||||
assert(EncodeLua({3, 2}) == "{3, 2}")
|
||||
assert(EncodeLua({[0]=false}) == "{[0]=false}")
|
||||
assert(EncodeLua({[1]=123, [2]=456}) == "{123, 456}")
|
||||
assert(EncodeLua({[1]=123, [3]=456}) == "{[1]=123, [3]=456}")
|
||||
assert(EncodeLua({["hi"]=1, [1]=2}) == "{[1]=2, hi=1}")
|
||||
assert(EncodeLua({[1]=2, ["hi"]=1}) == "{[1]=2, hi=1}")
|
||||
assert(EncodeLua({[3]=3, [1]=3}) == "{[1]=3, [3]=3}")
|
||||
assert(EncodeLua({[{[{[3]=2}]=2}]=2}) == "{[{[{[3]=2}]=2}]=2}")
|
||||
assert(EncodeLua(" [\"new\nline\"] ") == "\" [\\\"new\\nline\\\"] \"")
|
||||
assert(EncodeLua("hello") == [["hello"]])
|
||||
assert(EncodeLua("\x00") == [["\x00"]])
|
||||
assert(EncodeLua("→") == [["\xe2\x86\x92"]])
|
||||
assert(EncodeLua("𐌰") == [["\xf0\x90\x8c\xb0"]])
|
||||
assert(EncodeLua("\a") == [["\a"]])
|
||||
assert(EncodeLua("\b") == [["\b"]])
|
||||
assert(EncodeLua("\r") == [["\r"]])
|
||||
assert(EncodeLua("\n") == [["\n"]])
|
||||
assert(EncodeLua("\v") == [["\v"]])
|
||||
assert(EncodeLua("\t") == [["\t"]])
|
||||
assert(EncodeLua("\f") == [["\f"]])
|
||||
assert(EncodeLua("\e") == [["\e"]])
|
||||
assert(EncodeLua("\"") == [["\""]])
|
||||
assert(EncodeLua("\\") == [["\\"]])
|
||||
|
||||
x = {}
|
||||
x.c = 'c'
|
||||
x.a = 'a'
|
||||
x.b = 'b'
|
||||
assert(EncodeLua(x) == '{a="a", b="b", c="c"}')
|
||||
|
||||
x = {2, 1}
|
||||
x[3] = x
|
||||
assert(string.match(EncodeLua(x), "{2, 1, \"cyclic@0x%x+\"}"))
|
||||
|
||||
-- 63 objects
|
||||
assert(EncodeLua(
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}) ==
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=0}}"..
|
||||
"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"..
|
||||
"}}}}}}}}}}}}}")
|
||||
|
||||
-- 64 objects
|
||||
assert(EncodeLua(
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=
|
||||
{k={k={k={k=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
) ==
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k="..
|
||||
"{k={k={k={k={k={k={k={k={k={k={k={k={k={k={k={k=\"greatdepth@0\"}}}"..
|
||||
"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"..
|
||||
"}}}}}}}}}}}}}")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- benchmark nanos ticks
|
||||
-- LuaEncArray 455 1410
|
||||
-- LuaEncUnsort 699 2165
|
||||
-- LuaEncObject 1134 3513
|
||||
|
||||
function LuaEncArray()
|
||||
EncodeLua({2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
function LuaEncObject()
|
||||
EncodeLua({hi=2, 1, 10, 3, "hello"})
|
||||
end
|
||||
|
||||
UNSORT = {sorted=false}
|
||||
function LuaEncUnsort()
|
||||
EncodeLua({hi=2, 1, 10, 3, "hello"}, UNSORT)
|
||||
end
|
||||
|
||||
function bench()
|
||||
print("LuaEncArray", Benchmark(LuaEncArray))
|
||||
print("LuaEncUnsort", Benchmark(LuaEncUnsort))
|
||||
print("LuaEncObject", Benchmark(LuaEncObject))
|
||||
end
|
|
@ -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))
|
||||
|
|
|
@ -115,8 +115,27 @@ res, err = DecodeJson('"\\ucjcc"')
|
|||
assert(res == nil)
|
||||
assert(err == 'invalid unicode escape')
|
||||
|
||||
res, err = DecodeJson('[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[')
|
||||
assert(not res)
|
||||
-- 63 objects
|
||||
res, err = DecodeJson([[
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}
|
||||
]])
|
||||
assert(res)
|
||||
|
||||
-- 64 objects
|
||||
res, err = DecodeJson([[
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":{"k":
|
||||
{"k":{"k":{"k":{"k":0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
||||
}}}}}}}}}}
|
||||
]])
|
||||
assert(res == nil)
|
||||
assert(err == "maximum depth exceeded")
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
@ -126,9 +145,11 @@ assert(err == "maximum depth exceeded")
|
|||
-- JsonParseInteger 66 206
|
||||
-- JsonParseDouble 123 383
|
||||
-- JsonParseString 116 361
|
||||
-- JsonParseArray 256 793
|
||||
-- JsonParseInts 256 793
|
||||
-- JsonParseFloats 361 1118
|
||||
-- JsonParseObject 890 2756
|
||||
-- JsonEncodeArray 639 1979
|
||||
-- JsonEncodeInts 498 1543
|
||||
-- JsonEncodeFloats 498 1543
|
||||
-- JsonEncodeObject 1333 4129
|
||||
|
||||
function JsonParseEmpty()
|
||||
|
@ -147,27 +168,39 @@ function JsonParseString()
|
|||
DecodeJson[[ "\ud800\udf30 he𐌰𐌰o \ud800\udf30" ]]
|
||||
end
|
||||
|
||||
function JsonParseArray()
|
||||
function JsonParseInts()
|
||||
DecodeJson[[ [123,456,789] ]]
|
||||
end
|
||||
|
||||
function JsonParseFloats()
|
||||
DecodeJson[[ [3.14,1.23,7.89] ]]
|
||||
end
|
||||
|
||||
function JsonParseObject()
|
||||
DecodeJson[[ {"3": "1", "4": "1", "5": {"3":"1", "4":"1", "5":"9"}} ]]
|
||||
end
|
||||
|
||||
function JsonEncodeArray()
|
||||
function JsonEncodeInts()
|
||||
EncodeJson({2, 0, {5, 7, 3}})
|
||||
end
|
||||
|
||||
function JsonEncodeFloats()
|
||||
EncodeJson({3.14,1.23,7.89})
|
||||
end
|
||||
|
||||
function JsonEncodeObject()
|
||||
EncodeJson({["3"]="1", ["4"]="1", ["5"]={["3"]="1", ["4"]="1", ["5"]="9"}})
|
||||
end
|
||||
|
||||
-- print('JsonParseEmpty', Benchmark(JsonParseEmpty))
|
||||
-- print('JsonParseInteg', Benchmark(JsonParseInteger))
|
||||
-- print('JsonParseDouble', Benchmark(JsonParseDouble))
|
||||
-- print('JsonParseString', Benchmark(JsonParseString))
|
||||
-- print('JsonParseArray', Benchmark(JsonParseArray))
|
||||
-- print('JsonParseObject', Benchmark(JsonParseObject))
|
||||
-- print('JsonEncodeArr', Benchmark(JsonEncodeArray))
|
||||
-- print('JsonEncodeObj', Benchmark(JsonEncodeObject))
|
||||
if nil then
|
||||
print('JsonParseEmpty', Benchmark(JsonParseEmpty))
|
||||
print('JsonParseInteg', Benchmark(JsonParseInteger))
|
||||
print('JsonParseDouble', Benchmark(JsonParseDouble))
|
||||
print('JsonParseString', Benchmark(JsonParseString))
|
||||
print('JsonParseInts', Benchmark(JsonParseInts))
|
||||
print('JsonParseFloats', Benchmark(JsonParseFloats))
|
||||
print('JsonParseObject', Benchmark(JsonParseObject))
|
||||
print('JsonEncodeInts', Benchmark(JsonEncodeInts))
|
||||
print('JsonEncodeFlts', Benchmark(JsonEncodeFloats))
|
||||
print('JsonEncodeObj', Benchmark(JsonEncodeObject))
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue