mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-12 16:15:20 +02:00
Improve minetest.parse_json
Let modders handle parsing errors, get rid of two unnecessary copies.
This commit is contained in:
committed by
Lars Müller
parent
c7938ce81c
commit
4c419c4020
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user