Skip to content

Commit 1de64b3

Browse files
committed
feat: add json module for cleaning up json
This is a hack until PR is accepted upstream to Plenary
1 parent 5481cc3 commit 1de64b3

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

lua/astrocore/json.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
-- based on https://github.com/nvim-lua/plenary.nvim/blob/a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683/lua/plenary/json.lua
2+
-- remove once PR accepted: https://github.com/nvim-lua/plenary.nvim/pull/613
3+
4+
local singleComment = "singleComment"
5+
local multiComment = "multiComment"
6+
local stripWithoutWhitespace = function() return "" end
7+
8+
local function slice(str, from, to)
9+
from = from or 1
10+
to = to or #str
11+
return str:sub(from, to)
12+
end
13+
14+
local stripWithWhitespace = function(str, from, to) return slice(str, from, to):gsub("%S", " ") end
15+
16+
local isEscaped = function(jsonString, quotePosition)
17+
local index = quotePosition - 1
18+
local backslashCount = 0
19+
20+
while jsonString:sub(index, index) == "\\" do
21+
index = index - 1
22+
backslashCount = backslashCount + 1
23+
end
24+
return backslashCount % 2 == 1 and true or false
25+
end
26+
27+
local M = {}
28+
29+
-- Strips any json comments from a json string.
30+
-- The resulting string can then be used by `vim.fn.json_decode`
31+
--
32+
---@param jsonString string
33+
---@param options table
34+
--- * whitespace:
35+
--- - defaults to true
36+
--- - when true, comments will be replaced by whitespace
37+
--- - when false, comments will be stripped
38+
--- * trailing_commas:
39+
--- - defaults to false
40+
--- - when true, trailing commas will be included
41+
--- - when false, trailing commas will be removed
42+
function M.json_strip_comments(jsonString, options)
43+
options = options or {}
44+
local strip = options.whitespace == false and stripWithoutWhitespace or stripWithWhitespace
45+
local omitTrailingCommas = not options.trailing_commas
46+
47+
local insideString = false
48+
local insideComment = false
49+
local offset = 1
50+
local result = ""
51+
local skip = false
52+
local lastComma = 0
53+
54+
for i = 1, #jsonString, 1 do
55+
if skip then
56+
skip = false
57+
else
58+
local currentCharacter = jsonString:sub(i, i)
59+
local nextCharacter = jsonString:sub(i + 1, i + 1)
60+
61+
if not insideComment and currentCharacter == '"' then
62+
local escaped = isEscaped(jsonString, i)
63+
if not escaped then insideString = not insideString end
64+
end
65+
66+
if not insideString then
67+
if not insideComment and currentCharacter .. nextCharacter == "//" then
68+
result = result .. slice(jsonString, offset, i - 1)
69+
offset = i
70+
insideComment = singleComment
71+
skip = true
72+
elseif insideComment == singleComment and currentCharacter .. nextCharacter == "\r\n" then
73+
i = i + 1
74+
skip = true
75+
insideComment = false
76+
result = result .. strip(jsonString, offset, i - 1)
77+
offset = i
78+
elseif insideComment == singleComment and currentCharacter == "\n" then
79+
insideComment = false
80+
result = result .. strip(jsonString, offset, i - 1)
81+
offset = i
82+
elseif not insideComment and currentCharacter .. nextCharacter == "/*" then
83+
result = result .. slice(jsonString, offset, i - 1)
84+
offset = i
85+
insideComment = multiComment
86+
skip = true
87+
elseif insideComment == multiComment and currentCharacter .. nextCharacter == "*/" then
88+
i = i + 1
89+
skip = true
90+
insideComment = false
91+
result = result .. strip(jsonString, offset, i)
92+
offset = i + 1
93+
elseif omitTrailingCommas and not insideComment then
94+
if currentCharacter == "," then
95+
lastComma = i
96+
elseif (currentCharacter == "]" or currentCharacter == "}") and lastComma > 0 then
97+
result = result .. slice(jsonString, offset, lastComma - 1) .. slice(jsonString, lastComma + 1, i)
98+
offset = i + 1
99+
lastComma = 0
100+
elseif currentCharacter:match "%S" then
101+
lastComma = 0
102+
end
103+
end
104+
end
105+
end
106+
end
107+
108+
return result .. (insideComment and strip(slice(jsonString, offset)) or slice(jsonString, offset))
109+
end
110+
111+
return M

0 commit comments

Comments
 (0)