Skip to content

Commit 1d14031

Browse files
committed
update json.lua
1 parent 1b67188 commit 1d14031

File tree

11 files changed

+196
-307
lines changed

11 files changed

+196
-307
lines changed

Diff for: script/cli/check.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local ws = require 'workspace'
44
local files = require 'files'
55
local diag = require 'provider.diagnostic'
66
local util = require 'utility'
7-
local json = require 'json-beautify'
7+
local jsonb = require 'json-beautify'
88
local lang = require 'language'
99
local define = require 'proto.define'
1010
local config = require 'config.config'
@@ -93,7 +93,7 @@ if count == 0 then
9393
print(lang.script('CLI_CHECK_SUCCESS'))
9494
else
9595
local outpath = LOGPATH .. '/check.json'
96-
util.saveFile(outpath, json.beautify(results))
96+
util.saveFile(outpath, jsonb.beautify(results))
9797

9898
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
9999
end

Diff for: script/cli/doc.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local furi = require 'file-uri'
33
local ws = require 'workspace'
44
local files = require 'files'
55
local util = require 'utility'
6-
local json = require 'json-beautify'
6+
local jsonb = require 'json-beautify'
77
local lang = require 'language'
88
local define = require 'proto.define'
99
local config = require 'config.config'
@@ -258,7 +258,7 @@ lclient():start(function (client)
258258
end)
259259

260260
local outpath = LOGPATH .. '/doc.json'
261-
json.supportSparseArray = true
262-
util.saveFile(outpath, json.beautify(results))
261+
jsonb.supportSparseArray = true
262+
util.saveFile(outpath, jsonb.beautify(results))
263263

264264
require 'cli.doc2md'

Diff for: script/cli/doc2md.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
-- This is an example of how to process the generated `doc.json` file.
22
-- You can use it to generate a markdown file or a html file.
33

4-
local json = require 'json'
4+
local jsonc = require 'jsonc'
55
local util = require 'utility'
66
local markdown = require 'provider.markdown'
77

8-
local doc = json.decode(util.loadFile(LOGPATH .. '/doc.json'))
8+
local doc = jsonc.decode_jsonc(util.loadFile(LOGPATH .. '/doc.json'))
99
local md = markdown()
1010

11+
assert(type(doc) == 'table')
12+
1113
for _, class in ipairs(doc) do
1214
md:add('md', '# ' .. class.name)
1315
md:emptyLine()

Diff for: script/client.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local proto = require 'proto'
66
local define = require 'proto.define'
77
local config = require 'config'
88
local converter = require 'proto.converter'
9-
local json = require 'json-beautify'
9+
local jsonb = require 'json-beautify'
1010
local await = require 'await'
1111
local scope = require 'workspace.scope'
1212
local inspect = require 'inspect'
@@ -243,7 +243,7 @@ local function tryModifySpecifiedConfig(uri, finalChanges)
243243
if not path then
244244
return false
245245
end
246-
util.saveFile(path, json.beautify(scp:get('lastLocalConfig'), { indent = ' ' }))
246+
util.saveFile(path, jsonb.beautify(scp:get('lastLocalConfig'), { indent = ' ' }))
247247
return true
248248
end
249249

@@ -274,7 +274,7 @@ local function tryModifyRC(uri, finalChanges, create)
274274
if not suc then
275275
return false
276276
end
277-
util.saveFile(path, json.beautify(rc, { indent = ' ' }))
277+
util.saveFile(path, jsonb.beautify(rc, { indent = ' ' }))
278278
return true
279279
end
280280

Diff for: script/config/loader.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function m.loadRCConfig(uri, filename)
3030
scp:set('lastRCConfig', nil)
3131
return nil
3232
end
33-
local suc, res = pcall(jsonc.decode, buf)
33+
local suc, res = pcall(jsonc.decode_jsonc, buf)
3434
if not suc then
3535
errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
3636
return scp:get('lastRCConfig')
@@ -61,7 +61,7 @@ function m.loadLocalConfig(uri, filename)
6161
end
6262
local firstChar = buf:match '%S'
6363
if firstChar == '{' then
64-
local suc, res = pcall(jsonc.decode, buf)
64+
local suc, res = pcall(jsonc.decode_jsonc, buf)
6565
if not suc then
6666
errorMessage(lang.script('CONFIG_LOAD_ERROR', res))
6767
return scp:get('lastLocalConfig')

