aboutsummaryrefslogtreecommitdiff
path: root/json.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2024-08-27 21:58:31 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2024-08-27 22:16:14 +0200
commitfdc6e201533c4d2e0409dbda4ebedbf625ac0c63 (patch)
treeaf281750cf88ea656b321b2b89d76092b278c8d1 /json.lua
parente1de91801727634c34ab408c62e5a4938f9cdc6d (diff)
downloadvis-lspc-fdc6e201533c4d2e0409dbda4ebedbf625ac0c63.tar.gz
vis-lspc-fdc6e201533c4d2e0409dbda4ebedbf625ac0c63.zip
export the json module and move all json related code into json.lua
In the future other modules can use the json implementation included in our lspc table.
Diffstat (limited to 'json.lua')
-rw-r--r--json.lua27
1 files changed, 23 insertions, 4 deletions
diff --git a/json.lua b/json.lua
index 631e7f5..876a889 100644
--- a/json.lua
+++ b/json.lua
@@ -1,3 +1,26 @@
+--- Find or provide a suitable json module for vis-lspc.
+--
+-- @module json
+local json = {}
+
+--- Json modules to use if they are included in LUA_PATH.
+local json_impls = {'json', 'cjson', 'dkjson'}
+
+-- find a suitable json implementation
+for _, json_impl in ipairs(json_impls) do
+ if vis:module_exist(json_impl) then
+ json = require(json_impl)
+ if not json.encode or not json.decode then
+ json = nil
+ end
+
+ -- found a usable json implementation
+ if json then
+ return json
+ end
+ end
+end
+
--[[ json.lua
A compact pure-Lua JSON library.
@@ -44,10 +67,6 @@ avoiding null values in your data to begin with.
--]]
-
-local json = {}
-
-
-- Internal functions.
local function kind_of(obj)