aboutsummaryrefslogtreecommitdiff
path: root/test/functional/vimscript/json_functions_spec.lua
diff options
context:
space:
mode:
authorEmanuel <emanuel@empa.xyz>2023-12-06 16:56:04 +0100
committerGitHub <noreply@github.com>2023-12-06 07:56:04 -0800
commite057b38e7037808b3593fb1035794595b4e4a45e (patch)
treec746a875319d527094eafd2143b0d89296250463 /test/functional/vimscript/json_functions_spec.lua
parentc84af395e88ba143c19f7e34674bd222622e08ee (diff)
downloadrneovim-e057b38e7037808b3593fb1035794595b4e4a45e.tar.gz
rneovim-e057b38e7037808b3593fb1035794595b4e4a45e.tar.bz2
rneovim-e057b38e7037808b3593fb1035794595b4e4a45e.zip
fix(json): allow objects with empty keys #25564
Problem: Empty string is a valid JSON key, but json_decode() treats an object with empty key as ":help msgpack-special-dict". #20757 :echo json_decode('{"": "1"}') {'_TYPE': [], '_VAL': [['', '1']]} Note: vim returns `{'': '1'}`. Solution: Allow empty string as an object key. Note that we still (currently) disallow empty keys in object_to_vim() (since 7c01d5ff9286d262097484c680e3a4eab49e2911): https://github.com/neovim/neovim/blob/f64e4b43e1191ff30d902730f752875aa55682ce/src/nvim/api/private/converter.c#L333-L334 Fix #20757 Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Diffstat (limited to 'test/functional/vimscript/json_functions_spec.lua')
-rw-r--r--test/functional/vimscript/json_functions_spec.lua15
1 files changed, 8 insertions, 7 deletions
diff --git a/test/functional/vimscript/json_functions_spec.lua b/test/functional/vimscript/json_functions_spec.lua
index a9dab8431c..53899085e0 100644
--- a/test/functional/vimscript/json_functions_spec.lua
+++ b/test/functional/vimscript/json_functions_spec.lua
@@ -467,19 +467,18 @@ describe('json_decode() function', function()
'[1, {"d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]')
sp_decode_eq({1, {a={}, d={_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'a', 2}, {'c', 4}}}}},
'[1, {"a": [], "d": {"b": 3, "a": 1, "c": 4, "a": 2, "c": 4}}]')
- end)
-
- it('parses dictionaries with empty keys to special maps', function()
- sp_decode_eq({_TYPE='map', _VAL={{'', 4}}},
- '{"": 4}')
- sp_decode_eq({_TYPE='map', _VAL={{'b', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}},
- '{"b": 3, "a": 1, "c": 4, "d": 2, "": 4}')
sp_decode_eq({_TYPE='map', _VAL={{'', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}},
'{"": 3, "a": 1, "c": 4, "d": 2, "": 4}')
sp_decode_eq({{_TYPE='map', _VAL={{'', 3}, {'a', 1}, {'c', 4}, {'d', 2}, {'', 4}}}},
'[{"": 3, "a": 1, "c": 4, "d": 2, "": 4}]')
end)
+ it('parses dictionaries with empty keys', function()
+ eq({[""] = 4}, funcs.json_decode('{"": 4}'))
+ eq({b = 3, a = 1, c = 4, d = 2, [""] = 4},
+ funcs.json_decode('{"b": 3, "a": 1, "c": 4, "d": 2, "": 4}'))
+ end)
+
it('parses dictionaries with keys with NUL bytes to special maps', function()
sp_decode_eq({_TYPE='map', _VAL={{{_TYPE='string', _VAL={'a\n', 'b'}}, 4}}},
'{"a\\u0000\\nb": 4}')
@@ -577,6 +576,8 @@ describe('json_encode() function', function()
eq('{}', eval('json_encode({})'))
eq('{"d": []}', funcs.json_encode({d={}}))
eq('{"d": [], "e": []}', funcs.json_encode({d={}, e={}}))
+ -- Empty keys not allowed (yet?) in object_to_vim() (since 7c01d5ff9286). #25564
+ -- eq('{"": []}', funcs.json_encode({['']={}}))
end)
it('cannot dump generic mapping with generic mapping keys and values',