mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Improve minetest.parse_json
Let modders handle parsing errors, get rid of two unnecessary copies.
This commit is contained in:
parent
c7938ce81c
commit
4c419c4020
3 changed files with 48 additions and 35 deletions
|
@ -155,13 +155,22 @@ unittests.register("test_urlencode", test_urlencode)
|
|||
|
||||
local function test_parse_json()
|
||||
local raw = "{\"how\\u0000weird\":\n\"yes\\u0000really\",\"n\":-1234567891011,\"z\":null}"
|
||||
local data = core.parse_json(raw)
|
||||
assert(data["how\000weird"] == "yes\000really")
|
||||
assert(data.n == -1234567891011)
|
||||
assert(data.z == nil)
|
||||
local null = {}
|
||||
data = core.parse_json(raw, null)
|
||||
assert(data.z == null)
|
||||
do
|
||||
local data = core.parse_json(raw)
|
||||
assert(data["how\000weird"] == "yes\000really")
|
||||
assert(data.n == -1234567891011)
|
||||
assert(data.z == nil)
|
||||
end
|
||||
do
|
||||
local null = {}
|
||||
local data = core.parse_json(raw, null)
|
||||
assert(data.z == null)
|
||||
end
|
||||
do
|
||||
local data, err = core.parse_json('"ceci n\'est pas un json', nil, true)
|
||||
assert(data == nil)
|
||||
assert(type(err) == "string")
|
||||
end
|
||||
end
|
||||
unittests.register("test_parse_json", test_parse_json)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue