Add pretty printing to redbean serializers

This commit is contained in:
Justine Tunney 2022-07-22 10:10:33 -07:00
parent 6bb8b26d70
commit 516b68606f
11 changed files with 327 additions and 91 deletions

View file

@ -45,6 +45,36 @@ assert(EncodeJson("\"") == [["\""]])
assert(EncodeJson("\'") == [["\'"]])
assert(EncodeJson("\\") == [["\\"]])
assert(EncodeJson(
{yo=2,
bye={yo=2,
dawg=3},
there={yo=2},
sup={yo=2},
hi="hello"},
{pretty=true}) ==
"{\n"..
" \"bye\": {\n"..
" \"dawg\": 3,\n"..
" \"yo\": 2\n"..
" },\n"..
" \"hi\": \"hello\",\n"..
" \"sup\": {\"yo\": 2},\n"..
" \"there\": {\"yo\": 2},\n"..
" \"yo\": 2\n"..
"}")
assert(EncodeJson(
{yo=2, bye=1, there=10, sup=3, hi="hello"},
{pretty=true, indent=" "}) ==
"{\n"..
" \"bye\": 1,\n"..
" \"hi\": \"hello\",\n"..
" \"sup\": 3,\n"..
" \"there\": 10,\n"..
" \"yo\": 2\n"..
"}")
val, err = EncodeJson({[3]=3, [2]=3})
assert(val == nil)
assert(err == 'json objects must only use string keys')
@ -69,7 +99,12 @@ x.a = 'a'
x.b = 'b'
assert(EncodeJson(x) == '{"a":"a","b":"b","c":"c"}')
assert(EncodeJson({{{{{{{{{{{},{{{{},{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}) ==
assert(EncodeJson(0, {maxdepth=1}))
val, err = EncodeJson(0, {maxdepth=0})
assert(val == nil)
assert(err == 'table has great depth')
assert(EncodeJson({{{{{{{{{{{},{{{{},{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}, {maxdepth=64}) ==
'[[[[[[[[[[{},[[[{},[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]')
val, err = EncodeJson({{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})
assert(val == nil)
@ -82,7 +117,7 @@ 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=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}) ==
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}, {maxdepth=64}) ==
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
"{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":{\"k\":"..
@ -97,7 +132,7 @@ 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=0}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}, {maxdepth=64})
assert(res == nil)
assert(err == "table has great depth")