json.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ---@meta
  2. ---@nodoc
  3. vim.json = {}
  4. -- luacheck: no unused args
  5. ---@brief
  6. ---
  7. --- This module provides encoding and decoding of Lua objects to and
  8. --- from JSON-encoded strings. Supports |vim.NIL| and |vim.empty_dict()|.
  9. --- Decodes (or "unpacks") the JSON-encoded {str} to a Lua object.
  10. ---
  11. --- - Decodes JSON "null" as |vim.NIL| (controllable by {opts}, see below).
  12. --- - Decodes empty object as |vim.empty_dict()|.
  13. --- - Decodes empty array as `{}` (empty Lua table).
  14. ---
  15. --- Example:
  16. ---
  17. --- ```lua
  18. --- vim.print(vim.json.decode('{"bar":[],"foo":{},"zub":null}'))
  19. --- -- { bar = {}, foo = vim.empty_dict(), zub = vim.NIL }
  20. --- ```
  21. ---
  22. ---@param str string Stringified JSON data.
  23. ---@param opts? table<string,any> Options table with keys:
  24. --- - luanil: (table) Table with keys:
  25. --- * object: (boolean) When true, converts `null` in JSON objects
  26. --- to Lua `nil` instead of |vim.NIL|.
  27. --- * array: (boolean) When true, converts `null` in JSON arrays
  28. --- to Lua `nil` instead of |vim.NIL|.
  29. ---@return any
  30. function vim.json.decode(str, opts) end
  31. --- Encodes (or "packs") Lua object {obj} as JSON in a Lua string.
  32. ---@param obj any
  33. ---@param opts? table<string,any> Options table with keys:
  34. --- - escape_slash: (boolean) (default false) When true, escapes `/`
  35. --- character in JSON strings
  36. ---@return string
  37. function vim.json.encode(obj, opts) end