Diff for: script/core/command/jsonToLua.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local files = require 'files'
2-
local json = require 'jsonc'
32
local util = require 'utility'
43
local proto = require 'proto'
54
local define = require 'proto.define'
65
local lang = require 'language'
76
local converter = require 'proto.converter'
87
local guide = require 'parser.guide'
8+
local json = require 'json'
9+
local jsonc = require 'jsonc'
910

1011
---@async
1112
return function (data)
@@ -17,7 +18,7 @@ return function (data)
1718
local start = guide.positionToOffset(state, data.start)
1819
local finish = guide.positionToOffset(state, data.finish)
1920
local jsonStr = text:sub(start + 1, finish)
20-
local suc, res = pcall(json.decode, jsonStr:match '[%{%[].+')
21+
local suc, res = pcall(jsonc.decode_jsonc, jsonStr:match '[%{%[].+')
2122
if not suc or res == json.null then
2223
proto.notify('window/showMessage', {
2324
type = define.MessageType.Warning,

Diff for: script/json-beautify.lua

+77-35
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,36 @@ local error = error
55
local table_concat = table.concat
66
local table_sort = table.sort
77
local string_rep = string.rep
8-
local math_type = math.type
98
local setmetatable = setmetatable
10-
local getmetatable = getmetatable
119

12-
local statusMark
13-
local statusQue
10+
local math_type
11+
12+
if _VERSION == "Lua 5.1" or _VERSION == "Lua 5.2" then
13+
local math_floor = math.floor
14+
function math_type(v)
15+
if v >= -2147483648 and v <= 2147483647 and math_floor(v) == v then
16+
return "integer"
17+
end
18+
return "float"
19+
end
20+
else
21+
math_type = math.type
22+
end
23+
24+
local statusVisited
25+
local statusBuilder
1426
local statusDep
1527
local statusOpt
1628

1729
local defaultOpt = {
1830
newline = "\n",
19-
indent = " ",
31+
indent = " ",
32+
depth = 0,
2033
}
2134
defaultOpt.__index = defaultOpt
2235

2336
local function encode_newline()
24-
statusQue[#statusQue+1] = statusOpt.newline..string_rep(statusOpt.indent, statusDep)
37+
statusBuilder[#statusBuilder+1] = statusOpt.newline..string_rep(statusOpt.indent, statusDep)
2538
end
2639

2740
local encode_map = {}
@@ -32,96 +45,125 @@ end
3245

3346
local function encode(v)
3447
local res = encode_map[type(v)](v)
35-
statusQue[#statusQue+1] = res
48+
statusBuilder[#statusBuilder+1] = res
3649
end
3750

3851
function encode_map.string(v)
39-
statusQue[#statusQue+1] = '"'
40-
statusQue[#statusQue+1] = encode_string(v)
52+
statusBuilder[#statusBuilder+1] = '"'
53+
statusBuilder[#statusBuilder+1] = encode_string(v)
4154
return '"'
4255
end
4356

4457
function encode_map.table(t)
4558
local first_val = next(t)
4659
if first_val == nil then
47-
if getmetatable(t) == json.object then
60+
if json.isObject(t) then
4861
return "{}"
4962
else
5063
return "[]"
5164
end
5265
end
53-
if statusMark[t] then
66+
if statusVisited[t] then
5467
error("circular reference")
5568
end
56-
statusMark[t] = true
69+
statusVisited[t] = true
5770
if type(first_val) == 'string' then
5871
local key = {}
5972
for k in next, t do
6073
if type(k) ~= "string" then
61-
error("invalid table: mixed or invalid key types")
74+
error("invalid table: mixed or invalid key types: "..k)
6275
end
6376
key[#key+1] = k
6477
end
6578
table_sort(key)
66-
statusQue[#statusQue+1] = "{"
79+
statusBuilder[#statusBuilder+1] = "{"
6780
statusDep = statusDep + 1
6881
encode_newline()
6982
local k = key[1]
70-
statusQue[#statusQue+1] = '"'
71-
statusQue[#statusQue+1] = encode_string(k)
72-
statusQue[#statusQue+1] = '": '
83+
statusBuilder[#statusBuilder+1] = '"'
84+
statusBuilder[#statusBuilder+1] = encode_string(k)
85+
statusBuilder[#statusBuilder+1] = '": '
7386
encode(t[k])
7487
for i = 2, #key do
7588
local k = key[i]
76-
statusQue[#statusQue+1] = ","
89+
statusBuilder[#statusBuilder+1] = ","
7790
encode_newline()
78-
statusQue[#statusQue+1] = '"'
79-
statusQue[#statusQue+1] = encode_string(k)
80-
statusQue[#statusQue+1] = '": '
91+
statusBuilder[#statusBuilder+1] = '"'
92+
statusBuilder[#statusBuilder+1] = encode_string(k)
93+
statusBuilder[#statusBuilder+1] = '": '
8194
encode(t[k])
8295
end
8396
statusDep = statusDep - 1
8497
encode_newline()
85-
statusMark[t] = nil
98+
statusVisited[t] = nil
8699
return "}"
87-
else
100+
elseif json.supportSparseArray then
88101
local max = 0
89102
for k in next, t do
90103
if math_type(k) ~= "integer" or k <= 0 then
91-
error("invalid table: mixed or invalid key types")
104+
error("invalid table: mixed or invalid key types: "..k)
92105
end
93106
if max < k then
94107
max = k
95108
end
96109
end
97-
statusQue[#statusQue+1] = "["
110+
statusBuilder[#statusBuilder+1] = "["
98111
statusDep = statusDep + 1
99112
encode_newline()
100113
encode(t[1])
101114
for i = 2, max do
102-
statusQue[#statusQue+1] = ","
115+
statusBuilder[#statusBuilder+1] = ","
103116
encode_newline()
104117
encode(t[i])
105118
end
106119
statusDep = statusDep - 1
107120
encode_newline()
108-
statusMark[t] = nil
121+
statusVisited[t] = nil
122+
return "]"
123+
else
124+
if t[1] == nil then
125+
error("invalid table: sparse array is not supported")
126+
end
127+
statusBuilder[#statusBuilder+1] = "["
128+
statusDep = statusDep + 1
129+
encode_newline()
130+
encode(t[1])
131+
local count = 2
132+
while t[count] ~= nil do
133+
statusBuilder[#statusBuilder+1] = ","
134+
encode_newline()
135+
encode(t[count])
136+
count = count + 1
137+
end
138+
if next(t, count-1) ~= nil then
139+
local k = next(t, count-1)
140+
if type(k) == "number" then
141+
error("invalid table: sparse array is not supported")
142+
else
143+
error("invalid table: mixed or invalid key types: "..k)
144+
end
145+
end
146+
statusDep = statusDep - 1
147+
encode_newline()
148+
statusVisited[t] = nil
109149
return "]"
110150
end
111151
end
112152

153+
local function beautify_option(option)
154+
return setmetatable(option or {}, defaultOpt)
155+
end
156+
113157
local function beautify(v, option)
114-
if type(v) == "string" then
115-
v = json.decode(v)
116-
end
117-
statusMark = {}
118-
statusQue = {}
119-
statusDep = 0
120-
statusOpt = option and setmetatable(option, defaultOpt) or defaultOpt
158+
statusVisited = {}
159+
statusBuilder = {}
160+
statusOpt = beautify_option(option)
161+
statusDep = statusOpt.depth
121162
encode(v)
122-
return table_concat(statusQue)
163+
return table_concat(statusBuilder)
123164
end
124165

125166
json.beautify = beautify
167+
json.beautify_option = beautify_option
126168

127169
return json

0 commit comments

Comments
 (0)