summaryrefslogtreecommitdiff
path: root/server/src/json
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-06 10:21:49 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-06 10:21:49 +0800
commitcd57345c054659f27e7b9fdf91d30074a6dca997 (patch)
tree891a7e1ca8c60820e4e1748664e5f84136feffcc /server/src/json
parentc84d61d473c514f4fe5483f38a8d159ffe8689cd (diff)
downloadlua-language-server-cd57345c054659f27e7b9fdf91d30074a6dca997.zip
json表支持null
Diffstat (limited to 'server/src/json')
-rw-r--r--server/src/json/init.lua1
-rw-r--r--server/src/json/table.lua62
2 files changed, 63 insertions, 0 deletions
diff --git a/server/src/json/init.lua b/server/src/json/init.lua
index c28e7aed..db513678 100644
--- a/server/src/json/init.lua
+++ b/server/src/json/init.lua
@@ -1,6 +1,7 @@
local api = {
decode = require 'json.decode',
encode = require 'json.encode',
+ table = require 'json.table',
}
return api
diff --git a/server/src/json/table.lua b/server/src/json/table.lua
new file mode 100644
index 00000000..351bfe07
--- /dev/null
+++ b/server/src/json/table.lua
@@ -0,0 +1,62 @@
+
+local pairs = pairs
+local next = next
+local sort = table.sort
+local type = type
+local rawset = rawset
+local move = table.move
+local setmetatable = setmetatable
+
+local function sort_table(tbl)
+ if not tbl then
+ tbl = {}
+ end
+ local mt = {}
+ local keys = {}
+ local mark = {}
+ local n = 0
+ for key in next, tbl do
+ n=n+1;keys[n] = key
+ mark[key] = true
+ end
+ sort(keys)
+ function mt:__newindex(key, value)
+ rawset(self, key, value)
+ n=n+1;keys[n] = key
+ mark[key] = true
+ if type(value) == 'table' then
+ sort_table(value)
+ end
+ end
+ function mt:__pairs()
+ local list = {}
+ local m = 0
+ for key in next, self do
+ if not mark[key] then
+ m=m+1;list[m] = key
+ end
+ end
+ if m > 0 then
+ move(keys, 1, n, m+1)
+ sort(list)
+ for i = 1, m do
+ local key = list[i]
+ keys[i] = key
+ mark[key] = true
+ end
+ n = n + m
+ end
+ local i = 0
+ return function ()
+ i = i + 1
+ local key = keys[i]
+ return key, self[key]
+ end
+ end
+
+ return setmetatable(tbl, mt)
+end
+
+return function (tbl)
+ return sort_table(tbl)
+end