Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 25, 2023
1 parent 7912cb7 commit 62cc991
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 98 deletions.
56 changes: 28 additions & 28 deletions json-edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ if _VERSION == "Lua 5.1" or _VERSION == "Lua 5.2" then
for i = f, e do
a2[t + (i - f)] = a1[i]
end
return a2
return a2
end
else
utf8_char = utf8.char
Expand All @@ -61,14 +61,14 @@ end
local json = require "json-beautify"

local encode_escape_map = {
[ "\"" ] = "\\\"",
[ "\\" ] = "\\\\",
[ "/" ] = "\\/",
[ "\b" ] = "\\b",
[ "\f" ] = "\\f",
[ "\n" ] = "\\n",
[ "\r" ] = "\\r",
[ "\t" ] = "\\t",
["\""] = "\\\"",
["\\"] = "\\\\",
["/"] = "\\/",
["\b"] = "\\b",
["\f"] = "\\f",
["\n"] = "\\n",
["\r"] = "\\r",
["\t"] = "\\t",
}

local decode_escape_set = {}
Expand Down Expand Up @@ -175,8 +175,8 @@ local function decode_string()
local s = string_sub(statusBuf, statusPos + 1, i - 1)
if has_unicode_escape then
s = string_gsub(string_gsub(s
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
end
if has_escape then
s = string_gsub(s, "\\.", decode_escape_map)
Expand Down Expand Up @@ -302,23 +302,23 @@ local function decode_object(ast)
end

local decode_uncompleted_map = {
[ string_byte '"' ] = decode_string,
[ string_byte "0" ] = decode_number_zero,
[ string_byte "1" ] = decode_number,
[ string_byte "2" ] = decode_number,
[ string_byte "3" ] = decode_number,
[ string_byte "4" ] = decode_number,
[ string_byte "5" ] = decode_number,
[ string_byte "6" ] = decode_number,
[ string_byte "7" ] = decode_number,
[ string_byte "8" ] = decode_number,
[ string_byte "9" ] = decode_number,
[ string_byte "-" ] = decode_number_negative,
[ string_byte "t" ] = decode_true,
[ string_byte "f" ] = decode_false,
[ string_byte "n" ] = decode_null,
[ string_byte "[" ] = decode_array,
[ string_byte "{" ] = decode_object,
[string_byte '"'] = decode_string,
[string_byte "0"] = decode_number_zero,
[string_byte "1"] = decode_number,
[string_byte "2"] = decode_number,
[string_byte "3"] = decode_number,
[string_byte "4"] = decode_number,
[string_byte "5"] = decode_number,
[string_byte "6"] = decode_number,
[string_byte "7"] = decode_number,
[string_byte "8"] = decode_number,
[string_byte "9"] = decode_number,
[string_byte "-"] = decode_number_negative,
[string_byte "t"] = decode_true,
[string_byte "f"] = decode_false,
[string_byte "n"] = decode_null,
[string_byte "["] = decode_array,
[string_byte "{"] = decode_object,
}
local function unexpected_character()
decode_error("unexpected character '"..string_sub(statusBuf, statusPos, statusPos).."'")
Expand Down
28 changes: 14 additions & 14 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ if debug and debug.upvalueid then
-- Generate a lightuserdata
json.null = debug.upvalueid(json.createEmptyObject, 1)
else
json.null = function() end
json.null = function () end
end

-- json.encode --
Expand All @@ -86,14 +86,14 @@ local statusBuilder
local encode_map = {}

local encode_escape_map = {
[ "\"" ] = "\\\"",
[ "\\" ] = "\\\\",
[ "/" ] = "\\/",
[ "\b" ] = "\\b",
[ "\f" ] = "\\f",
[ "\n" ] = "\\n",
[ "\r" ] = "\\r",
[ "\t" ] = "\\t",
["\""] = "\\\"",
["\\"] = "\\\\",
["/"] = "\\/",
["\b"] = "\\b",
["\f"] = "\\f",
["\n"] = "\\n",
["\r"] = "\\r",
["\t"] = "\\t",
}

local decode_escape_set = {}
Expand Down Expand Up @@ -254,9 +254,9 @@ local function encode_unexpected(v)
error("unexpected type '"..type(v).."'")
end
end
encode_map[ "function" ] = encode_unexpected
encode_map[ "userdata" ] = encode_unexpected
encode_map[ "thread" ] = encode_unexpected
encode_map["function"] = encode_unexpected
encode_map["userdata"] = encode_unexpected
encode_map["thread"] = encode_unexpected

function json.encode(v)
statusVisited = {}
Expand Down Expand Up @@ -352,8 +352,8 @@ local function decode_string()
local s = string_sub(statusBuf, statusPos + 1, i - 1)
if has_unicode_escape then
s = string_gsub(string_gsub(s
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
end
if has_escape then
s = string_gsub(s, "\\.", decode_escape_map)
Expand Down
54 changes: 27 additions & 27 deletions jsonc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ end
local json = require "json"

local encode_escape_map = {
[ "\"" ] = "\\\"",
[ "\\" ] = "\\\\",
[ "/" ] = "\\/",
[ "\b" ] = "\\b",
[ "\f" ] = "\\f",
[ "\n" ] = "\\n",
[ "\r" ] = "\\r",
[ "\t" ] = "\\t",
["\""] = "\\\"",
["\\"] = "\\\\",
["/"] = "\\/",
["\b"] = "\\b",
["\f"] = "\\f",
["\n"] = "\\n",
["\r"] = "\\r",
["\t"] = "\\t",
}

local decode_escape_set = {}
Expand Down Expand Up @@ -155,8 +155,8 @@ local function decode_string()
local s = string_sub(statusBuf, statusPos + 1, i - 1)
if has_unicode_escape then
s = string_gsub(string_gsub(s
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
, "\\u([dD][89aAbB]%x%x)\\u([dD][c-fC-F]%x%x)", decode_unicode_surrogate)
, "\\u(%x%x%x%x)", decode_unicode_escape)
end
if has_escape then
s = string_gsub(s, "\\.", decode_escape_map)
Expand Down Expand Up @@ -280,23 +280,23 @@ local function decode_object()
end

local decode_uncompleted_map = {
[ string_byte '"' ] = decode_string,
[ string_byte "0" ] = decode_number_zero,
[ string_byte "1" ] = decode_number,
[ string_byte "2" ] = decode_number,
[ string_byte "3" ] = decode_number,
[ string_byte "4" ] = decode_number,
[ string_byte "5" ] = decode_number,
[ string_byte "6" ] = decode_number,
[ string_byte "7" ] = decode_number,
[ string_byte "8" ] = decode_number,
[ string_byte "9" ] = decode_number,
[ string_byte "-" ] = decode_number_negative,
[ string_byte "t" ] = decode_true,
[ string_byte "f" ] = decode_false,
[ string_byte "n" ] = decode_null,
[ string_byte "[" ] = decode_array,
[ string_byte "{" ] = decode_object,
[string_byte '"'] = decode_string,
[string_byte "0"] = decode_number_zero,
[string_byte "1"] = decode_number,
[string_byte "2"] = decode_number,
[string_byte "3"] = decode_number,
[string_byte "4"] = decode_number,
[string_byte "5"] = decode_number,
[string_byte "6"] = decode_number,
[string_byte "7"] = decode_number,
[string_byte "8"] = decode_number,
[string_byte "9"] = decode_number,
[string_byte "-"] = decode_number_negative,
[string_byte "t"] = decode_true,
[string_byte "f"] = decode_false,
[string_byte "n"] = decode_null,
[string_byte "["] = decode_array,
[string_byte "{"] = decode_object,
}
local function unexpected_character()
decode_error("unexpected character '"..string_sub(statusBuf, statusPos, statusPos).."'")
Expand Down
45 changes: 23 additions & 22 deletions test/conformance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ local jsonc_yes = {
}

local function reload()
package.loaded['json'] = nil
package.loaded["json"] = nil
package.loaded[JSONLIB] = nil
json = require(JSONLIB)
end

local isWindows = package.config:sub(1,1) == '\\'
local isWindows = package.config:sub(1, 1) == "\\"

local function each_directory(dir)
local command = isWindows
and "dir /B " .. dir:gsub("/", "\\") .. " 2>nul"
or "ls -1 " .. dir
and "dir /B "..dir:gsub("/", "\\").." 2>nul"
or "ls -1 "..dir
local lst = {}
for file in io.popen(command):lines() do
lst[#lst+1] = file
Expand Down Expand Up @@ -66,23 +66,23 @@ local function readfile(path)
end

local function test_yes(path)
return function()
return function ()
local res = json.decode(readfile(path))
lt.assertEquals(json.decode(json.encode(res)), res)
end
end

local ERROR = ": ERROR: "
local function test_no(path)
return function()
return function ()
local ok, msg = pcall(json.decode, readfile(path))
lt.assertEquals(ok, false)
lt.assertEquals(msg:match(ERROR), ERROR)
end
end

local function test_impl(path)
return function()
return function ()
json.decode(readfile(path))
end
end
Expand All @@ -92,12 +92,12 @@ function lt.format(className, methodName)
if className == "parsing" or className == "transform" then
return ("test/JSONTestSuite/test_%s/%s"):format(className, methodName)
end
return className..'.'..methodName
return className.."."..methodName
end

local parsing = lt.test "parsing"
for name, path in each_directory "test/JSONTestSuite/test_parsing" do
local type = name:sub(1,1)
local type = name:sub(1, 1)
if JSONLIB == "jsonc" and jsonc_yes[name] then
parsing[name] = test_yes(path)
elseif type == "y" then
Expand Down Expand Up @@ -127,23 +127,23 @@ local other = lt.test "other"
function other.encode_sparse_array()
json.supportSparseArray = false
lt.assertEquals(json.encode {}, "[]")
lt.assertEquals(json.encode {1}, "[1]")
lt.assertEquals(json.encode {1,2}, "[1,2]")
lt.assertError(json.encode, {nil,2})
lt.assertError(json.encode, {1,2,nil,4})
lt.assertError(json.encode, {1,2,[100]=1})
lt.assertError(json.encode, {1,2,a=1})
lt.assertError(json.encode, {1,2,[0]=1})
lt.assertEquals(json.encode { 1 }, "[1]")
lt.assertEquals(json.encode { 1, 2 }, "[1,2]")
lt.assertError(json.encode, { nil, 2 })
lt.assertError(json.encode, { 1, 2, nil, 4 })
lt.assertError(json.encode, { 1, 2,[100] = 1 })
lt.assertError(json.encode, { 1, 2, a = 1 })
lt.assertError(json.encode, { 1, 2,[0] = 1 })
json.supportSparseArray = true
lt.assertEquals(json.encode {nil,1}, "[null,1]")
lt.assertEquals(json.encode { nil, 1 }, "[null,1]")
end

function other.encode_float()
lt.assertEquals(json.encode(0.12345678901234566), "0.12345678901234566")
lt.assertError(json.encode, function() end)
lt.assertError(json.encode, function () end)
lt.assertError(json.encode, math.huge)
lt.assertError(json.encode, -math.huge)
lt.assertError(json.encode, 0/0)
lt.assertError(json.encode, 0 / 0)
if supportBigInt then
lt.assertEquals(json.encode(BigInt), tostring(BigInt))
end
Expand All @@ -156,18 +156,19 @@ function other.encode()
lt.assertEquals(json.isObject(json.decode "[1]"), false)

do
local t = {}; t[1] = t
local t = {}
t[1] = t
lt.assertError(json.encode, t)
end

do
local t = {1,a=1}
local t = { 1, a = 1 }
lt.assertEquals(next(t), 1)
lt.assertError(json.encode, t)
end

do
local t = {[true]=true}
local t = { [true] = true }
local i = 1
repeat
t[tostring(i)] = true
Expand Down
7 changes: 3 additions & 4 deletions test/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ local function findline(str, positon)
local line = 1
local pos = 1
while true do
local f, _, nl1, nl2 = str:find('([\n\r])([\n\r]?)', pos)
local f, _, nl1, nl2 = str:find("([\n\r])([\n\r]?)", pos)
if not f then
return line, positon - pos + 1
end
local newpos = f + ((nl1 == nl2 or nl2 == '') and 1 or 2)
local newpos = f + ((nl1 == nl2 or nl2 == "") and 1 or 2)
if newpos > positon then
return line, positon - pos + 1
end
Expand Down Expand Up @@ -172,7 +172,7 @@ function testsuc.add()
TEST {
op = "add",
path = "/outputCapture",
value = {"print"},
value = { "print" },
expected = [[
{
/*
Expand Down Expand Up @@ -368,7 +368,6 @@ function testsuc.empty_table()
}
end


local function TEST_C(test)
local expected = test.expected
local actual = json.edit(test.doc, test.patch, {
Expand Down
6 changes: 3 additions & 3 deletions test/jsonc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function m.comment()
"a" : 1e0/**/,
/**/"b"/**/: [1,2]
}
]], {a=1,b={1,2}})
]], { a = 1, b = { 1, 2 } })
test_yes("{/**/}", jsonc.createEmptyObject())
test_yes("[/**/]", {})
test_yes("", jsonc.null)
Expand All @@ -40,10 +40,10 @@ end
function m.trailing_comma()
test_yes([[
[1,]
]], {1})
]], { 1 })
test_yes([[
{"a":1,}
]], {a=1})
]], { a = 1 })
end

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

0 comments on commit 62cc991

Please sign in to comment.