From ca2dbf9237064d61897cd12e195142f544792564 Mon Sep 17 00:00:00 2001 From: XeroOl Date: Tue, 26 Mar 2024 13:00:57 -0500 Subject: [PATCH] let me hide keys using mt.__json_exclude_keys --- src/fennel-ls/json/json.lua | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/fennel-ls/json/json.lua b/src/fennel-ls/json/json.lua index 3929ae2..eb36b42 100644 --- a/src/fennel-ls/json/json.lua +++ b/src/fennel-ls/json/json.lua @@ -22,11 +22,16 @@ -- SOFTWARE. -- +-- Modifications have been made to this file. The ORIGINAL code can be found at +-- this URL: https://github.com/rxi/json.lua + local json = { _version = "0.1.2" } -- unique placeholder for "null" json.null = { _ = "nil" } +local view = require("fennel").view + ------------------------------------------------------------------------------- -- Encode ------------------------------------------------------------------------------- @@ -73,7 +78,7 @@ local function encode_table(val, stack) local n = 0 for k in pairs(val) do if type(k) ~= "number" then - error("invalid table: mixed or invalid key types") + error("invalid table: mixed or invalid key types in " .. view(val)) end n = n + 1 end @@ -89,11 +94,15 @@ local function encode_table(val, stack) else -- Treat as an object + local mt = getmetatable(val) + local exclude = mt and mt.__json_exclude_keys for k, v in pairs(val) do - if type(k) ~= "string" then - error("invalid table: mixed or invalid key types") + if not (exclude and exclude[k]) then + if type(k) ~= "string" then + error("invalid table: mixed or invalid key types in " .. view(val)) + end + table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) end - table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) end stack[val] = nil return "{" .. table.concat(res, ",") .. "}"