<|start_filename|>test/jsonc.lua<|end_filename|>
package.path = table.concat({
    "?.lua",
    "test/ltest/?.lua",
}, ";")

local lt = require "ltest"
local jsonc = require "jsonc"

local m = lt.test "jsonc"

local function test_yes(input, ouput)
    lt.assertEquals(jsonc.encode(jsonc.decode(input)), jsonc.encode(ouput))
end
local function test_no(input)
    lt.assertError(jsonc.decode, input)
end

function m.comment()
    test_yes([[
        /* block comment */
        // line comment
        []
    ]], {})
    test_yes([[
        {
            "a" : 1e0/**/,
            /**/"b"/**/: [1,2]
        }
    ]], {a=1,b={1,2}})
    test_yes("{/**/}", jsonc.createEmptyObject())
    test_yes("[/**/]", {})
    test_yes("", jsonc.null)
    lt.assertEquals(jsonc.decode(""), jsonc.null)
    test_no [[
        []
        /* block comment
    ]]
end

function m.trailing_comma()
    test_yes([[
        [1,]
    ]], {1})
    test_yes([[
        {"a":1,}
    ]], {a=1})
end

os.exit(lt.run(), true)


<|start_filename|>test/benchmark.lua<|end_filename|>
package.path = table.concat({
    "?.lua",
    "test/ltest/?.lua",
}, ";")

local lt = require "ltest"
local json = require "json"

local function readfile(path)
    local f = assert(io.open(path, "rb"))
    if f:read(3) ~= "\239\187\191" then
        f:seek "set"
    end
    local data = f:read "*a"
    f:close()
    return data
end

local dir = "test/nativejson-benchmark/data/"
local lst = {}
for file in io.lines(dir .. "data.txt") do
    lst[#lst+1] = file:gsub("^[ \t\r\b]*(.-)[ \t\r\b]*$", "%1")
end
table.sort(lst)

local decode = lt.test "decode"
local encode = lt.test "encode"
local decode_output = {}
local encode_output = {}
for i, file in ipairs(lst) do
    local decode_input = readfile(dir .. file)
    local encode_input = json.decode(decode_input)
    decode[file] = function()
        decode_output[i] = json.decode(decode_input)
    end
    encode[file] = function()
        encode_output[i] = json.encode(encode_input)
    end
end

os.exit(lt.run(), true)


<|start_filename|>json-beautify.lua<|end_filename|>
local json = require "json"
local type = type
local next = next
local error = error
local table_concat = table.concat
local table_sort = table.sort
local string_rep = string.rep
local math_type = math.type
local setmetatable = setmetatable

local statusVisited
local statusBuilder
local statusDep
local statusOpt

local defaultOpt = {
    newline = "\n",
    indent = "  ",
}
defaultOpt.__index = defaultOpt

local function encode_newline()
    statusBuilder[#statusBuilder+1] = statusOpt.newline..string_rep(statusOpt.indent, statusDep)
end

local encode_map = {}
local encode_string = json._encode_string
for k ,v in next, json._encode_map do
    encode_map[k] = v
end

local function encode(v)
    local res = encode_map[type(v)](v)
    statusBuilder[#statusBuilder+1] = res
end

function encode_map.string(v)
    statusBuilder[#statusBuilder+1] = '"'
    statusBuilder[#statusBuilder+1] = encode_string(v)
    return '"'
end

function encode_map.table(t)
    local first_val = next(t)
    if first_val == nil then
        if json.isObject(t) then
            return "{}"
        else
            return "[]"
        end
    end
    if statusVisited[t] then
        error("circular reference")
    end
    statusVisited[t] = true
    if type(first_val) == 'string' then
        local key = {}
        for k in next, t do
            if type(k) ~= "string" then
                error("invalid table: mixed or invalid key types")
            end
            key[#key+1] = k
        end
        table_sort(key)
        statusBuilder[#statusBuilder+1] = "{"
        statusDep = statusDep + 1
        encode_newline()
        local k = key[1]
        statusBuilder[#statusBuilder+1] = '"'
        statusBuilder[#statusBuilder+1] = encode_string(k)
        statusBuilder[#statusBuilder+1] = '": '
        encode(t[k])
        for i = 2, #key do
            local k = key[i]
            statusBuilder[#statusBuilder+1] = ","
            encode_newline()
            statusBuilder[#statusBuilder+1] = '"'
            statusBuilder[#statusBuilder+1] = encode_string(k)
            statusBuilder[#statusBuilder+1] = '": '
            encode(t[k])
        end
        statusDep = statusDep - 1
        encode_newline()
        statusVisited[t] = nil
        return "}"
    elseif json.supportSparseArray then
        local max = 0
        for k in next, t do
            if math_type(k) ~= "integer" or k <= 0 then
                error("invalid table: mixed or invalid key types")
            end
            if max < k then
                max = k
            end
        end
        statusBuilder[#statusBuilder+1] = "["
        statusDep = statusDep + 1
        encode_newline()
        encode(t[1])
        for i = 2, max do
            statusBuilder[#statusBuilder+1] = ","
            encode_newline()
            encode(t[i])
        end
        statusDep = statusDep - 1
        encode_newline()
        statusVisited[t] = nil
        return "]"
    else
        if t[1] == nil then
            error("invalid table: mixed or invalid key types")
        end
        statusBuilder[#statusBuilder+1] = "["
        statusDep = statusDep + 1
        encode_newline()
        encode(t[1])
        local count = 2
        while t[count] ~= nil do
            statusBuilder[#statusBuilder+1] = ","
            encode_newline()
            encode(t[count])
            count = count + 1
        end
        if next(t, count-1) ~= nil then
            error("invalid table: mixed or invalid key types")
        end
        statusDep = statusDep - 1
        encode_newline()
        statusVisited[t] = nil
        return "]"
    end
end

local function beautify(v, option)
    statusVisited = {}
    statusBuilder = {}
    statusDep = 0
    statusOpt = option and setmetatable(option, defaultOpt) or defaultOpt
    encode(v)
    return table_concat(statusBuilder)
end

json.beautify = beautify

return json
