From 517ecb85f58ed6ac8b4d5443931612e75e7c7dc2 Mon Sep 17 00:00:00 2001 From: Bartłomiej Maryńczak Date: Fri, 6 Dec 2024 21:43:41 +0100 Subject: feat(stdlib): vim.json.encode(...,{escape_slash:boolean}) #30561 Problem: vim.json.encode escapes every slash in string values (for example in file paths), and is not optional. Use-case is for preventing HTML injections (eg. injecting `` closing tag); in the context of Nvim this is rarely useful. Solution: - Add a `escape_slash` flag to `vim.json.encode`. - Defaults to `false`. (This is a "breaking" change, but more like a bug fix.) --- runtime/lua/vim/_meta/json.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/_meta/json.lua') diff --git a/runtime/lua/vim/_meta/json.lua b/runtime/lua/vim/_meta/json.lua index 07d89aafc8..1a7e87db9c 100644 --- a/runtime/lua/vim/_meta/json.lua +++ b/runtime/lua/vim/_meta/json.lua @@ -35,5 +35,8 @@ function vim.json.decode(str, opts) end --- Encodes (or "packs") Lua object {obj} as JSON in a Lua string. ---@param obj any +---@param opts? table Options table with keys: +--- - escape_slash: (boolean) (default false) When true, escapes `/` +--- character in JSON strings ---@return string -function vim.json.encode(obj) end +function vim.json.encode(obj, opts) end -- cgit From dc692f553aae367a03f286e0d59561247941f96c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 1 Jan 2025 12:29:51 -0800 Subject: docs: misc #31479 --- runtime/lua/vim/_meta/json.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'runtime/lua/vim/_meta/json.lua') diff --git a/runtime/lua/vim/_meta/json.lua b/runtime/lua/vim/_meta/json.lua index 1a7e87db9c..0d59de5fa6 100644 --- a/runtime/lua/vim/_meta/json.lua +++ b/runtime/lua/vim/_meta/json.lua @@ -25,18 +25,18 @@ vim.json = {} --- ---@param str string Stringified JSON data. ---@param opts? table Options table with keys: ---- - luanil: (table) Table with keys: ---- * object: (boolean) When true, converts `null` in JSON objects ---- to Lua `nil` instead of |vim.NIL|. ---- * array: (boolean) When true, converts `null` in JSON arrays ---- to Lua `nil` instead of |vim.NIL|. +--- - luanil: (table) Table with keys: +--- - object: (boolean) When true, converts `null` in JSON objects +--- to Lua `nil` instead of |vim.NIL|. +--- - array: (boolean) When true, converts `null` in JSON arrays +--- to Lua `nil` instead of |vim.NIL|. ---@return any function vim.json.decode(str, opts) end --- Encodes (or "packs") Lua object {obj} as JSON in a Lua string. ---@param obj any ---@param opts? table Options table with keys: ---- - escape_slash: (boolean) (default false) When true, escapes `/` ---- character in JSON strings +--- - escape_slash: (boolean) (default false) Escape slash +--- characters "/" in string values. ---@return string function vim.json.encode(obj, opts) end -- cgit