Further improve JSON serialization

This commit is contained in:
Justine Tunney 2022-07-11 23:06:49 -07:00
parent 4814b6bdf8
commit 6ee18986e4
20 changed files with 868 additions and 687 deletions

View file

@ -1,11 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_LOG_ROP_H_
#define COSMOPOLITAN_LIBC_LOG_ROP_H_
#include "libc/bits/likely.h"
#define RETURN_ON_ERROR(expr) \
do { \
if ((expr) == -1) { \
goto OnError; \
} \
#define RETURN_ON_ERROR(expr) \
do { \
if (UNLIKELY((expr) == -1)) { \
goto OnError; \
} \
} while (0)
#endif /* COSMOPOLITAN_LIBC_LOG_ROP_H_ */

View file

@ -61,6 +61,8 @@ 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
@ -78,7 +80,13 @@ x = {2, 1}
x[3] = x
json, err = EncodeJson(x)
assert(not json)
assert(err == "serialization failed")
assert(err == "won't serialize cyclic lua table")
-- pass the parser to itself lool
json, err = EncodeJson(EncodeJson)
assert(not json)
print(err)
assert(err == "unsupported lua type")
-- EncodeJson() sorts table entries
-- JSON always requires quotes around key names

View file

@ -13,58 +13,104 @@
-- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-- PERFORMANCE OF THIS SOFTWARE.
assert(EncodeLua(DecodeJson[[ ]]) == 'nil')
assert(EncodeLua(DecodeJson[[ 0 ]]) == '0' )
assert(EncodeLua(DecodeJson[[ [1] ]]) == '{1}')
assert(EncodeLua(DecodeJson[[ 2.3 ]]) == '2.3')
assert(EncodeLua(DecodeJson[[ [1,3,2] ]]) == '{1, 3, 2}')
-- assert(EncodeLua(DecodeJson[[ {1: 2, 3: 4} ]]) == '{[1]=2, [3]=4}')
assert(EncodeLua(DecodeJson[[ {"foo": 2, "bar": 4} ]]) == '{bar=4, foo=2}')
assert(EncodeLua(DecodeJson[[ null ]]) == 'nil')
assert(EncodeLua(DecodeJson[[ -123 ]]) == '-123')
assert(EncodeLua(DecodeJson[[ 1e6 ]]) == '1000000.')
assert(EncodeLua(DecodeJson[[ 1.e-6 ]]) == '0.000001')
assert(EncodeLua(DecodeJson[[ 1e-06 ]]) == '0.000001')
assert(EncodeLua(DecodeJson[[ 9.123e6 ]]) == '9123000.')
assert(EncodeLua(DecodeJson[[ [{"heh": [1,3,2]}] ]]) == '{{heh={1, 3, 2}}}')
assert(EncodeLua(DecodeJson[[ 3.14159 ]]) == '3.14159')
-- assert(EncodeLua(DecodeJson[[ {3=4} ]]) == '{[3]=4}')
assert(EncodeLua(DecodeJson[[ 1e-12 ]]) == '1e-12')
assert(EncodeLua(assert(DecodeJson[[ 0 ]])) == '0' )
assert(EncodeLua(assert(DecodeJson[[ [1] ]])) == '{1}')
assert(EncodeLua(assert(DecodeJson[[ 2.3 ]])) == '2.3')
assert(EncodeLua(assert(DecodeJson[[ [1,3,2] ]])) == '{1, 3, 2}')
-- assert(EncodeLua(assert(DecodeJson[[ {1: 2, 3: 4} ]])) == '{[1]=2, [3]=4}')
assert(EncodeLua(assert(DecodeJson[[ {"foo": 2, "bar": 4} ]])) == '{bar=4, foo=2}')
assert(EncodeLua(assert(DecodeJson[[ -123 ]])) == '-123')
assert(EncodeLua(assert(DecodeJson[[ 1e6 ]])) == '1000000.')
assert(EncodeLua(assert(DecodeJson[[ 1.e-6 ]])) == '0.000001')
assert(EncodeLua(assert(DecodeJson[[ 1e-06 ]])) == '0.000001')
assert(EncodeLua(assert(DecodeJson[[ 9.123e6 ]])) == '9123000.')
assert(EncodeLua(assert(DecodeJson[[ [{"heh": [1,3,2]}] ]])) == '{{heh={1, 3, 2}}}')
assert(EncodeLua(assert(DecodeJson[[ 3.14159 ]])) == '3.14159')
-- assert(EncodeLua(assert(DecodeJson[[ {3=4} ]])) == '{[3]=4}')
assert(EncodeLua(assert(DecodeJson[[ 1e-12 ]])) == '1e-12')
assert(EncodeJson(DecodeJson[[ 1e-12 ]]) == '1e-12')
assert(EncodeJson(DecodeJson[[ true ]]) == 'true')
assert(EncodeJson(DecodeJson[[ false ]]) == 'false')
assert(EncodeJson(DecodeJson[[ null ]]) == 'null')
assert(EncodeJson(DecodeJson[[ [] ]]) == '[]')
assert(EncodeJson(DecodeJson[[ {} ]]) == '{}')
assert(EncodeJson(assert(DecodeJson[[ 1e-12 ]])) == '1e-12')
assert(EncodeJson(assert(DecodeJson[[ true ]])) == 'true')
assert(EncodeJson(assert(DecodeJson[[ [] ]])) == '[]')
assert(EncodeJson(assert(DecodeJson[[ {} ]])) == '{}')
assert(DecodeJson[["\f"]] == '\f') -- c0
assert(DecodeJson[["\t"]] == '\t') -- c0
assert(DecodeJson[["\n"]] == '\n') -- c0
assert(DecodeJson[["\r"]] == '\r') -- c0
assert(DecodeJson[["\\"]] == '\\') -- c0
assert(DecodeJson[["\""]] == '\"') -- c0
assert(DecodeJson[["\u0100"]] == 'Ā') -- latin-1
assert(DecodeJson[["\ud800\udf30\ud800\udf30"]] == '𐌰𐌰') -- utf-16 astral planes gothic
assert(DecodeJson[["\uD800"]] == '\\uD800') -- utf-16 invalid (keep utf-8 well-formed)
assert(assert(DecodeJson[["\f"]]) == '\f') -- c0
assert(assert(DecodeJson[["\t"]]) == '\t') -- c0
assert(assert(DecodeJson[["\n"]]) == '\n') -- c0
assert(assert(DecodeJson[["\r"]]) == '\r') -- c0
assert(assert(DecodeJson[["\\"]]) == '\\') -- c0
assert(assert(DecodeJson[["\""]]) == '\"') -- c0
assert(assert(DecodeJson[["\u0100"]]) == 'Ā') -- latin-1
assert(assert(DecodeJson[["\ud800\udf30\ud800\udf30"]]) == '𐌰𐌰') -- utf-16 astral planes gothic
assert(assert(DecodeJson[["\uD800"]]) == '\\uD800') -- utf-16 invalid (keep utf-8 well-formed)
assert(EncodeJson(DecodeJson[[ -9223372036854775808 ]]) == '-9223372036854775808') -- minimum 64-bit integer
assert(EncodeJson(DecodeJson[[ 9223372036854775807 ]]) == '9223372036854775807') -- maximum 64-bit integer
assert(EncodeJson(DecodeJson[[ 9223372036854775808 ]]) == '9223372036854776000') -- switches to double due to integer overflow
assert(EncodeJson(DecodeJson[[ -9223372036854775809 ]]) == '-9223372036854776000') -- switches to double due to integer underflow
assert(EncodeJson(DecodeJson[[ 9223372036854775807.0 ]]) == '9223372036854776000') -- switches to double due to period mark
assert(EncodeJson(DecodeJson[[ 2.7182818284590452354 ]]) == '2.718281828459045') -- euler constant w/ 17 digit precision
assert( EncodeLua(DecodeJson[[ 2.7182818284590452354 ]]) == '2.718281828459045') -- euler constant w/ 17 digit precision
assert(EncodeJson(assert(DecodeJson[[ -9223372036854775808 ]])) == '-9223372036854775808') -- minimum 64-bit integer
assert(EncodeJson(assert(DecodeJson[[ 9223372036854775807 ]])) == '9223372036854775807') -- maximum 64-bit integer
assert(EncodeJson(assert(DecodeJson[[ 9223372036854775808 ]])) == '9223372036854776000') -- switches to double due to integer overflow
assert(EncodeJson(assert(DecodeJson[[ -9223372036854775809 ]])) == '-9223372036854776000') -- switches to double due to integer underflow
assert(EncodeJson(assert(DecodeJson[[ 9223372036854775807.0 ]])) == '9223372036854776000') -- switches to double due to period mark
assert(EncodeJson(assert(DecodeJson[[ 2.7182818284590452354 ]])) == '2.718281828459045') -- euler constant w/ 17 digit precision
assert( EncodeLua(assert(DecodeJson[[ 2.7182818284590452354 ]])) == '2.718281828459045') -- euler constant w/ 17 digit precision
res, err = DecodeJson[[ ]]
assert(not res)
assert(err == 'unexpected eof')
res, err = DecodeJson[[ {} {} ]]
assert(not res)
assert(err == "junk after expression")
res, err = DecodeJson[[ null ]]
assert(not res)
assert(err == "toplevel json can't be null")
res, err = DecodeJson[[ false ]]
assert(not res)
assert(err == "toplevel json can't be false")
res, err = DecodeJson[[ {3:4} ]]
assert(not res)
assert(err == "object key must be string")
res, err = DecodeJson[[ z ]]
assert(not res)
assert(err == "illegal character")
res, err = DecodeJson[[ "\e" ]]
assert(not res)
assert(err == "invalid escape character")
res, err = DecodeJson[[ {"key": } ]]
assert(not res)
assert(err == "unexpected '}'")
res, err = DecodeJson[[ {"key": ] ]]
assert(not res)
assert(err == "unexpected ']'")
res, err = DecodeJson[[ {"key": ]]
assert(not res)
assert(err == "unexpected eof")
res, err = DecodeJson[[ {true:3} ]]
assert(not res)
assert(err == "object key must be string")
res, err = DecodeJson('[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[')
assert(not res)
assert(err == "maximum depth exceeded")
--------------------------------------------------------------------------------
-- benchmark nanos ticks
--------------------------------------------------------------------------------
-- JsonParseEmpty 23 72
-- JsonParseInteger 45 142
-- JsonParseDouble 108 335
-- JsonParseString 106 329
-- JsonParseArray 243 754
-- JsonParseObject 523 1622
-- JsonParseEmpty 41 127
-- JsonParseInteger 66 206
-- JsonParseDouble 123 383
-- JsonParseString 116 361
-- JsonParseArray 256 793
-- JsonParseObject 890 2756
-- JsonEncodeArray 639 1979
-- JsonEncodeObject 1333 4129
function JsonParseEmpty()
DecodeJson[[]]
@ -87,7 +133,15 @@ function JsonParseArray()
end
function JsonParseObject()
DecodeJson[[ {"3":"1", "4":"1", "5":"9"} ]]
DecodeJson[[ {"3": "1", "4": "1", "5": {"3":"1", "4":"1", "5":"9"}} ]]
end
function JsonEncodeArray()
EncodeJson({2, 0, {5, 7, 3}})
end
function JsonEncodeObject()
EncodeJson({["3"]="1", ["4"]="1", ["5"]={["3"]="1", ["4"]="1", ["5"]="9"}})
end
print('JsonParseEmpty', Benchmark(JsonParseEmpty))
@ -96,3 +150,5 @@ print('JsonParseDouble', Benchmark(JsonParseDouble))
print('JsonParseString', Benchmark(JsonParseString))
print('JsonParseArray', Benchmark(JsonParseArray))
print('JsonParseObject', Benchmark(JsonParseObject))
print('JsonEncodeArr', Benchmark(JsonEncodeArray))
print('JsonEncodeObj', Benchmark(JsonEncodeObject))

View file

@ -1,4 +1,3 @@
--
-- Nicolas Seriot's JSONTestSuite
-- https://github.com/nst/JSONTestSuite
@ -31,211 +30,225 @@
-- ljson should reject all of them as invalid
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_no-colon.json
assert(false == pcall(DecodeJson, ' {"a" '))
assert(not DecodeJson(' {"a" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_missing_value.json
assert(false == pcall(DecodeJson, ' {"a": '))
assert(not DecodeJson(' {"a": '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_missing_key.json
assert(false == pcall(DecodeJson, ' {:"b"} '))
assert(not DecodeJson(' {:"b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_missing_colon.json
assert(false == pcall(DecodeJson, ' {"a" b} '))
assert(not DecodeJson(' {"a" b} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_key_with_single_quotes.json
assert(false == pcall(DecodeJson, ' {key: \'value\'} '))
assert(not DecodeJson(' {key: \'value\'} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_garbage_at_end.json
assert(false == pcall(DecodeJson, ' {"a":"a" 123} '))
assert(not DecodeJson(' {"a":"a" 123} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_emoji.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\xf0\x9f\x87\xa8\xf0\x9f\x87\xad\x7d '))
assert(not DecodeJson(' \x7b\xf0\x9f\x87\xa8\xf0\x9f\x87\xad\x7d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_bracket_key.json
assert(false == pcall(DecodeJson, ' {[: "x"} '))
assert(not DecodeJson(' {[: "x"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_with_alpha_char.json
assert(false == pcall(DecodeJson, ' [1.8011670033376514H-308] '))
assert(not DecodeJson(' [1.8011670033376514H-308] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_with_alpha.json
assert(false == pcall(DecodeJson, ' [1.2a-3] '))
assert(not DecodeJson(' [1.2a-3] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_starting_with_dot.json
assert(false == pcall(DecodeJson, ' [.123] '))
assert(not DecodeJson(' [.123] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_real_with_invalid_utf8_after_e.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x31\x65\xe5\x5d '))
assert(not DecodeJson(' \x5b\x31\x65\xe5\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_real_garbage_after_e.json
assert(false == pcall(DecodeJson, ' [1ea] '))
assert(not DecodeJson(' [1ea] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_neg_with_garbage_at_end.json
assert(false == pcall(DecodeJson, ' [-1x] '))
assert(not DecodeJson(' [-1x] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_neg_real_without_int_part.json
assert(false == pcall(DecodeJson, ' [-.123] '))
assert(not DecodeJson(' [-.123] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_minus_sign_with_trailing_garbage.json
assert(false == pcall(DecodeJson, ' [-foo] '))
assert(not DecodeJson(' [-foo] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_minus_infinity.json
assert(false == pcall(DecodeJson, ' [-Infinity] '))
assert(not DecodeJson(' [-Infinity] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_invalid-utf-8-in-int.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x30\xe5\x5d '))
assert(not DecodeJson(' \x5b\x30\xe5\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_invalid-utf-8-in-exponent.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x31\x65\x31\xe5\x5d '))
assert(not DecodeJson(' \x5b\x31\x65\x31\xe5\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_invalid-utf-8-in-bigger-int.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x31\x32\x33\xe5\x5d '))
assert(not DecodeJson(' \x5b\x31\x32\x33\xe5\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_invalid-negative-real.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x2d\x31\x32\x33\x2e\x31\x32\x33\x66\x6f\x6f\x5d '))
assert(not DecodeJson(' \x5b\x2d\x31\x32\x33\x2e\x31\x32\x33\x66\x6f\x6f\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_invalid+-.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x30\x65\x2b\x2d\x31\x5d '))
assert(not DecodeJson(' \x5b\x30\x65\x2b\x2d\x31\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_infinity.json
assert(false == pcall(DecodeJson, ' [Infinity] '))
assert(not DecodeJson(' [Infinity] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_hex_2_digits.json
assert(false == pcall(DecodeJson, ' [0x42] '))
assert(not DecodeJson(' [0x42] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_hex_1_digit.json
assert(false == pcall(DecodeJson, ' [0x1] '))
assert(not DecodeJson(' [0x1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_expression.json
assert(false == pcall(DecodeJson, ' [1+2] '))
assert(not DecodeJson(' [1+2] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_U+FF11_fullwidth_digit_one.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\xef\xbc\x91\x5d '))
assert(not DecodeJson(' \x5b\xef\xbc\x91\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_NaN.json
assert(false == pcall(DecodeJson, ' [NaN] '))
assert(not DecodeJson(' [NaN] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_Inf.json
assert(false == pcall(DecodeJson, ' [Inf] '))
assert(not DecodeJson(' [Inf] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_9.e+.json
assert(false == pcall(DecodeJson, ' [9.e+] '))
assert(not DecodeJson(' [9.e+] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_1eE2.json
assert(false == pcall(DecodeJson, ' [1eE2] '))
assert(not DecodeJson(' [1eE2] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_1.0e.json
assert(false == pcall(DecodeJson, ' [1.0e] '))
assert(not DecodeJson(' [1.0e] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_1.0e-.json
assert(false == pcall(DecodeJson, ' [1.0e-] '))
assert(not DecodeJson(' [1.0e-] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_1.0e+.json
assert(false == pcall(DecodeJson, ' [1.0e+] '))
assert(not DecodeJson(' [1.0e+] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0e.json
assert(false == pcall(DecodeJson, ' [0e] '))
assert(not DecodeJson(' [0e] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0e+.json
assert(false == pcall(DecodeJson, ' [0e+] '))
assert(not DecodeJson(' [0e+] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0_capital_E.json
assert(false == pcall(DecodeJson, ' [0E] '))
assert(not DecodeJson(' [0E] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0_capital_E+.json
assert(false == pcall(DecodeJson, ' [0E+] '))
assert(not DecodeJson(' [0E+] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0.3e.json
assert(false == pcall(DecodeJson, ' [0.3e] '))
assert(not DecodeJson(' [0.3e] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0.3e+.json
assert(false == pcall(DecodeJson, ' [0.3e+] '))
assert(not DecodeJson(' [0.3e+] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0.1.2.json
assert(false == pcall(DecodeJson, ' [0.1.2] '))
assert(not DecodeJson(' [0.1.2] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_.2e-3.json
assert(false == pcall(DecodeJson, ' [.2e-3] '))
assert(not DecodeJson(' [.2e-3] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_.-1.json
assert(false == pcall(DecodeJson, ' [.-1] '))
assert(not DecodeJson(' [.-1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_-NaN.json
assert(false == pcall(DecodeJson, ' [-NaN] '))
assert(not DecodeJson(' [-NaN] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_-1.0..json
assert(false == pcall(DecodeJson, ' [-1.0.] '))
assert(not DecodeJson(' [-1.0.] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_+Inf.json
assert(false == pcall(DecodeJson, ' [+Inf] '))
assert(not DecodeJson(' [+Inf] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_+1.json
assert(false == pcall(DecodeJson, ' [+1] '))
assert(not DecodeJson(' [+1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_++.json
assert(false == pcall(DecodeJson, ' [++1234] '))
assert(not DecodeJson(' [++1234] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_incomplete_true.json
assert(false == pcall(DecodeJson, ' [tru] '))
assert(not DecodeJson(' [tru] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_incomplete_null.json
assert(false == pcall(DecodeJson, ' [nul] '))
assert(not DecodeJson(' [nul] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_incomplete_false.json
assert(false == pcall(DecodeJson, ' [fals] '))
assert(not DecodeJson(' [fals] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_unclosed_with_object_inside.json
assert(false == pcall(DecodeJson, ' [{} '))
assert(not DecodeJson(' [{} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_unclosed_with_new_lines.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[1,
1
,1 ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_unclosed_trailing_comma.json
assert(false == pcall(DecodeJson, ' [1, '))
assert(not DecodeJson(' [1, '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_unclosed.json
assert(false == pcall(DecodeJson, ' ["" '))
assert(not DecodeJson(' ["" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_star_inside.json
assert(false == pcall(DecodeJson, ' [*] '))
assert(not DecodeJson(' [*] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_spaces_vertical_tab_formfeed.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x0b\x61\x22\x5c\x66\x5d '))
assert(not DecodeJson(' \x5b\x22\x0b\x61\x22\x5c\x66\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_newlines_unclosed.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
["a",
4
,1, ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_items_separated_by_semicolon.json
assert(false == pcall(DecodeJson, ' [1:2] '))
assert(not DecodeJson(' [1:2] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_invalid_utf8.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\xff\x5d '))
assert(not DecodeJson(' \x5b\xff\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_incomplete_invalid_value.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x78 '))
assert(not DecodeJson(' \x5b\x78 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_incomplete.json
assert(false == pcall(DecodeJson, ' ["x" '))
assert(not DecodeJson(' ["x" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_colon_instead_of_comma.json
assert(false == pcall(DecodeJson, ' ["": 1] '))
assert(not DecodeJson(' ["": 1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_a_invalid_utf8.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x61\xe5\x5d '))
assert(not DecodeJson(' \x5b\x61\xe5\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_comma_instead_of_colon.json
assert(not DecodeJson(' {"x", null} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_bad_value.json
assert(not DecodeJson(' ["x", truth] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_multidigit_number_then_00.json
assert(not DecodeJson(' 123\x00 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_extra_close.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(not DecodeJson([[
[ "x"] ] ]]))

View file

@ -1,4 +1,3 @@
--
-- Nicolas Seriot's JSONTestSuite
-- https://github.com/nst/JSONTestSuite
@ -33,124 +32,156 @@
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_whitespace_formfeed.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x0c\x5d '))
assert(not DecodeJson(' \x5b\x0c\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_whitespace_U+2060_word_joiner.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\xe2\x81\xa0\x5d '))
assert(not DecodeJson(' \x5b\xe2\x81\xa0\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unicode-identifier.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \xc3\xa5 '))
assert(not DecodeJson(' \xc3\xa5 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unclosed_object.json
assert(false == pcall(DecodeJson, ' {"asd":"asd" '))
assert(not DecodeJson(' {"asd":"asd" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unclosed_array_unfinished_true.json
assert(false == pcall(DecodeJson, ' [ false, tru '))
assert(not DecodeJson(' [ false, tru '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unclosed_array_unfinished_false.json
assert(false == pcall(DecodeJson, ' [ true, fals '))
assert(not DecodeJson(' [ true, fals '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unclosed_array_partial_null.json
assert(false == pcall(DecodeJson, ' [ false, nul '))
assert(not DecodeJson(' [ false, nul '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_unclosed_array.json
assert(false == pcall(DecodeJson, ' [1 '))
assert(not DecodeJson(' [1 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_uescaped_LF_before_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x5c\x75\x30\x30\x30\x41\x22\x22\x5d '))
assert(not DecodeJson(' \x5b\x5c\x75\x30\x30\x30\x41\x22\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_single_star.json
assert(false == pcall(DecodeJson, ' * '))
assert(not DecodeJson(' * '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_single_eacute.json
assert(false == pcall(DecodeJson, ' é '))
assert(not DecodeJson(' é '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_open.json
assert(false == pcall(DecodeJson, [[ ["{["{["{["{ ]]))
assert(not DecodeJson([[ ["{["{["{["{ ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object_string_with_apostrophes.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\x27\x61\x27 '))
assert(not DecodeJson(' \x7b\x27\x61\x27 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object_open_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\x22\x61 '))
assert(not DecodeJson(' \x7b\x22\x61 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object_open_array.json
assert(false == pcall(DecodeJson, ' {[ '))
assert(not DecodeJson(' {[ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object_comma.json
assert(false == pcall(DecodeJson, ' {, '))
assert(not DecodeJson(' {, '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object_close_array.json
assert(false == pcall(DecodeJson, ' {] '))
assert(not DecodeJson(' {] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_object.json
assert(false == pcall(DecodeJson, ' { '))
assert(not DecodeJson(' { '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x61\x22 '))
assert(not DecodeJson(' \x5b\x22\x61\x22 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_open_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x61 '))
assert(not DecodeJson(' \x5b\x22\x61 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_open_object.json
assert(false == pcall(DecodeJson, ' [{ '))
assert(not DecodeJson(' [{ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_comma.json
assert(false == pcall(DecodeJson, ' [, '))
assert(not DecodeJson(' [, '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_apostrophe.json
assert(false == pcall(DecodeJson, ' [\' '))
assert(not DecodeJson(' [\' '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_with_comment.json
assert(false == pcall(DecodeJson, ' {"a":/*comment*/"b"} '))
assert(not DecodeJson(' {"a":/*comment*/"b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_unclosed_no_value.json
assert(false == pcall(DecodeJson, ' {"": '))
assert(not DecodeJson(' {"": '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_null-byte-outside-string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x00\x5d '))
assert(not DecodeJson(' \x5b\x00\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_lone-open-bracket.json
assert(false == pcall(DecodeJson, ' [ '))
assert(not DecodeJson(' [ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_lone-invalid-utf-8.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \xe5 '))
assert(not DecodeJson(' \xe5 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_incomplete_UTF8_BOM.json
assert(false == pcall(DecodeJson, ' ï»{} '))
assert(not DecodeJson(' ï»{} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_comma_instead_of_closing_brace.json
assert(false == pcall(DecodeJson, ' {"x": true, '))
assert(not DecodeJson(' {"x": true, '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_capitalized_True.json
assert(false == pcall(DecodeJson, ' [True] '))
assert(not DecodeJson(' [True] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_ascii-unicode-identifier.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x61\xc3\xa5 '))
assert(not DecodeJson(' \x61\xc3\xa5 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_array_with_unclosed_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x61\x73\x64\x5d '))
assert(not DecodeJson(' \x5b\x22\x61\x73\x64\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_angle_bracket_null.json
assert(false == pcall(DecodeJson, ' [<null>] '))
assert(not DecodeJson(' [<null>] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_angle_bracket_..json
assert(false == pcall(DecodeJson, ' <.> '))
assert(not DecodeJson(' <.> '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_UTF8_BOM_no_data.json
assert(false == pcall(DecodeJson, '  '))
assert(not DecodeJson('  '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_U+2060_word_joined.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\xe2\x81\xa0\x5d '))
assert(not DecodeJson(' \x5b\xe2\x81\xa0\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_trailing_#.json
assert(not DecodeJson(' {"a":"b"}#{} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_with_trailing_garbage.json
assert(not DecodeJson(' {"a": true} "x" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_followed_by_closing_object.json
assert(not DecodeJson(' {}} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_number_with_trailing_garbage.json
assert(not DecodeJson(' 2@ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_no_data.json
assert(not DecodeJson(' '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_end_array.json
assert(not DecodeJson(' ] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_double_array.json
assert(not DecodeJson(' [][] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_close_unopened_array.json
assert(not DecodeJson(' 1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_array_with_extra_array_close.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(not DecodeJson([[
[ 1] ] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_array_trailing_garbage.json
assert(not DecodeJson(' [1]x '))

View file

@ -32,7 +32,7 @@
-- ljson should reject all of them as invalid
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_open_array_object.json
assert(false == pcall(DecodeJson, [[ [{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
assert(not DecodeJson([[ [{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
@ -3161,7 +3161,7 @@ assert(false == pcall(DecodeJson, [[ [{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"":[{"
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_100000_opening_arrays.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [
[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [
[ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [

View file

@ -32,68 +32,145 @@
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_start_escape_unclosed.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c '))
assert(not DecodeJson(' \x5b\x22\x5c '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_single_string_no_double_quotes.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x61\x62\x63 '))
assert(not DecodeJson(' \x61\x62\x63 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_single_quote.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x27\x73\x69\x6e\x67\x6c\x65\x20\x71\x75\x6f\x74\x65\x27\x5d '))
assert(not DecodeJson(' \x5b\x27\x73\x69\x6e\x67\x6c\x65\x20\x71\x75\x6f\x74\x65\x27\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_no_quotes_with_bad_escape.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x5c\x6e\x5d '))
assert(not DecodeJson(' \x5b\x5c\x6e\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_leading_uescaped_thinspace.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x5c\x75\x30\x30\x32\x30\x22\x61\x73\x64\x22\x5d '))
assert(not DecodeJson(' \x5b\x5c\x75\x30\x30\x32\x30\x22\x61\x73\x64\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_surrogate_escape_invalid.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x44\x38\x30\x30\x5c\x78\x22\x5d '))
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x44\x38\x30\x30\x5c\x78\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_escape.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c\x22\x5d '))
assert(not DecodeJson(' \x5b\x22\x5c\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escaped_backslash_bad.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c\x5c\x5c\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escape_x.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c\x78\x30\x30\x22\x5d '))
assert(not DecodeJson(' \x5b\x22\x5c\x5c\x5c\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_accentuated_char_no_quotes.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\xc3\xa9\x5d '))
assert(not DecodeJson(' \x5b\xc3\xa9\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x22\x5d '))
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_with_single_string.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\x20\x22\x66\x6f\x6f\x22\x20\x3a\x20\x22\x62\x61\x72\x22\x2c\x20\x22\x61\x22\x20\x7d '))
assert(not DecodeJson(' \x7b\x20\x22\x66\x6f\x6f\x22\x20\x3a\x20\x22\x62\x61\x72\x22\x2c\x20\x22\x61\x22\x20\x7d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_unterminated-value.json
assert(false == pcall(DecodeJson, ' {"a":"a '))
assert(not DecodeJson(' {"a":"a '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_unquoted_key.json
assert(false == pcall(DecodeJson, ' {a: "b"} '))
assert(not DecodeJson(' {a: "b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_single_quote.json
assert(false == pcall(DecodeJson, ' {\'a\':0} '))
assert(not DecodeJson(' {\'a\':0} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_repeated_null_null.json
assert(false == pcall(DecodeJson, ' {null:null,null:null} '))
assert(not DecodeJson(' {null:null,null:null} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_non_string_key_but_huge_number_instead.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\x39\x39\x39\x39\x45\x39\x39\x39\x39\x3a\x31\x7d '))
assert(not DecodeJson(' \x7b\x39\x39\x39\x39\x45\x39\x39\x39\x39\x3a\x31\x7d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_non_string_key.json
-- (converted to binary for safety)
assert(false == pcall(DecodeJson, ' \x7b\x31\x3a\x31\x7d '))
assert(not DecodeJson(' \x7b\x31\x3a\x31\x7d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_comma_after_close.json
assert(not DecodeJson(' [""], '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_backslash_00.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x00\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escaped_emoji.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\xf0\x9f\x8c\x80\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escaped_ctrl_char_tab.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x09\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_surrogate.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x33\x34\x5c\x75\x44\x64\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_escaped_character.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x30\x30\x41\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_single_doublequote.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x22 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_utf8_after_escape.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\xe5\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_unicode_escape.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x71\x71\x71\x71\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_backslash_esc.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x61\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid-utf-8-in-escape.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\xe5\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u1x.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x31\x78\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u1.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x31\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_unicode_CapitalU.json
-- (converted to binary for safety)
assert(not DecodeJson(' \x22\x5c\x55\x41\x36\x36\x44\x22 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_with_trailing_garbage.json
-- (converted to binary for safety)
assert(not DecodeJson(" \"\"x "))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_single_space.json
assert(not DecodeJson(' '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_with_trailing_garbage.json
assert(not DecodeJson(' {"a":"b"}# '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_slash_open_incomplete.json
assert(not DecodeJson(' {"a":"b"}/ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_slash_open.json
assert(not DecodeJson(' {"a":"b"}// '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_open.json
assert(not DecodeJson(' {"a":"b"}/**// '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment.json
assert(not DecodeJson(' {"a":"b"}/**/ '))

View file

@ -1,4 +1,3 @@
--
-- Nicolas Seriot's JSONTestSuite
-- https://github.com/nst/JSONTestSuite
@ -27,241 +26,111 @@
-- SOFTWARE.
--
-- these test cases are prefixed with n_, but
-- ljson doesn't reject any of them as invalid
-- we run these tests anyway just to ensure that
-- no segfaults occurs while parsing these cases
-- from fail4.lua
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_backslash_00.json
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escape_x.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x00\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escaped_emoji.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\xf0\x9f\x8c\x80\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_escaped_ctrl_char_tab.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x09\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_surrogate.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x33\x34\x5c\x75\x44\x64\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_incomplete_escaped_character.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x30\x30\x41\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_single_doublequote.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x22 '))
assert(DecodeJson(" [\"\\x00\"] "))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_unescaped_tab.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x09\x22\x5d '))
assert(DecodeJson([[ [" "] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_unescaped_newline.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x6e\x65\x77\x0a\x6c\x69\x6e\x65\x22\x5d '))
assert(DecodeJson(" [\"new\nline\"] "))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_unescaped_ctrl_char.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x61\x00\x61\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_utf8_after_escape.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\xe5\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_unicode_escape.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x71\x71\x71\x71\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid_backslash_esc.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x61\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_invalid-utf-8-in-escape.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\xe5\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u1x.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x31\x78\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u1.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x31\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_1_surrogate_then_escape_u.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x5b\x22\x5c\x75\x44\x38\x30\x30\x5c\x75\x22\x5d '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_unicode_CapitalU.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x22\x5c\x55\x41\x36\x36\x44\x22 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_string_with_trailing_garbage.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x22\x22\x78 '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_single_space.json
assert(nil ~= pcall(DecodeJson, ' '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_with_trailing_garbage.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}# '))
assert(assert(EncodeLua(assert(DecodeJson(" [\"a\x00a\"] ")))) == assert(EncodeLua({"a\x00a"})))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_two_commas_in_a_row.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b",,"c":"d"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_slash_open_incomplete.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}/ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_slash_open.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}// '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment_open.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}/**// '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comment.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}/**/ '))
assert(assert(EncodeLua(assert(DecodeJson(' {"a":"b",,"c":"d"} ')))) == assert(EncodeLua({a="b", c="d"})))
assert(DecodeJson(' {"a":"b",,"c":"d"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_trailing_comma.json
assert(nil ~= pcall(DecodeJson, ' {"id":0,} '))
assert(DecodeJson(' {"id":0,} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_several_trailing_commas.json
assert(nil ~= pcall(DecodeJson, ' {"id":0,,,,,} '))
-- from fail2.lua
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_trailing_#.json
assert(nil ~= pcall(DecodeJson, ' {"a":"b"}#{} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_with_trailing_garbage.json
assert(nil ~= pcall(DecodeJson, ' {"a": true} "x" '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_object_followed_by_closing_object.json
assert(nil ~= pcall(DecodeJson, ' {}} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_number_with_trailing_garbage.json
assert(nil ~= pcall(DecodeJson, ' 2@ '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_no_data.json
assert(nil ~= pcall(DecodeJson, ' '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_end_array.json
assert(nil ~= pcall(DecodeJson, ' ] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_double_array.json
assert(nil ~= pcall(DecodeJson, ' [][] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_close_unopened_array.json
assert(nil ~= pcall(DecodeJson, ' 1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_array_with_extra_array_close.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(nil ~= pcall(DecodeJson, [[
[ 1] ] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_structure_array_trailing_garbage.json
assert(nil ~= pcall(DecodeJson, ' [1]x '))
assert(DecodeJson(' {"id":0,,,,,} '))
-- from fail1.lua
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json
-- (converted to binary for safety)
assert(nil ~= pcall(DecodeJson, ' \x7b\x22\xb9\x22\x3a\x22\x30\x22\x2c\x7d '))
assert(DecodeJson(" {\"\xb9\":\"0\",} "))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_missing_semicolon.json
assert(nil ~= pcall(DecodeJson, ' {"a" "b"} '))
assert(DecodeJson(' {"a" "b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_double_colon.json
assert(nil ~= pcall(DecodeJson, ' {"x"::"b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_comma_instead_of_colon.json
assert(nil ~= pcall(DecodeJson, ' {"x", null} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_object_bad_value.json
assert(nil ~= pcall(DecodeJson, ' ["x", truth] '))
assert(DecodeJson(' {"x"::"b"} '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_with_leading_zero.json
assert(nil ~= pcall(DecodeJson, ' [012] '))
assert(DecodeJson(' [012] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_real_without_fractional_part.json
assert(nil ~= pcall(DecodeJson, ' [1.] '))
assert(DecodeJson(' [1.] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_neg_int_starting_with_zero.json
assert(nil ~= pcall(DecodeJson, ' [-012] '))
assert(DecodeJson(' [-012] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_minus_space_1.json
assert(nil ~= pcall(DecodeJson, ' [- 1] '))
assert(DecodeJson(' [- 1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_2.e3.json
assert(nil ~= pcall(DecodeJson, ' [2.e3] '))
assert(DecodeJson(' [2.e3] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_2.e-3.json
assert(nil ~= pcall(DecodeJson, ' [2.e-3] '))
assert(DecodeJson(' [2.e-3] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_2.e+3.json
assert(nil ~= pcall(DecodeJson, ' [2.e+3] '))
assert(DecodeJson(' [2.e+3] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_1_000.json
assert(nil ~= pcall(DecodeJson, ' [1 000.0] '))
assert(DecodeJson(' [1 000.0] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_0.e1.json
assert(nil ~= pcall(DecodeJson, ' [0.e1] '))
assert(DecodeJson(' [0.e1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_-2..json
assert(nil ~= pcall(DecodeJson, ' [-2.] '))
assert(DecodeJson(' [-2.] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_number_-01.json
assert(nil ~= pcall(DecodeJson, ' [-01] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_multidigit_number_then_00.json
assert(nil ~= pcall(DecodeJson, ' 123\x00 '))
assert(DecodeJson(' [-01] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_number_and_several_commas.json
assert(nil ~= pcall(DecodeJson, ' [1,,] '))
assert(DecodeJson(' [1,,] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_number_and_comma.json
assert(nil ~= pcall(DecodeJson, ' [1,] '))
assert(DecodeJson(' [1,] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_missing_value.json
assert(nil ~= pcall(DecodeJson, ' [ , ""] '))
assert(DecodeJson(' [ , ""] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_just_minus.json
assert(nil ~= pcall(DecodeJson, ' [-] '))
assert(DecodeJson(' [-] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_just_comma.json
assert(nil ~= pcall(DecodeJson, ' [,] '))
assert(DecodeJson(' [,] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_inner_array_no_comma.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ 3[ 4] ] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_extra_comma.json
assert(nil ~= pcall(DecodeJson, ' ["",] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_extra_close.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(nil ~= pcall(DecodeJson, [[
[ "x"] ] ]]))
assert(DecodeJson(' ["",] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_double_extra_comma.json
assert(nil ~= pcall(DecodeJson, ' ["x",,] '))
assert(DecodeJson(' ["x",,] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_double_comma.json
assert(nil ~= pcall(DecodeJson, ' [1,,2] '))
assert(DecodeJson(' [1,,2] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_comma_and_number.json
assert(nil ~= pcall(DecodeJson, ' [,1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_comma_after_close.json
assert(nil ~= pcall(DecodeJson, ' [""], '))
assert(DecodeJson(' [,1] '))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/n_array_1_true_without_comma.json
assert(nil ~= pcall(DecodeJson, ' [1 true] '))
assert(DecodeJson(' [1 true] '))

View file

@ -32,294 +32,294 @@
-- ljson should accept all of them as valid
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_whitespace_array.json
assert(true == pcall(DecodeJson, [[ [] ]]))
assert(DecodeJson([[ [] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_true_in_array.json
assert(true == pcall(DecodeJson, [[ [true] ]]))
assert(DecodeJson([[ [true] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_trailing_newline.json
assert(true == pcall(DecodeJson, [[ ["a"] ]]))
assert(DecodeJson([[ ["a"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_string_empty.json
assert(true == pcall(DecodeJson, [[ "" ]]))
assert(DecodeJson([[ "" ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_true.json
assert(true == pcall(DecodeJson, [[ true ]]))
assert(DecodeJson([[ true ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_string.json
assert(true == pcall(DecodeJson, [[ "asd" ]]))
assert(DecodeJson([[ "asd" ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_null.json
assert(true == pcall(DecodeJson, [[ null ]]))
assert(not DecodeJson([[ null ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_negative_real.json
assert(true == pcall(DecodeJson, [[ -0.1 ]]))
assert(DecodeJson([[ -0.1 ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_int.json
assert(true == pcall(DecodeJson, [[ 42 ]]))
assert(DecodeJson([[ 42 ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_structure_lonely_false.json
assert(true == pcall(DecodeJson, [[ false ]]))
assert(not DecodeJson([[ false ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_with_del_character.json
assert(true == pcall(DecodeJson, [[ ["aa"] ]]))
assert(DecodeJson([[ ["aa"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_utf8.json
assert(true == pcall(DecodeJson, [[ ["€𝄞"] ]]))
assert(DecodeJson([[ ["€𝄞"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_escaped_double_quote.json
assert(true == pcall(DecodeJson, [[ ["\u0022"] ]]))
assert(DecodeJson([[ ["\u0022"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+FFFE_nonchar.json
assert(true == pcall(DecodeJson, [[ ["\uFFFE"] ]]))
assert(DecodeJson([[ ["\uFFFE"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+FDD0_nonchar.json
assert(true == pcall(DecodeJson, [[ ["\uFDD0"] ]]))
assert(DecodeJson([[ ["\uFDD0"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+2064_invisible_plus.json
assert(true == pcall(DecodeJson, [[ ["\u2064"] ]]))
assert(DecodeJson([[ ["\u2064"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json
assert(true == pcall(DecodeJson, [[ ["\u200B"] ]]))
assert(DecodeJson([[ ["\u200B"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+1FFFE_nonchar.json
assert(true == pcall(DecodeJson, [[ ["\uD83F\uDFFE"] ]]))
assert(DecodeJson([[ ["\uD83F\uDFFE"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_U+10FFFE_nonchar.json
assert(true == pcall(DecodeJson, [[ ["\uDBFF\uDFFE"] ]]))
assert(DecodeJson([[ ["\uDBFF\uDFFE"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode_2.json
assert(true == pcall(DecodeJson, [[ ["⍂㈴⍂"] ]]))
assert(DecodeJson([[ ["⍂㈴⍂"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicodeEscapedBackslash.json
assert(true == pcall(DecodeJson, [[ ["\u005C"] ]]))
assert(DecodeJson([[ ["\u005C"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unicode.json
assert(true == pcall(DecodeJson, [[ ["\uA66D"] ]]))
assert(DecodeJson([[ ["\uA66D"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_unescaped_char_delete.json
assert(true == pcall(DecodeJson, [[ [""] ]]))
assert(DecodeJson([[ [""] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_uescaped_newline.json
assert(true == pcall(DecodeJson, [[ ["new\u000Aline"] ]]))
assert(DecodeJson([[ ["new\u000Aline"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_uEscape.json
assert(true == pcall(DecodeJson, [[ ["\u0061\u30af\u30EA\u30b9"] ]]))
assert(DecodeJson([[ ["\u0061\u30af\u30EA\u30b9"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_u+2029_par_sep.json
assert(true == pcall(DecodeJson, [[ [""] ]]))
assert(DecodeJson([[ [""] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_u+2028_line_sep.json
assert(true == pcall(DecodeJson, [[ [""] ]]))
assert(DecodeJson([[ [""] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_two-byte-utf-8.json
assert(true == pcall(DecodeJson, [[ ["\u0123"] ]]))
assert(DecodeJson([[ ["\u0123"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_three-byte-utf-8.json
assert(true == pcall(DecodeJson, [[ ["\u0821"] ]]))
assert(DecodeJson([[ ["\u0821"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json
assert(true == pcall(DecodeJson, [[ ["\uD834\uDd1e"] ]]))
assert(DecodeJson([[ ["\uD834\uDd1e"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_space.json
assert(true == pcall(DecodeJson, [[ " " ]]))
assert(DecodeJson([[ " " ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_simple_ascii.json
assert(true == pcall(DecodeJson, [[ ["asd "] ]]))
assert(DecodeJson([[ ["asd "] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_reservedCharacterInUTF-8_U+1BFFF.json
assert(true == pcall(DecodeJson, [[ ["𛿿"] ]]))
assert(DecodeJson([[ ["𛿿"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_pi.json
assert(true == pcall(DecodeJson, [[ ["π"] ]]))
assert(DecodeJson([[ ["π"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_one-byte-utf-8.json
assert(true == pcall(DecodeJson, [[ ["\u002c"] ]]))
assert(DecodeJson([[ ["\u002c"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_null_escape.json
assert(true == pcall(DecodeJson, [[ ["\u0000"] ]]))
assert(DecodeJson([[ ["\u0000"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json
assert(true == pcall(DecodeJson, [[ ["￿"] ]]))
assert(DecodeJson([[ ["￿"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json
assert(true == pcall(DecodeJson, [[ ["􏿿"] ]]))
assert(DecodeJson([[ ["􏿿"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_nbsp_uescaped.json
assert(true == pcall(DecodeJson, [[ ["new\u00A0line"] ]]))
assert(DecodeJson([[ ["new\u00A0line"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_last_surrogates_1_and_2.json
assert(true == pcall(DecodeJson, [[ ["\uDBFF\uDFFF"] ]]))
assert(DecodeJson([[ ["\uDBFF\uDFFF"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_in_array_with_leading_space.json
assert(true == pcall(DecodeJson, [[ [ "asd"] ]]))
assert(DecodeJson([[ [ "asd"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_in_array.json
assert(true == pcall(DecodeJson, [[ ["asd"] ]]))
assert(DecodeJson([[ ["asd"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_escaped_noncharacter.json
assert(true == pcall(DecodeJson, [[ ["\uFFFF"] ]]))
assert(DecodeJson([[ ["\uFFFF"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_escaped_control_character.json
assert(true == pcall(DecodeJson, [[ ["\u0012"] ]]))
assert(DecodeJson([[ ["\u0012"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_double_escape_n.json
assert(true == pcall(DecodeJson, [[ ["\\n"] ]]))
assert(DecodeJson([[ ["\\n"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_double_escape_a.json
assert(true == pcall(DecodeJson, [[ ["\\a"] ]]))
assert(DecodeJson([[ ["\\a"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_comments.json
assert(true == pcall(DecodeJson, [[ ["a/*b*/c/*d//e"] ]]))
assert(DecodeJson([[ ["a/*b*/c/*d//e"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_backslash_doublequotes.json
assert(true == pcall(DecodeJson, [[ ["\""] ]]))
assert(DecodeJson([[ ["\""] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_backslash_and_u_escaped_zero.json
assert(true == pcall(DecodeJson, [[ ["\\u0000"] ]]))
assert(DecodeJson([[ ["\\u0000"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_allowed_escapes.json
assert(true == pcall(DecodeJson, [[ ["\"\\\/\b\f\n\r\t"] ]]))
assert(DecodeJson([[ ["\"\\\/\b\f\n\r\t"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_accepted_surrogate_pairs.json
assert(true == pcall(DecodeJson, [[ ["\ud83d\ude39\ud83d\udc8d"] ]]))
assert(DecodeJson([[ ["\ud83d\ude39\ud83d\udc8d"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_accepted_surrogate_pair.json
assert(true == pcall(DecodeJson, [[ ["\uD801\udc37"] ]]))
assert(DecodeJson([[ ["\uD801\udc37"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json
assert(true == pcall(DecodeJson, [[ ["\u0060\u012a\u12AB"] ]]))
assert(DecodeJson([[ ["\u0060\u012a\u12AB"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_with_newlines.json
assert(true == pcall(DecodeJson, [[
assert(DecodeJson([[
{
"a": "b"
} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_string_unicode.json
assert(true == pcall(DecodeJson, [[ {"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } ]]))
assert(DecodeJson([[ {"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_simple.json
assert(true == pcall(DecodeJson, [[ {"a":[]} ]]))
assert(DecodeJson([[ {"a":[]} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_long_strings.json
assert(true == pcall(DecodeJson, [[ {"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} ]]))
assert(DecodeJson([[ {"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_extreme_numbers.json
assert(true == pcall(DecodeJson, [[ { "min": -1.0e+28, "max": 1.0e+28 } ]]))
assert(DecodeJson([[ { "min": -1.0e+28, "max": 1.0e+28 } ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_escaped_null_in_key.json
assert(true == pcall(DecodeJson, [[ {"foo\u0000bar": 42} ]]))
assert(DecodeJson([[ {"foo\u0000bar": 42} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_empty_key.json
assert(true == pcall(DecodeJson, [[ {"":0} ]]))
assert(DecodeJson([[ {"":0} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_empty.json
assert(true == pcall(DecodeJson, [[ {} ]]))
assert(DecodeJson([[ {} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_duplicated_key_and_value.json
assert(true == pcall(DecodeJson, [[ {"a":"b","a":"b"} ]]))
assert(DecodeJson([[ {"a":"b","a":"b"} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_duplicated_key.json
assert(true == pcall(DecodeJson, [[ {"a":"b","a":"c"} ]]))
assert(DecodeJson([[ {"a":"b","a":"c"} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object_basic.json
assert(true == pcall(DecodeJson, [[ {"asd":"sdf"} ]]))
assert(DecodeJson([[ {"asd":"sdf"} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_object.json
assert(true == pcall(DecodeJson, [[ {"asd":"sdf", "dfg":"fgh"} ]]))
assert(DecodeJson([[ {"asd":"sdf", "dfg":"fgh"} ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_simple_real.json
assert(true == pcall(DecodeJson, [[ [123.456789] ]]))
assert(DecodeJson([[ [123.456789] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_simple_int.json
assert(true == pcall(DecodeJson, [[ [123] ]]))
assert(DecodeJson([[ [123] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_pos_exponent.json
assert(true == pcall(DecodeJson, [[ [1e+2] ]]))
assert(DecodeJson([[ [1e+2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_neg_exp.json
assert(true == pcall(DecodeJson, [[ [1e-2] ]]))
assert(DecodeJson([[ [1e-2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_fraction_exponent.json
assert(true == pcall(DecodeJson, [[ [123.456e78] ]]))
assert(DecodeJson([[ [123.456e78] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_exponent.json
assert(true == pcall(DecodeJson, [[ [123e45] ]]))
assert(DecodeJson([[ [123e45] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_capital_e_pos_exp.json
assert(true == pcall(DecodeJson, [[ [1E+2] ]]))
assert(DecodeJson([[ [1E+2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_capital_e_neg_exp.json
assert(true == pcall(DecodeJson, [[ [1E-2] ]]))
assert(DecodeJson([[ [1E-2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_real_capital_e.json
assert(true == pcall(DecodeJson, [[ [1E22] ]]))
assert(DecodeJson([[ [1E22] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_negative_zero.json
assert(true == pcall(DecodeJson, [[ [-0] ]]))
assert(DecodeJson([[ [-0] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_negative_one.json
assert(true == pcall(DecodeJson, [[ [-1] ]]))
assert(DecodeJson([[ [-1] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_negative_int.json
assert(true == pcall(DecodeJson, [[ [-123] ]]))
assert(DecodeJson([[ [-123] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_minus_zero.json
assert(true == pcall(DecodeJson, [[ [-0] ]]))
assert(DecodeJson([[ [-0] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_int_with_exp.json
assert(true == pcall(DecodeJson, [[ [20e1] ]]))
assert(DecodeJson([[ [20e1] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_double_close_to_zero.json
assert(true == pcall(DecodeJson, [[ [-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] ]]))
assert(DecodeJson([[ [-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_after_space.json
assert(true == pcall(DecodeJson, [[ [ 4] ]]))
assert(DecodeJson([[ [ 4] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_0e1.json
assert(true == pcall(DecodeJson, [[ [0e1] ]]))
assert(DecodeJson([[ [0e1] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number_0e+1.json
assert(true == pcall(DecodeJson, [[ [0e+1] ]]))
assert(DecodeJson([[ [0e+1] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_number.json
assert(true == pcall(DecodeJson, [[ [123e65] ]]))
assert(DecodeJson([[ [123e65] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_with_trailing_space.json
assert(true == pcall(DecodeJson, [[ [2] ]]))
assert(DecodeJson([[ [2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_with_several_null.json
assert(true == pcall(DecodeJson, [[ [1,null,null,null,2] ]]))
assert(DecodeJson([[ [1,null,null,null,2] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_with_leading_space.json
assert(true == pcall(DecodeJson, [[ [1] ]]))
assert(DecodeJson([[ [1] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_with_1_and_newline.json
assert(true == pcall(DecodeJson, [[
assert(DecodeJson([[
[1
] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_null.json
assert(true == pcall(DecodeJson, [[ [null] ]]))
assert(DecodeJson([[ [null] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_heterogeneous.json
assert(true == pcall(DecodeJson, [[ [null, 1, "1", {}] ]]))
assert(DecodeJson([[ [null, 1, "1", {}] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_false.json
assert(true == pcall(DecodeJson, [[ [false] ]]))
assert(DecodeJson([[ [false] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_ending_with_newline.json
assert(true == pcall(DecodeJson, [[ ["a"] ]]))
assert(DecodeJson([[ ["a"] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_empty.json
assert(true == pcall(DecodeJson, [[ [] ]]))
assert(DecodeJson([[ [] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_empty-string.json
assert(true == pcall(DecodeJson, [[ [""] ]]))
assert(DecodeJson([[ [""] ]]))
-- https://github.com/nst/JSONTestSuite/tree/d64aefb55228d9584d3e5b2433f720ea8fd00c82/test_parsing/y_array_arraysWithSpaces.json
-- (added spaces between [[ and ]] so lua doesn't get confused)
assert(true == pcall(DecodeJson, [[
assert(DecodeJson([[
[ [ ] ] ]]))

View file

@ -1,101 +1,132 @@
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail11.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{"Illegal expression": 1 + 2}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail12.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{"Illegal invocation": alert()}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail13.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{"Numbers cannot have leading zeroes": 013}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail14.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{"Numbers cannot be hex": 0x14}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail15.json
assert(false == pcall(DecodeJson, [[
[ "Illegal backslash escape: \x15"]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail16.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ \naked]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail17.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ "Illegal backslash escape: \017"]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail22.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ "Colon instead of comma": false]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail23.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ "Bad value", truth]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail24.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ 'single quote']
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail29.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ 0e]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail2.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ "Unclosed array"
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail30.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ 0e+]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail31.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ 0e+-1]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail32.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{"Comma instead if closing brace": true,
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail33.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
[ "mismatch"}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail3.json
assert(false == pcall(DecodeJson, [[
assert(not DecodeJson([[
{unquoted_key: "keys must be quoted"}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail21.json
assert(not DecodeJson([[
{"Comma instead of colon", null}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail8.json
assert(not DecodeJson([[
[ "Extra close"] ]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail7.json
assert(not DecodeJson([[
[ "Comma after the close"] ,
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail10.json
assert(not DecodeJson([[
{"Extra value after close": true} "misplaced quoted value"
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail26.json
assert(not DecodeJson([[
[ "tab\ character\ in\ string\ "]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail28.json
assert(not DecodeJson([[
[ "line\
break"]
]]))

View file

@ -5,86 +5,55 @@
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail4.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ "extra comma",]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail5.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ "double extra comma",,]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail6.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ , "<-- missing value"]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail7.json
assert(nil ~= pcall(DecodeJson, [[
[ "Comma after the close"] ,
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail8.json
assert(nil ~= pcall(DecodeJson, [[
[ "Extra close"] ]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail9.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
{"Extra comma": true,}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail19.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
{"Missing colon" null}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail20.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
{"Double colon":: null}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail21.json
assert(nil ~= pcall(DecodeJson, [[
{"Comma instead of colon", null}
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail10.json
assert(nil ~= pcall(DecodeJson, [[
{"Extra value after close": true} "misplaced quoted value"
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail25.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ " tab character in string "]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail26.json
assert(nil ~= pcall(DecodeJson, [[
[ "tab\ character\ in\ string\ "]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail27.json
assert(nil ~= pcall(DecodeJson, [[
assert(DecodeJson([[
[ "line
break"]
]]))
-- https://www.json.org/JSON_checker/test.zip
-- JSON parsing sample test case: fail28.json
assert(nil ~= pcall(DecodeJson, [[
[ "line\
break"]
-- JSON parsing sample test case: fail15.json
assert(DecodeJson([[
[ "Illegal backslash escape: \x15"]
]]))

View file

@ -28,12 +28,12 @@ int EscapeLuaString(char *s, size_t len, char **buf) {
size_t i;
RETURN_ON_ERROR(appendw(buf, '"'));
for (i = 0; i < len; i++) {
if (' ' <= s[i] && s[i] <= 0x7e) {
RETURN_ON_ERROR(appendw(buf, s[i]));
} else if (s[i] == '\n') {
if (s[i] == '\n') {
RETURN_ON_ERROR(appendw(buf, '\\' | 'n' << 8));
} else if (s[i] == '\\' || s[i] == '\'' || s[i] == '\"') {
RETURN_ON_ERROR(appendw(buf, '\\' | s[i] << 8));
} else if (' ' <= s[i] && s[i] <= 0x7e) {
RETURN_ON_ERROR(appendw(buf, s[i]));
} else {
RETURN_ON_ERROR(
appendw(buf, '\\' | 'x' << 010 |

View file

@ -19,6 +19,8 @@
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/log.h"
#include "libc/log/rop.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
@ -34,13 +36,14 @@
static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
char *numformat, int idx,
struct LuaVisited *visited) {
struct LuaVisited *visited,
const char **reason) {
char *s;
int sli, rc;
bool isarray;
char ibuf[128];
size_t tbllen, i, z;
struct StrList sl = {0};
char ibuf[128], fmt[] = "%.14g";
if (level > 0) {
switch (lua_type(L, idx)) {
@ -56,7 +59,9 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
case LUA_TSTRING:
s = lua_tolstring(L, idx, &z);
if (!(s = EscapeJsStringLiteral(s, z, &z))) goto OnError;
if (!(s = EscapeJsStringLiteral(s, z, &z))) {
goto OnError;
}
RETURN_ON_ERROR(appendw(buf, '"'));
RETURN_ON_ERROR(appendd(buf, s, z));
RETURN_ON_ERROR(appendw(buf, '"'));
@ -76,19 +81,28 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
case LUA_TTABLE:
RETURN_ON_ERROR(rc = LuaPushVisit(visited, lua_topointer(L, idx)));
if (!rc) {
lua_pushvalue(L, idx); // table ref
tbllen = lua_rawlen(L, -1);
// encode tables with numeric indices and empty tables as arrays
isarray =
tbllen > 0 || // integer keys present
(lua_pushnil(L), !lua_next(L, -2)) || // no non-integer keys
(lua_pop(L, 2), false); // pop key/value pushed by lua_next
// create nearby reference to table at idx
lua_pushvalue(L, idx);
// fast way to tell if table is an array or object
if ((tbllen = lua_rawlen(L, -1)) > 0) {
isarray = true;
} else {
// the json parser inserts `[0]=false` in empty arrays
// so we can tell them apart from empty objects, which
// is needed in order to have `[]` roundtrip the parse
isarray = (lua_rawgeti(L, -1, 0) == LUA_TBOOLEAN &&
!lua_toboolean(L, -1));
lua_pop(L, 1);
}
// now serialize the table
if (isarray) {
for (i = 1; i <= tbllen; i++) {
RETURN_ON_ERROR(sli = AppendStrList(&sl));
lua_rawgeti(L, -1, i); // table/-2, value/-1
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
numformat, -1, visited));
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(
L, &sl.p[sli], level - 1, numformat, -1, visited, reason));
lua_pop(L, 1);
}
} else {
@ -96,20 +110,15 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
lua_pushnil(L); // push the first key
while (lua_next(L, -2)) {
if (lua_type(L, -2) != LUA_TSTRING) {
// json tables must be arrays or use string keys
*reason = "json objects must only use string keys";
goto OnError;
}
// the json parser inserts a `__json_object__` into empty
// objects, so we don't serialize `{}` as `[]` by mistake
// and as such, we should ignore it here, for readability
if (strcmp(luaL_checkstring(L, -2), "__json_object__")) {
RETURN_ON_ERROR(sli = AppendStrList(&sl));
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
numformat, -2, visited));
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
numformat, -1, visited));
}
RETURN_ON_ERROR(sli = AppendStrList(&sl));
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(
L, &sl.p[sli], level - 1, numformat, -2, visited, reason));
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(
L, &sl.p[sli], level - 1, numformat, -1, visited, reason));
lua_pop(L, 1); // table/-2, key/-1
}
// stack: table/-1, as the key was popped by lua_next
@ -121,18 +130,18 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
RETURN_ON_ERROR(appendw(buf, isarray ? ']' : '}'));
LuaPopVisit(visited);
lua_pop(L, 1); // table ref
return 0;
} else {
// cyclic data structure
*reason = "won't serialize cyclic lua table";
goto OnError;
}
default:
// unsupported lua type
*reason = "unsupported lua type";
goto OnError;
}
} else {
// too much depth
*reason = "table has great depth";
goto OnError;
}
OnError:
@ -152,7 +161,12 @@ OnError:
int LuaEncodeJsonData(lua_State *L, char **buf, char *numformat, int idx) {
int rc;
struct LuaVisited visited = {0};
rc = LuaEncodeJsonDataImpl(L, buf, 64, numformat, idx, &visited);
const char *reason = "out of memory";
rc = LuaEncodeJsonDataImpl(L, buf, 64, numformat, idx, &visited, &reason);
free(visited.p);
if (rc == -1) {
lua_pushnil(L);
lua_pushstring(L, reason);
}
return rc;
}

View file

@ -57,6 +57,15 @@ static bool IsLuaArray(lua_State *L) {
return true;
}
static int LuaEncodeLuaOpaqueData(lua_State *L, char **buf, int idx,
const char *kind) {
if (appendf(buf, "\"%s@%p\"", kind, lua_topointer(L, idx)) != -1) {
return 0;
} else {
return -1;
}
}
static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
char *numformat, int idx,
struct LuaVisited *visited) {
@ -80,19 +89,13 @@ static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
return 0;
case LUA_TFUNCTION:
RETURN_ON_ERROR(
appendf(buf, "\"%s@%p\"", "func", lua_topointer(L, idx)));
return 0;
return LuaEncodeLuaOpaqueData(L, buf, idx, "func");
case LUA_TLIGHTUSERDATA:
RETURN_ON_ERROR(
appendf(buf, "\"%s@%p\"", "light", lua_topointer(L, idx)));
return 0;
return LuaEncodeLuaOpaqueData(L, buf, idx, "light");
case LUA_TTHREAD:
RETURN_ON_ERROR(
appendf(buf, "\"%s@%p\"", "thread", lua_topointer(L, idx)));
return 0;
return LuaEncodeLuaOpaqueData(L, buf, idx, "thread");
case LUA_TUSERDATA:
if (luaL_callmeta(L, idx, "__repr")) {
@ -117,9 +120,7 @@ static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
lua_pop(L, 1);
return 0;
}
RETURN_ON_ERROR(
appendf(buf, "\"%s@%p\"", "udata", lua_touserdata(L, idx)));
return 0;
return LuaEncodeLuaOpaqueData(L, buf, idx, "udata");
case LUA_TNUMBER:
if (lua_isinteger(L, idx)) {
@ -174,19 +175,16 @@ static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
RETURN_ON_ERROR(appendw(buf, '}'));
FreeStrList(&sl);
LuaPopVisit(visited);
} else {
RETURN_ON_ERROR(
appendf(buf, "\"%s@%p\"", "cyclic", lua_topointer(L, idx)));
}
return 0;
return 0;
} else {
return LuaEncodeLuaOpaqueData(L, buf, idx, "cyclic");
}
default:
// unsupported lua type
goto OnError;
return LuaEncodeLuaOpaqueData(L, buf, idx, "unsupported");
}
} else {
// too much depth
goto OnError;
return LuaEncodeLuaOpaqueData(L, buf, idx, "greatdepth");
}
OnError:
FreeStrList(&sl);
@ -196,6 +194,13 @@ OnError:
/**
* Encodes Lua data structure as Lua code string.
*
* This serializer is intended primarily for describing the data
* structure. For example, it's used by the REPL where we need to be
* able to ignore errors when displaying data structures, since showing
* most things imperfectly is better than crashing. Therefore this isn't
* the kind of serializer you'd want to use to persist data in prod. Try
* using the JSON serializer for that purpose.
*
* @param L is Lua interpreter state
* @param buf receives encoded output string
* @param numformat controls double formatting
@ -207,5 +212,9 @@ int LuaEncodeLuaData(lua_State *L, char **buf, char *numformat, int idx) {
struct LuaVisited visited = {0};
rc = LuaEncodeLuaDataImpl(L, buf, 64, numformat, idx, &visited);
free(visited.p);
if (rc == -1) {
lua_pushnil(L);
lua_pushstring(L, "out of memory");
}
return rc;
}

View file

@ -17,29 +17,43 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/x/x.h"
#include "libc/mem/mem.h"
#include "third_party/lua/visitor.h"
int LuaPushVisit(struct LuaVisited *visited, const void *p) {
int i, n2;
const void **p2;
for (i = 0; i < visited->n; ++i) {
if (visited->p[i] == p) {
return 1;
static inline bool IsVisited(struct LuaVisited *v, const void *p) {
int i;
for (i = 0; i < v->i; ++i) {
if (v->p[i] == p) {
return true;
}
}
n2 = visited->n;
if ((p2 = realloc(visited->p, ++n2 * sizeof(*visited->p)))) {
visited->p = p2;
visited->n = n2;
} else {
return -1;
return false;
}
static inline int Visit(struct LuaVisited *v, const void *p) {
int n2;
const void **p2;
if (v->i == v->n) {
n2 = v->n;
if (!n2) n2 = 2;
n2 += n2 >> 1;
if ((p2 = realloc(v->p, n2 * sizeof(*p2)))) {
v->p = p2;
v->n = n2;
} else {
return -1;
}
}
visited->p[visited->n - 1] = p;
v->p[v->i++] = p;
return 0;
}
void LuaPopVisit(struct LuaVisited *visited) {
assert(visited->n > 0);
--visited->n;
int LuaPushVisit(struct LuaVisited *v, const void *p) {
if (IsVisited(v, p)) return 1;
return Visit(v, p);
}
void LuaPopVisit(struct LuaVisited *v) {
assert(v->i > 0);
--v->i;
}

View file

@ -4,7 +4,7 @@
COSMOPOLITAN_C_START_
struct LuaVisited {
int n;
int i, n;
const void **p;
};

View file

@ -674,7 +674,12 @@ FUNCTIONS
encodebase64.c.
DecodeJson(input:str)
├─→ value:*
├─→ int64
├─→ string
├─→ double
├─→ array
├─→ object
├─→ true
└─→ nil, error:str
Turns JSON string into a Lua data structure.
@ -687,6 +692,9 @@ FUNCTIONS
even though that structure won't round-trip with `EncodeJson`
since redbean won't generate invalid JSON (see Postel's Law).
This parser permits top-level values regardless of type, with
the exception of `false`, `null`, and absent.
EncodeJson(value[,options:table])
├─→ json:str
├─→ true [if useoutput]
@ -695,10 +703,12 @@ FUNCTIONS
Turns Lua data structure into a JSON string.
Tables with non-zero length (as reported by `#`) are encoded
as arrays with non-array elements ignored. Empty tables are
encoded as empty arrays. All other tables are encoded as
objects with numerical keys converted to strings (so `{[3]=1}`
is encoded as `{"3":1}`).
as arrays and any non-array elements are ignored. Empty tables
are encoded as `{}` with the exception of the special empty
table `{[0]=false}` shall be encoded as `[]`. Arrays elements
are serialized in specified order. Object entries are sorted
ASCIIbetically using strcmp() on their string keys to ensure
deterministic order.
The following options may be used:
@ -711,6 +721,7 @@ FUNCTIONS
- `value` is cyclic
- `value` has depth greater than 64
- `value` contains functions, user data, or threads
- `value` is table that blends string / non-string keys
When arrays and objects are serialized, entries will be sorted
in a deterministic order.
@ -728,22 +739,30 @@ FUNCTIONS
output buffer and returns `nil` value. This option is
ignored if used outside of request handling code.
This function will fail if:
- `value` has depth greater than 64
If a user data object has a `__repr` or `__tostring` meta
method, then that'll be used to encode the Lua code.
When tables are serialized, entries will be sorted in a
deterministic order. This makes `EncodeLua` a great fit for
writing unit tests, when tables contain regular normal data.
This serializer is designed primarily to describe data. For
example, it's used by the REPL where we need to be able to
ignore errors when displaying data structures, since showing
most things imperfectly is better than crashing. Therefore
this isn't the kind of serializer you'd want to use to persist
data in prod. Try using the JSON serializer for that purpose.
Non-encodable value types (e.g. threads, functions) will be
represented as a string literal with the type name and pointer
address. Note this is subject to change in the future.
address. The string description is of an unspecified format
that could most likely change. This encoder detects cyclic
tables; however instead of failing, it embeds a string of
unspecified layout describing the cycle.
This encoder detects cyclic tables, and encodes a string
literal saying it's cyclic when cycles are encountered.
The only failure return condition currently implemented is
when C runs out of heap memory.
When tables are serialized, entries will be sorted in a
deterministic order.
EncodeLatin1(utf-8:str[,flags:int]) → iso-8859-1:str
Turns UTF-8 into ISO-8859-1 string.

View file

@ -23,34 +23,32 @@
#include "libc/str/tpenc.h"
#include "libc/str/utf16.h"
#include "third_party/double-conversion/wrapper.h"
#include "third_party/lua/cosmo.h"
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/ltests.h"
#include "third_party/lua/lua.h"
#include "tool/net/ljson.h"
#define TOP_LEVEL 0
#define ARRAY_VAL 1
#define OBJECT_KEY 2
#define OBJECT_VAL 3
#define MAX_JSON_DEPTH 128
struct Rc {
int t;
const char *p;
};
static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
static struct DecodeJson Parse(struct lua_State *L, const char *p,
const char *e, int context, int depth) {
long x;
char w[4];
struct Rc r;
const char *a;
luaL_Buffer b;
struct DecodeJson r;
int A, B, C, D, c, d, i, u;
if (lua_gettop(L) >= MAX_JSON_DEPTH) {
luaL_error(L, "maximum depth exceeded\n");
return (struct Rc){-1, p};
if (UNLIKELY(!--depth)) {
return (struct DecodeJson){-1, "maximum depth exceeded"};
}
for (a = p, d = +1; p < e;) {
switch ((c = *p++ & 255)) {
default:
luaL_error(L, "illegal character\n");
return (struct Rc){-1, p};
case ' ': // spaces
case '\n':
case '\r':
@ -59,53 +57,69 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
break;
case ',': // present in list and object
a = p;
break;
if (context == ARRAY_VAL || context == OBJECT_KEY) {
a = p;
break;
} else {
return (struct DecodeJson){-1, "unexpected ','"};
}
case ':': // present only in object after key
if (LUA_TSTRING != lua_type(L, -1)) {
luaL_error(L, "unexpected ':'\n");
return (struct Rc){-1, p};
if (context == OBJECT_VAL) {
a = p;
break;
} else {
return (struct DecodeJson){-1, "unexpected ':'"};
}
a = p;
break;
case 'n': // null
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
if (UNLIKELY(context == TOP_LEVEL)) {
return (struct DecodeJson){-1, "toplevel json can't be null"};
}
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("null")) {
lua_pushnil(L);
return (struct Rc){1, p + 3};
return (struct DecodeJson){1, p + 3};
} else {
goto IllegalCharacter;
}
luaL_error(L, "expecting null\n");
return (struct Rc){-1, p};
case 't': // true
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("true")) {
lua_pushboolean(L, true);
return (struct Rc){1, p + 3};
}
luaL_error(L, "expecting true\n");
return (struct Rc){-1, p};
case 'f': // false
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
if (UNLIKELY(context == TOP_LEVEL)) {
return (struct DecodeJson){-1, "toplevel json can't be false"};
}
if (p + 4 <= e && READ32LE(p) == READ32LE("alse")) {
lua_pushboolean(L, false);
return (struct Rc){1, p + 4};
return (struct DecodeJson){1, p + 4};
} else {
goto IllegalCharacter;
}
case 't': // true
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
if (p + 3 <= e && READ32LE(p - 1) == READ32LE("true")) {
lua_pushboolean(L, true);
return (struct DecodeJson){1, p + 3};
} else {
goto IllegalCharacter;
}
luaL_error(L, "expecting false\n");
return (struct Rc){-1, p};
case '-': // negative
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
d = -1;
break;
case '0': // zero or number
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
if (p < e && (*p == '.' || *p == 'e' || *p == 'E')) {
goto UseDubble;
}
lua_pushinteger(L, 0);
return (struct Rc){1, p};
return (struct DecodeJson){1, p};
case '1' ... '9': // integer
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
for (x = (c - '0') * d; p < e; ++p) {
c = *p & 255;
if (isdigit(c)) {
@ -120,67 +134,75 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
}
}
lua_pushinteger(L, x);
return (struct Rc){1, p};
return (struct DecodeJson){1, p};
UseDubble: // number
lua_pushnumber(L, StringToDouble(a, e - a, &c));
return (struct Rc){1, a + c};
return (struct DecodeJson){1, a + c};
case '[': // Array
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
lua_newtable(L);
i = 0;
do {
r = Parse(L, p, e);
p = r.p;
if (r.t) {
lua_rawseti(L, -2, i++ + 1);
for (;;) {
r = Parse(L, p, e, ARRAY_VAL, depth);
if (UNLIKELY(r.rc == -1)) {
lua_pop(L, 1);
return r;
}
} while (r.t);
if (*(p - 1) != ']') {
luaL_error(L, "invalid list\n");
return (struct Rc){-1, p};
p = r.p;
if (!r.rc) {
break;
}
lua_rawseti(L, -2, i++ + 1);
}
return (struct Rc){1, p};
if (!i) {
// we need this kludge so `[]` won't round-trip as `{}`
lua_pushboolean(L, false);
lua_rawseti(L, -2, 0);
}
return (struct DecodeJson){1, p};
case ']':
if (context == ARRAY_VAL) {
return (struct DecodeJson){0, p};
} else {
return (struct DecodeJson){-1, "unexpected ']'"};
}
case '}':
return (struct Rc){0, p};
if (context == OBJECT_KEY) {
return (struct DecodeJson){0, p};
} else {
return (struct DecodeJson){-1, "unexpected '}'"};
}
case '{': // Object
if (UNLIKELY(context == OBJECT_KEY)) goto BadObjectKey;
lua_newtable(L);
i = 0;
do {
r = Parse(L, p, e);
p = r.p;
if (r.t) {
if (LUA_TSTRING != lua_type(L, -1)) {
/* json keys can only be strings */
lua_settop(L, -2);
break;
}
r = Parse(L, p, e);
p = r.p;
if (!r.t) {
/* key provided but no value */
lua_settop(L, -2);
luaL_error(L, "key provided but no value\n");
return (struct Rc){-1, p};
}
lua_settable(L, -3);
++i;
for (;;) {
r = Parse(L, p, e, OBJECT_KEY, depth);
if (r.rc == -1) {
lua_pop(L, 1);
return r;
}
} while (r.t);
if (!i) {
// we need this kludge so `{}` won't round-trip as `[]`
lua_pushstring(L, "__json_object__");
lua_pushboolean(L, true);
p = r.p;
if (!r.rc) {
break;
}
r = Parse(L, p, e, OBJECT_VAL, depth);
if (r.rc == -1) {
lua_pop(L, 2);
return r;
}
if (!r.rc) {
lua_pop(L, 2);
return (struct DecodeJson){-1, "unexpected eof in object"};
}
p = r.p;
lua_settable(L, -3);
}
if (*(p - 1) != '}') {
luaL_error(L, "invalid object\n");
return (struct Rc){-1, p};
}
return (struct Rc){1, p};
return (struct DecodeJson){1, p};
case '"': // string
luaL_buffinit(L, &b);
@ -193,15 +215,11 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
case '\\':
if (p < e) {
switch ((c = *p++ & 255)) {
case '0':
case 'x':
luaL_error(L, "invalid escaped character\n");
return (struct Rc){-1, p};
default:
goto InvalidEscapeCharacter;
case '"':
case '/':
case '\\':
default:
goto AddByte;
case 'b':
c = '\b';
@ -218,6 +236,16 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
case 't':
c = '\t';
goto AddByte;
case 'x':
if (p + 2 <= e && //
(A = kHexToInt[p[0] & 255]) != -1 && // HEX
(B = kHexToInt[p[1] & 255]) != -1) { //
c = A << 4 | B;
p += 2;
goto AddByte;
} else {
goto InvalidEscapeCharacter;
}
case 'u':
if (p + 4 <= e && //
(A = kHexToInt[p[0] & 255]) != -1 && //
@ -276,6 +304,7 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
}
luaL_addlstring(&b, w, i);
} else {
goto InvalidEscapeCharacter;
BadUnicode:
// Echo invalid \uXXXX sequences
// Rather than corrupting UTF-8!
@ -283,18 +312,35 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
}
break;
}
} else {
goto InvalidEscapeCharacter;
}
break;
case '"':
goto FinishString;
luaL_pushresult(&b);
return (struct DecodeJson){1, p};
}
}
FinishString:
luaL_pushresult(&b);
return (struct Rc){1, p};
luaL_pushresultsize(&b, 0);
lua_pop(L, 1);
return (struct DecodeJson){-1, "unexpected eof in string"};
default:
IllegalCharacter:
return (struct DecodeJson){-1, "illegal character"};
BadObjectKey:
return (struct DecodeJson){-1, "object key must be string"};
InvalidEscapeCharacter:
luaL_pushresultsize(&b, 0);
lua_pop(L, 1);
return (struct DecodeJson){-1, "invalid escape character"};
}
}
return (struct Rc){0, p};
if (UNLIKELY(context == TOP_LEVEL)) {
return (struct DecodeJson){0, 0};
} else {
return (struct DecodeJson){-1, "unexpected eof"};
}
}
/**
@ -318,13 +364,15 @@ static struct Rc Parse(struct lua_State *L, const char *p, const char *e) {
* @param L is Lua interpreter state
* @param p is input string
* @param n is byte length of `p` or -1 for automatic strlen()
* @return 1 if value was pushed, 0 on end, or -1 on error
* @return res.rc is 1 if value pushed, 0 on eof, otherwise -1
* @return res.p is is advanced `p` pointer if `rc` isn't -1
* @return res.p is string describing error if `rc` is -1
*/
int DecodeJson(struct lua_State *L, const char *p, size_t n) {
struct DecodeJson DecodeJson(struct lua_State *L, const char *p, size_t n) {
if (n == -1) n = p ? strlen(p) : 0;
if(!lua_checkstack(L, MAX_JSON_DEPTH + MAX_JSON_DEPTH/2)) {
luaL_error(L, "unable to set stack depth of %d\n", MAX_JSON_DEPTH + MAX_JSON_DEPTH/2);
return -1;
if (lua_checkstack(L, MAX_JSON_DEPTH + MAX_JSON_DEPTH / 2)) {
return Parse(L, p, p + n, TOP_LEVEL, MAX_JSON_DEPTH);
} else {
return (struct DecodeJson){-1, "can't set stack depth"};
}
return Parse(L, p, p + n).t;
}

View file

@ -4,7 +4,12 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int DecodeJson(struct lua_State *, const char *, size_t);
struct DecodeJson {
int rc;
const char *p;
};
struct DecodeJson DecodeJson(struct lua_State *, const char *, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/atomic.h"
#include "libc/bits/likely.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
@ -4238,8 +4239,6 @@ static int LuaEncodeSmth(lua_State *L,
lua_settop(L, 1); // keep the passed argument on top
if (Encoder(L, useoutput ? &outbuf : &p, numformat, -1) == -1) {
free(p);
lua_pushnil(L);
lua_pushstring(L, "serialization failed");
return 2;
}
if (useoutput) {
@ -4262,8 +4261,26 @@ static int LuaEncodeLua(lua_State *L) {
static int LuaDecodeJson(lua_State *L) {
size_t n;
const char *p;
struct DecodeJson r;
p = luaL_checklstring(L, 1, &n);
return DecodeJson(L, p, n);
r = DecodeJson(L, p, n);
if (UNLIKELY(!r.rc)) {
lua_pushnil(L);
lua_pushstring(L, "unexpected eof");
return 2;
}
if (UNLIKELY(r.rc == -1)) {
lua_pushnil(L);
lua_pushstring(L, r.p);
return 2;
}
r = DecodeJson(L, r.p, n - (r.p - p));
if (UNLIKELY(r.rc)) {
lua_pushnil(L);
lua_pushstring(L, "junk after expression");
return 2;
}
return 1;
}
static int LuaGetUrl(lua_State *L) {