|
| 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 | +-- **INTERNAL USE ONLY**, this method is not meant to be public facing and |
| 33 | +-- will be removed at a future date without being considered a breaking change |
| 34 | +-- |
| 35 | +---@param jsonString string |
| 36 | +---@param options table |
| 37 | +--- * whitespace: |
| 38 | +--- - defaults to true |
| 39 | +--- - when true, comments will be replaced by whitespace |
| 40 | +--- - when false, comments will be stripped |
| 41 | +--- * trailing_commas: |
| 42 | +--- - defaults to false |
| 43 | +--- - when true, trailing commas will be included |
| 44 | +--- - when false, trailing commas will be removed |
| 45 | +function M.json_strip_comments(jsonString, options) |
| 46 | + options = options or {} |
| 47 | + local strip = options.whitespace == false and stripWithoutWhitespace or stripWithWhitespace |
| 48 | + local omitTrailingCommas = not options.trailing_commas |
| 49 | + |
| 50 | + local insideString = false |
| 51 | + local insideComment = false |
| 52 | + local offset = 1 |
| 53 | + local result = "" |
| 54 | + local skip = false |
| 55 | + local lastComma = 0 |
| 56 | + |
| 57 | + for i = 1, #jsonString, 1 do |
| 58 | + if skip then |
| 59 | + skip = false |
| 60 | + else |
| 61 | + local currentCharacter = jsonString:sub(i, i) |
| 62 | + local nextCharacter = jsonString:sub(i + 1, i + 1) |
| 63 | + |
| 64 | + if not insideComment and currentCharacter == '"' then |
| 65 | + local escaped = isEscaped(jsonString, i) |
| 66 | + if not escaped then insideString = not insideString end |
| 67 | + end |
| 68 | + |
| 69 | + if not insideString then |
| 70 | + if not insideComment and currentCharacter .. nextCharacter == "//" then |
| 71 | + result = result .. slice(jsonString, offset, i - 1) |
| 72 | + offset = i |
| 73 | + insideComment = singleComment |
| 74 | + skip = true |
| 75 | + elseif insideComment == singleComment and currentCharacter .. nextCharacter == "\r\n" then |
| 76 | + i = i + 1 |
| 77 | + skip = true |
| 78 | + insideComment = false |
| 79 | + result = result .. strip(jsonString, offset, i - 1) |
| 80 | + offset = i |
| 81 | + elseif insideComment == singleComment and currentCharacter == "\n" then |
| 82 | + insideComment = false |
| 83 | + result = result .. strip(jsonString, offset, i - 1) |
| 84 | + offset = i |
| 85 | + elseif not insideComment and currentCharacter .. nextCharacter == "/*" then |
| 86 | + result = result .. slice(jsonString, offset, i - 1) |
| 87 | + offset = i |
| 88 | + insideComment = multiComment |
| 89 | + skip = true |
| 90 | + elseif insideComment == multiComment and currentCharacter .. nextCharacter == "*/" then |
| 91 | + i = i + 1 |
| 92 | + skip = true |
| 93 | + insideComment = false |
| 94 | + result = result .. strip(jsonString, offset, i) |
| 95 | + offset = i + 1 |
| 96 | + elseif omitTrailingCommas and not insideComment then |
| 97 | + if currentCharacter == "," then |
| 98 | + lastComma = i |
| 99 | + elseif (currentCharacter == "]" or currentCharacter == "}") and lastComma > 0 then |
| 100 | + result = result .. slice(jsonString, offset, lastComma - 1) .. slice(jsonString, lastComma + 1, i) |
| 101 | + offset = i + 1 |
| 102 | + lastComma = 0 |
| 103 | + elseif currentCharacter:match "%S" then |
| 104 | + lastComma = 0 |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + return result .. (insideComment and strip(slice(jsonString, offset)) or slice(jsonString, offset)) |
| 112 | +end |
| 113 | + |
| 114 | +return M |
0 commit comments