eval: Rename json* functions to json_*

This commit is contained in:
ZyX
2016-02-13 21:39:28 +03:00
parent f1ced96c28
commit 2f67786796
6 changed files with 216 additions and 211 deletions

View File

@@ -1420,7 +1420,7 @@ v:exception The value of the exception most recently caught and not
*v:false* *false-variable* *v:false* *false-variable*
v:false Special value used to put "false" in JSON and msgpack. See v:false Special value used to put "false" in JSON and msgpack. See
|jsonencode()|. This value is converted to "false" when used |json_encode()|. This value is converted to "false" when used
as a String (e.g. in |expr5| with string concatenation as a String (e.g. in |expr5| with string concatenation
operator) and to zero when used as a Number (e.g. in |expr5| operator) and to zero when used as a Number (e.g. in |expr5|
or |expr7| when used with numeric operators). or |expr7| when used with numeric operators).
@@ -1566,7 +1566,7 @@ v:msgpack_types Dictionary containing msgpack types used by |msgpackparse()|
*v:null* *null-variable* *v:null* *null-variable*
v:null Special value used to put "null" in JSON and NIL in msgpack. v:null Special value used to put "null" in JSON and NIL in msgpack.
See |jsonencode()|. This value is converted to "null" when See |json_encode()|. This value is converted to "null" when
used as a String (e.g. in |expr5| with string concatenation used as a String (e.g. in |expr5| with string concatenation
operator) and to zero when used as a Number (e.g. in |expr5| operator) and to zero when used as a Number (e.g. in |expr5|
or |expr7| when used with numeric operators). or |expr7| when used with numeric operators).
@@ -1738,7 +1738,7 @@ v:throwpoint The point where the exception most recently caught and not
*v:true* *true-variable* *v:true* *true-variable*
v:true Special value used to put "true" in JSON and msgpack. See v:true Special value used to put "true" in JSON and msgpack. See
|jsonencode()|. This value is converted to "true" when used |json_encode()|. This value is converted to "true" when used
as a String (e.g. in |expr5| with string concatenation as a String (e.g. in |expr5| with string concatenation
operator) and to one when used as a Number (e.g. in |expr5| or operator) and to one when used as a Number (e.g. in |expr5| or
|expr7| when used with numeric operators). |expr7| when used with numeric operators).
@@ -1953,8 +1953,8 @@ jobstart( {cmd}[, {opts}]) Number Spawns {cmd} as a job
jobstop( {job}) Number Stops a job jobstop( {job}) Number Stops a job
jobwait( {ids}[, {timeout}]) Number Wait for a set of jobs jobwait( {ids}[, {timeout}]) Number Wait for a set of jobs
join( {list} [, {sep}]) String join {list} items into one String join( {list} [, {sep}]) String join {list} items into one String
jsondecode( {expr}) any Convert {expr} from JSON json_decode( {expr}) any Convert {expr} from JSON
jsonencode( {expr}) String Convert {expr} to JSON json_encode( {expr}) String Convert {expr} to JSON
keys( {dict}) List keys in {dict} keys( {dict}) List keys in {dict}
len( {expr}) Number the length of {expr} len( {expr}) Number the length of {expr}
libcall( {lib}, {func}, {arg}) String call {func} in library {lib} with {arg} libcall( {lib}, {func}, {arg}) String call {func} in library {lib} with {arg}
@@ -4314,12 +4314,12 @@ join({list} [, {sep}]) *join()*
converted into a string like with |string()|. converted into a string like with |string()|.
The opposite function is |split()|. The opposite function is |split()|.
jsondecode({expr}) *jsondecode()* json_decode({expr}) *json_decode()*
Convert {expr} from JSON object. Accepts |readfile()|-style Convert {expr} from JSON object. Accepts |readfile()|-style
list as the input, as well as regular string. May output any list as the input, as well as regular string. May output any
Vim value. When 'encoding' is not UTF-8 string is converted Vim value. When 'encoding' is not UTF-8 string is converted
from UTF-8 to 'encoding', failing conversion fails from UTF-8 to 'encoding', failing conversion fails
jsondecode(). In the following cases it will output json_decode(). In the following cases it will output
|msgpack-special-dict|: |msgpack-special-dict|:
1. Dictionary contains duplicate key. 1. Dictionary contains duplicate key.
2. Dictionary contains empty key. 2. Dictionary contains empty key.
@@ -4327,7 +4327,7 @@ jsondecode({expr}) *jsondecode()*
dictionary and for string will be emitted in case string dictionary and for string will be emitted in case string
with NUL byte was a dictionary key. with NUL byte was a dictionary key.
jsonencode({expr}) *jsonencode()* json_encode({expr}) *json_encode()*
Convert {expr} into a JSON string. Accepts Convert {expr} into a JSON string. Accepts
|msgpack-special-dict| as the input. Converts from 'encoding' |msgpack-special-dict| as the input. Converts from 'encoding'
to UTF-8 when encoding strings. Will not convert |Funcref|s, to UTF-8 when encoding strings. Will not convert |Funcref|s,

View File

@@ -102,15 +102,19 @@ are always available and may be used simultaneously in separate plugins. The
5. (internal) Trying to print or stringify VAR_UNKNOWN in Vim results in 5. (internal) Trying to print or stringify VAR_UNKNOWN in Vim results in
nothing, |E908|, in Neovim it is internal error. nothing, |E908|, in Neovim it is internal error.
|jsondecode()| behaviour changed: |json_decode()| behaviour changed:
1. It may output |msgpack-special-dict|. 1. It may output |msgpack-special-dict|.
2. It accepts only valid JSON. |v:none| is never emitted. 2. |msgpack-special-dict| is emitted also in case of duplicate keys, while in
|jsonencode()| behaviour slightly changed: now |msgpack-special-dict| values Vim it errors out.
are accepted. 3. It accepts only valid JSON. Trailing commas are not accepted.
*v:none* variable is absent. In Vim it represents “no value” in non-JSON |json_encode()| behaviour slightly changed: now |msgpack-special-dict| values
strings like "{"a": }" parsed as "{'a': v:none}". See |jsondecode()| and are accepted, but |v:none| is not.
|jsonencode()| incompatibilities above.
*v:none* variable is absent. In Vim it represents “no value” in “js” strings
like "[,]" parsed as "[v:none]" by |js_decode()|.
*js_encode()* and *js_decode()* functions are also absent.
Viminfo text files were replaced with binary (messagepack) ShaDa files. Viminfo text files were replaced with binary (messagepack) ShaDa files.
Additional differences: Additional differences:

View File

@@ -6784,8 +6784,8 @@ static struct fst {
{ "jobstop", 1, 1, f_jobstop }, { "jobstop", 1, 1, f_jobstop },
{ "jobwait", 1, 2, f_jobwait }, { "jobwait", 1, 2, f_jobwait },
{ "join", 1, 2, f_join }, { "join", 1, 2, f_join },
{ "jsondecode", 1, 1, f_jsondecode }, { "json_decode", 1, 1, f_json_decode },
{ "jsonencode", 1, 1, f_jsonencode }, { "json_encode", 1, 1, f_json_encode },
{ "keys", 1, 1, f_keys }, { "keys", 1, 1, f_keys },
{ "last_buffer_nr", 0, 0, f_last_buffer_nr }, // obsolete { "last_buffer_nr", 0, 0, f_last_buffer_nr }, // obsolete
{ "len", 1, 1, f_len }, { "len", 1, 1, f_len },
@@ -11583,8 +11583,8 @@ static void f_join(typval_T *argvars, typval_T *rettv)
rettv->vval.v_string = NULL; rettv->vval.v_string = NULL;
} }
/// jsondecode() function /// json_decode() function
static void f_jsondecode(typval_T *argvars, typval_T *rettv) static void f_json_decode(typval_T *argvars, typval_T *rettv)
{ {
char numbuf[NUMBUFLEN]; char numbuf[NUMBUFLEN];
char *s = NULL; char *s = NULL;
@@ -11614,8 +11614,8 @@ static void f_jsondecode(typval_T *argvars, typval_T *rettv)
xfree(tofree); xfree(tofree);
} }
/// jsonencode() function /// json_encode() function
static void f_jsonencode(typval_T *argvars, typval_T *rettv) static void f_json_encode(typval_T *argvars, typval_T *rettv)
{ {
rettv->v_type = VAR_STRING; rettv->v_type = VAR_STRING;
rettv->vval.v_string = (char_u *) encode_tv2json(&argvars[0], NULL); rettv->vval.v_string = (char_u *) encode_tv2json(&argvars[0], NULL);

View File

@@ -1020,7 +1020,7 @@ static inline int convert_to_json_string(garray_T *const gap,
"attempt to dump function reference"), \ "attempt to dump function reference"), \
mpstack, objname) mpstack, objname)
/// Check whether given key can be used in jsonencode() /// Check whether given key can be used in json_encode()
/// ///
/// @param[in] tv Key to check. /// @param[in] tv Key to check.
static inline bool check_json_key(const typval_T *const tv) static inline bool check_json_key(const typval_T *const tv)

View File

@@ -78,14 +78,15 @@ static int included_patches[] = {
1511, 1511,
1425, 1425,
1366, 1366,
1304,
1292, 1292,
1284, 1284,
// 1283 // 1283
1282, 1282,
// 1281 // 1281
// 1280 // 1280
// 1279 // 1279 NA
// 1278 // 1278 NA
// 1277 // 1277
// 1276 // 1276
// 1275 // 1275
@@ -94,7 +95,7 @@ static int included_patches[] = {
// 1272 // 1272
1271, 1271,
// 1270 // 1270
// 1269 1269,
// 1268 // 1268
1267, 1267,
// 1266 // 1266
@@ -120,7 +121,7 @@ static int included_patches[] = {
// 1246 // 1246
// 1245 // 1245
// 1244 // 1244
// 1243 // 1243 NA
// 1242 // 1242
// 1241 // 1241
// 1240 // 1240
@@ -132,7 +133,7 @@ static int included_patches[] = {
// 1234 // 1234
// 1233 // 1233
// 1232 // 1232
// 1231 // 1231 NA
// 1230 // 1230
// 1229 // 1229
1228, 1228,

View File

@@ -7,7 +7,7 @@ local eval = helpers.eval
local execute = helpers.execute local execute = helpers.execute
local exc_exec = helpers.exc_exec local exc_exec = helpers.exc_exec
describe('jsondecode() function', function() describe('json_decode() function', function()
local restart = function(cmd) local restart = function(cmd)
clear(cmd) clear(cmd)
execute([[ execute([[
@@ -61,7 +61,7 @@ describe('jsondecode() function', function()
end end
it('accepts readfile()-style list', function() it('accepts readfile()-style list', function()
eq({Test=1}, funcs.jsondecode({ eq({Test=1}, funcs.json_decode({
'{', '{',
'\t"Test": 1', '\t"Test": 1',
'}', '}',
@@ -69,7 +69,7 @@ describe('jsondecode() function', function()
end) end)
it('accepts strings with newlines', function() it('accepts strings with newlines', function()
eq({Test=1}, funcs.jsondecode([[ eq({Test=1}, funcs.json_decode([[
{ {
"Test": 1 "Test": 1
} }
@@ -77,242 +77,242 @@ describe('jsondecode() function', function()
end) end)
it('parses null, true, false', function() it('parses null, true, false', function()
eq(nil, funcs.jsondecode('null')) eq(nil, funcs.json_decode('null'))
eq(true, funcs.jsondecode('true')) eq(true, funcs.json_decode('true'))
eq(false, funcs.jsondecode('false')) eq(false, funcs.json_decode('false'))
end) end)
it('fails to parse incomplete null, true, false', function() it('fails to parse incomplete null, true, false', function()
eq('Vim(call):E474: Expected null: n', eq('Vim(call):E474: Expected null: n',
exc_exec('call jsondecode("n")')) exc_exec('call json_decode("n")'))
eq('Vim(call):E474: Expected null: nu', eq('Vim(call):E474: Expected null: nu',
exc_exec('call jsondecode("nu")')) exc_exec('call json_decode("nu")'))
eq('Vim(call):E474: Expected null: nul', eq('Vim(call):E474: Expected null: nul',
exc_exec('call jsondecode("nul")')) exc_exec('call json_decode("nul")'))
eq('Vim(call):E474: Expected null: nul\n\t', eq('Vim(call):E474: Expected null: nul\n\t',
exc_exec('call jsondecode("nul\\n\\t")')) exc_exec('call json_decode("nul\\n\\t")'))
eq('Vim(call):E474: Expected true: t', eq('Vim(call):E474: Expected true: t',
exc_exec('call jsondecode("t")')) exc_exec('call json_decode("t")'))
eq('Vim(call):E474: Expected true: tr', eq('Vim(call):E474: Expected true: tr',
exc_exec('call jsondecode("tr")')) exc_exec('call json_decode("tr")'))
eq('Vim(call):E474: Expected true: tru', eq('Vim(call):E474: Expected true: tru',
exc_exec('call jsondecode("tru")')) exc_exec('call json_decode("tru")'))
eq('Vim(call):E474: Expected true: tru\t\n', eq('Vim(call):E474: Expected true: tru\t\n',
exc_exec('call jsondecode("tru\\t\\n")')) exc_exec('call json_decode("tru\\t\\n")'))
eq('Vim(call):E474: Expected false: f', eq('Vim(call):E474: Expected false: f',
exc_exec('call jsondecode("f")')) exc_exec('call json_decode("f")'))
eq('Vim(call):E474: Expected false: fa', eq('Vim(call):E474: Expected false: fa',
exc_exec('call jsondecode("fa")')) exc_exec('call json_decode("fa")'))
eq('Vim(call):E474: Expected false: fal', eq('Vim(call):E474: Expected false: fal',
exc_exec('call jsondecode("fal")')) exc_exec('call json_decode("fal")'))
eq('Vim(call):E474: Expected false: fal <', eq('Vim(call):E474: Expected false: fal <',
exc_exec('call jsondecode(" fal <")')) exc_exec('call json_decode(" fal <")'))
eq('Vim(call):E474: Expected false: fals', eq('Vim(call):E474: Expected false: fals',
exc_exec('call jsondecode("fals")')) exc_exec('call json_decode("fals")'))
end) end)
it('parses integer numbers', function() it('parses integer numbers', function()
eq(100000, funcs.jsondecode('100000')) eq(100000, funcs.json_decode('100000'))
eq(-100000, funcs.jsondecode('-100000')) eq(-100000, funcs.json_decode('-100000'))
eq(100000, funcs.jsondecode(' 100000 ')) eq(100000, funcs.json_decode(' 100000 '))
eq(-100000, funcs.jsondecode(' -100000 ')) eq(-100000, funcs.json_decode(' -100000 '))
end) end)
it('fails to parse +numbers', function() it('fails to parse +numbers', function()
eq('Vim(call):E474: Unidentified byte: +1000', eq('Vim(call):E474: Unidentified byte: +1000',
exc_exec('call jsondecode("+1000")')) exc_exec('call json_decode("+1000")'))
end) end)
it('fails to parse negative numbers with space after -', function() it('fails to parse negative numbers with space after -', function()
eq('Vim(call):E474: Missing number after minus sign: - 1000', eq('Vim(call):E474: Missing number after minus sign: - 1000',
exc_exec('call jsondecode("- 1000")')) exc_exec('call json_decode("- 1000")'))
end) end)
it('fails to parse -', function() it('fails to parse -', function()
eq('Vim(call):E474: Missing number after minus sign: -', eq('Vim(call):E474: Missing number after minus sign: -',
exc_exec('call jsondecode("-")')) exc_exec('call json_decode("-")'))
end) end)
it('parses floating-point numbers', function() it('parses floating-point numbers', function()
eq('100000.0', eval('string(jsondecode("100000.0"))')) eq('100000.0', eval('string(json_decode("100000.0"))'))
eq(100000.5, funcs.jsondecode('100000.5')) eq(100000.5, funcs.json_decode('100000.5'))
eq(-100000.5, funcs.jsondecode('-100000.5')) eq(-100000.5, funcs.json_decode('-100000.5'))
eq(-100000.5e50, funcs.jsondecode('-100000.5e50')) eq(-100000.5e50, funcs.json_decode('-100000.5e50'))
eq(100000.5e50, funcs.jsondecode('100000.5e50')) eq(100000.5e50, funcs.json_decode('100000.5e50'))
eq(100000.5e50, funcs.jsondecode('100000.5e+50')) eq(100000.5e50, funcs.json_decode('100000.5e+50'))
eq(-100000.5e-50, funcs.jsondecode('-100000.5e-50')) eq(-100000.5e-50, funcs.json_decode('-100000.5e-50'))
eq(100000.5e-50, funcs.jsondecode('100000.5e-50')) eq(100000.5e-50, funcs.json_decode('100000.5e-50'))
end) end)
it('fails to parse incomplete floating-point numbers', function() it('fails to parse incomplete floating-point numbers', function()
eq('Vim(call):E474: Missing number after decimal dot: 0.', eq('Vim(call):E474: Missing number after decimal dot: 0.',
exc_exec('call jsondecode("0.")')) exc_exec('call json_decode("0.")'))
eq('Vim(call):E474: Missing exponent: 0.0e', eq('Vim(call):E474: Missing exponent: 0.0e',
exc_exec('call jsondecode("0.0e")')) exc_exec('call json_decode("0.0e")'))
eq('Vim(call):E474: Missing exponent: 0.0e+', eq('Vim(call):E474: Missing exponent: 0.0e+',
exc_exec('call jsondecode("0.0e+")')) exc_exec('call json_decode("0.0e+")'))
eq('Vim(call):E474: Missing exponent: 0.0e-', eq('Vim(call):E474: Missing exponent: 0.0e-',
exc_exec('call jsondecode("0.0e-")')) exc_exec('call json_decode("0.0e-")'))
end) end)
it('fails to parse floating-point numbers with spaces inside', function() it('fails to parse floating-point numbers with spaces inside', function()
eq('Vim(call):E474: Missing number after decimal dot: 0. ', eq('Vim(call):E474: Missing number after decimal dot: 0. ',
exc_exec('call jsondecode("0. ")')) exc_exec('call json_decode("0. ")'))
eq('Vim(call):E474: Missing number after decimal dot: 0. 0', eq('Vim(call):E474: Missing number after decimal dot: 0. 0',
exc_exec('call jsondecode("0. 0")')) exc_exec('call json_decode("0. 0")'))
eq('Vim(call):E474: Missing exponent: 0.0e 1', eq('Vim(call):E474: Missing exponent: 0.0e 1',
exc_exec('call jsondecode("0.0e 1")')) exc_exec('call json_decode("0.0e 1")'))
eq('Vim(call):E474: Missing exponent: 0.0e+ 1', eq('Vim(call):E474: Missing exponent: 0.0e+ 1',
exc_exec('call jsondecode("0.0e+ 1")')) exc_exec('call json_decode("0.0e+ 1")'))
eq('Vim(call):E474: Missing exponent: 0.0e- 1', eq('Vim(call):E474: Missing exponent: 0.0e- 1',
exc_exec('call jsondecode("0.0e- 1")')) exc_exec('call json_decode("0.0e- 1")'))
end) end)
it('fails to parse "," and ":"', function() it('fails to parse "," and ":"', function()
eq('Vim(call):E474: Comma not inside container: , ', eq('Vim(call):E474: Comma not inside container: , ',
exc_exec('call jsondecode(" , ")')) exc_exec('call json_decode(" , ")'))
eq('Vim(call):E474: Colon not inside container: : ', eq('Vim(call):E474: Colon not inside container: : ',
exc_exec('call jsondecode(" : ")')) exc_exec('call json_decode(" : ")'))
end) end)
it('parses empty containers', function() it('parses empty containers', function()
eq({}, funcs.jsondecode('[]')) eq({}, funcs.json_decode('[]'))
eq('[]', eval('string(jsondecode("[]"))')) eq('[]', eval('string(json_decode("[]"))'))
end) end)
it('fails to parse "[" and "{"', function() it('fails to parse "[" and "{"', function()
eq('Vim(call):E474: Unexpected end of input: {', eq('Vim(call):E474: Unexpected end of input: {',
exc_exec('call jsondecode("{")')) exc_exec('call json_decode("{")'))
eq('Vim(call):E474: Unexpected end of input: [', eq('Vim(call):E474: Unexpected end of input: [',
exc_exec('call jsondecode("[")')) exc_exec('call json_decode("[")'))
end) end)
it('fails to parse "}" and "]"', function() it('fails to parse "}" and "]"', function()
eq('Vim(call):E474: No container to close: ]', eq('Vim(call):E474: No container to close: ]',
exc_exec('call jsondecode("]")')) exc_exec('call json_decode("]")'))
eq('Vim(call):E474: No container to close: }', eq('Vim(call):E474: No container to close: }',
exc_exec('call jsondecode("}")')) exc_exec('call json_decode("}")'))
end) end)
it('fails to parse containers which are closed by different brackets', it('fails to parse containers which are closed by different brackets',
function() function()
eq('Vim(call):E474: Closing dictionary with bracket: ]', eq('Vim(call):E474: Closing dictionary with bracket: ]',
exc_exec('call jsondecode("{]")')) exc_exec('call json_decode("{]")'))
eq('Vim(call):E474: Closing list with figure brace: }', eq('Vim(call):E474: Closing list with figure brace: }',
exc_exec('call jsondecode("[}")')) exc_exec('call json_decode("[}")'))
end) end)
it('fails to parse concat inside container', function() it('fails to parse concat inside container', function()
eq('Vim(call):E474: Expected comma before list item: []]', eq('Vim(call):E474: Expected comma before list item: []]',
exc_exec('call jsondecode("[[][]]")')) exc_exec('call json_decode("[[][]]")'))
eq('Vim(call):E474: Expected comma before list item: {}]', eq('Vim(call):E474: Expected comma before list item: {}]',
exc_exec('call jsondecode("[{}{}]")')) exc_exec('call json_decode("[{}{}]")'))
eq('Vim(call):E474: Expected comma before list item: ]', eq('Vim(call):E474: Expected comma before list item: ]',
exc_exec('call jsondecode("[1 2]")')) exc_exec('call json_decode("[1 2]")'))
eq('Vim(call):E474: Expected comma before dictionary key: ": 4}', eq('Vim(call):E474: Expected comma before dictionary key: ": 4}',
exc_exec('call jsondecode("{\\"1\\": 2 \\"3\\": 4}")')) exc_exec('call json_decode("{\\"1\\": 2 \\"3\\": 4}")'))
eq('Vim(call):E474: Expected colon before dictionary value: , "3" 4}', eq('Vim(call):E474: Expected colon before dictionary value: , "3" 4}',
exc_exec('call jsondecode("{\\"1\\" 2, \\"3\\" 4}")')) exc_exec('call json_decode("{\\"1\\" 2, \\"3\\" 4}")'))
end) end)
it('fails to parse containers with leading comma or colon', function() it('fails to parse containers with leading comma or colon', function()
eq('Vim(call):E474: Leading comma: ,}', eq('Vim(call):E474: Leading comma: ,}',
exc_exec('call jsondecode("{,}")')) exc_exec('call json_decode("{,}")'))
eq('Vim(call):E474: Leading comma: ,]', eq('Vim(call):E474: Leading comma: ,]',
exc_exec('call jsondecode("[,]")')) exc_exec('call json_decode("[,]")'))
eq('Vim(call):E474: Using colon not in dictionary: :]', eq('Vim(call):E474: Using colon not in dictionary: :]',
exc_exec('call jsondecode("[:]")')) exc_exec('call json_decode("[:]")'))
eq('Vim(call):E474: Unexpected colon: :}', eq('Vim(call):E474: Unexpected colon: :}',
exc_exec('call jsondecode("{:}")')) exc_exec('call json_decode("{:}")'))
end) end)
it('fails to parse containers with trailing comma', function() it('fails to parse containers with trailing comma', function()
eq('Vim(call):E474: Trailing comma: ]', eq('Vim(call):E474: Trailing comma: ]',
exc_exec('call jsondecode("[1,]")')) exc_exec('call json_decode("[1,]")'))
eq('Vim(call):E474: Trailing comma: }', eq('Vim(call):E474: Trailing comma: }',
exc_exec('call jsondecode("{\\"1\\": 2,}")')) exc_exec('call json_decode("{\\"1\\": 2,}")'))
end) end)
it('fails to parse dictionaries with missing value', function() it('fails to parse dictionaries with missing value', function()
eq('Vim(call):E474: Expected value after colon: }', eq('Vim(call):E474: Expected value after colon: }',
exc_exec('call jsondecode("{\\"1\\":}")')) exc_exec('call json_decode("{\\"1\\":}")'))
eq('Vim(call):E474: Expected value: }', eq('Vim(call):E474: Expected value: }',
exc_exec('call jsondecode("{\\"1\\"}")')) exc_exec('call json_decode("{\\"1\\"}")'))
end) end)
it('fails to parse containers with two commas or colons', function() it('fails to parse containers with two commas or colons', function()
eq('Vim(call):E474: Duplicate comma: , "2": 2}', eq('Vim(call):E474: Duplicate comma: , "2": 2}',
exc_exec('call jsondecode("{\\"1\\": 1,, \\"2\\": 2}")')) exc_exec('call json_decode("{\\"1\\": 1,, \\"2\\": 2}")'))
eq('Vim(call):E474: Duplicate comma: , "2", 2]', eq('Vim(call):E474: Duplicate comma: , "2", 2]',
exc_exec('call jsondecode("[\\"1\\", 1,, \\"2\\", 2]")')) exc_exec('call json_decode("[\\"1\\", 1,, \\"2\\", 2]")'))
eq('Vim(call):E474: Duplicate colon: : 2}', eq('Vim(call):E474: Duplicate colon: : 2}',
exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":: 2}")')) exc_exec('call json_decode("{\\"1\\": 1, \\"2\\":: 2}")'))
eq('Vim(call):E474: Comma after colon: , 2}', eq('Vim(call):E474: Comma after colon: , 2}',
exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":, 2}")')) exc_exec('call json_decode("{\\"1\\": 1, \\"2\\":, 2}")'))
eq('Vim(call):E474: Unexpected colon: : "2": 2}', eq('Vim(call):E474: Unexpected colon: : "2": 2}',
exc_exec('call jsondecode("{\\"1\\": 1,: \\"2\\": 2}")')) exc_exec('call json_decode("{\\"1\\": 1,: \\"2\\": 2}")'))
eq('Vim(call):E474: Unexpected colon: :, "2": 2}', eq('Vim(call):E474: Unexpected colon: :, "2": 2}',
exc_exec('call jsondecode("{\\"1\\": 1:, \\"2\\": 2}")')) exc_exec('call json_decode("{\\"1\\": 1:, \\"2\\": 2}")'))
end) end)
it('fails to parse concat of two values', function() it('fails to parse concat of two values', function()
eq('Vim(call):E474: Trailing characters: []', eq('Vim(call):E474: Trailing characters: []',
exc_exec('call jsondecode("{}[]")')) exc_exec('call json_decode("{}[]")'))
end) end)
it('parses containers', function() it('parses containers', function()
eq({1}, funcs.jsondecode('[1]')) eq({1}, funcs.json_decode('[1]'))
eq({nil, 1}, funcs.jsondecode('[null, 1]')) eq({nil, 1}, funcs.json_decode('[null, 1]'))
eq({['1']=2}, funcs.jsondecode('{"1": 2}')) eq({['1']=2}, funcs.json_decode('{"1": 2}'))
eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}}, eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}},
funcs.jsondecode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}')) funcs.json_decode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}'))
end) end)
it('fails to parse incomplete strings', function() it('fails to parse incomplete strings', function()
eq('Vim(call):E474: Expected string end: \t"', eq('Vim(call):E474: Expected string end: \t"',
exc_exec('call jsondecode("\\t\\"")')) exc_exec('call json_decode("\\t\\"")'))
eq('Vim(call):E474: Expected string end: \t"abc', eq('Vim(call):E474: Expected string end: \t"abc',
exc_exec('call jsondecode("\\t\\"abc")')) exc_exec('call json_decode("\\t\\"abc")'))
eq('Vim(call):E474: Unfinished escape sequence: \t"abc\\', eq('Vim(call):E474: Unfinished escape sequence: \t"abc\\',
exc_exec('call jsondecode("\\t\\"abc\\\\")')) exc_exec('call json_decode("\\t\\"abc\\\\")'))
eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u', eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u',
exc_exec('call jsondecode("\\t\\"abc\\\\u")')) exc_exec('call json_decode("\\t\\"abc\\\\u")'))
eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u0', eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u0',
exc_exec('call jsondecode("\\t\\"abc\\\\u0")')) exc_exec('call json_decode("\\t\\"abc\\\\u0")'))
eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u00', eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u00',
exc_exec('call jsondecode("\\t\\"abc\\\\u00")')) exc_exec('call json_decode("\\t\\"abc\\\\u00")'))
eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000', eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000',
exc_exec('call jsondecode("\\t\\"abc\\\\u000")')) exc_exec('call json_decode("\\t\\"abc\\\\u000")'))
eq('Vim(call):E474: Expected four hex digits after \\u: \\u" ', eq('Vim(call):E474: Expected four hex digits after \\u: \\u" ',
exc_exec('call jsondecode("\\t\\"abc\\\\u\\" ")')) exc_exec('call json_decode("\\t\\"abc\\\\u\\" ")'))
eq('Vim(call):E474: Expected four hex digits after \\u: \\u0" ', eq('Vim(call):E474: Expected four hex digits after \\u: \\u0" ',
exc_exec('call jsondecode("\\t\\"abc\\\\u0\\" ")')) exc_exec('call json_decode("\\t\\"abc\\\\u0\\" ")'))
eq('Vim(call):E474: Expected four hex digits after \\u: \\u00" ', eq('Vim(call):E474: Expected four hex digits after \\u: \\u00" ',
exc_exec('call jsondecode("\\t\\"abc\\\\u00\\" ")')) exc_exec('call json_decode("\\t\\"abc\\\\u00\\" ")'))
eq('Vim(call):E474: Expected four hex digits after \\u: \\u000" ', eq('Vim(call):E474: Expected four hex digits after \\u: \\u000" ',
exc_exec('call jsondecode("\\t\\"abc\\\\u000\\" ")')) exc_exec('call json_decode("\\t\\"abc\\\\u000\\" ")'))
eq('Vim(call):E474: Expected string end: \t"abc\\u0000', eq('Vim(call):E474: Expected string end: \t"abc\\u0000',
exc_exec('call jsondecode("\\t\\"abc\\\\u0000")')) exc_exec('call json_decode("\\t\\"abc\\\\u0000")'))
end) end)
it('fails to parse unknown escape sequnces', function() it('fails to parse unknown escape sequnces', function()
eq('Vim(call):E474: Unknown escape sequence: \\a"', eq('Vim(call):E474: Unknown escape sequence: \\a"',
exc_exec('call jsondecode("\\t\\"\\\\a\\"")')) exc_exec('call json_decode("\\t\\"\\\\a\\"")'))
end) end)
it('parses strings properly', function() it('parses strings properly', function()
eq('\n', funcs.jsondecode('"\\n"')) eq('\n', funcs.json_decode('"\\n"'))
eq('', funcs.jsondecode('""')) eq('', funcs.json_decode('""'))
eq('\\/"\t\b\n\r\f', funcs.jsondecode([["\\\/\"\t\b\n\r\f"]])) eq('\\/"\t\b\n\r\f', funcs.json_decode([["\\\/\"\t\b\n\r\f"]]))
eq('/a', funcs.jsondecode([["\/a"]])) eq('/a', funcs.json_decode([["\/a"]]))
-- Unicode characters: 2-byte, 3-byte, 4-byte -- Unicode characters: 2-byte, 3-byte, 4-byte
eq({ eq({
'«', '«',
'', '',
'\xF0\x90\x80\x80', '\xF0\x90\x80\x80',
}, funcs.jsondecode({ }, funcs.json_decode({
'[', '[',
'"«",', '"«",',
'"ફ",', '"ફ",',
@@ -323,74 +323,74 @@ describe('jsondecode() function', function()
it('fails on strings with invalid bytes', function() it('fails on strings with invalid bytes', function()
eq('Vim(call):E474: Only UTF-8 strings allowed: \255"', eq('Vim(call):E474: Only UTF-8 strings allowed: \255"',
exc_exec('call jsondecode("\\t\\"\\xFF\\"")')) exc_exec('call json_decode("\\t\\"\\xFF\\"")'))
eq('Vim(call):E474: ASCII control characters cannot be present inside string: ', eq('Vim(call):E474: ASCII control characters cannot be present inside string: ',
exc_exec('call jsondecode(["\\"\\n\\""])')) exc_exec('call json_decode(["\\"\\n\\""])'))
-- 0xC2 starts 2-byte unicode character -- 0xC2 starts 2-byte unicode character
eq('Vim(call):E474: Only UTF-8 strings allowed: \194"', eq('Vim(call):E474: Only UTF-8 strings allowed: \194"',
exc_exec('call jsondecode("\\t\\"\\xC2\\"")')) exc_exec('call json_decode("\\t\\"\\xC2\\"")'))
-- 0xE0 0xAA starts 3-byte unicode character -- 0xE0 0xAA starts 3-byte unicode character
eq('Vim(call):E474: Only UTF-8 strings allowed: \224"', eq('Vim(call):E474: Only UTF-8 strings allowed: \224"',
exc_exec('call jsondecode("\\t\\"\\xE0\\"")')) exc_exec('call json_decode("\\t\\"\\xE0\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \224\170"', eq('Vim(call):E474: Only UTF-8 strings allowed: \224\170"',
exc_exec('call jsondecode("\\t\\"\\xE0\\xAA\\"")')) exc_exec('call json_decode("\\t\\"\\xE0\\xAA\\"")'))
-- 0xF0 0x90 0x80 starts 4-byte unicode character -- 0xF0 0x90 0x80 starts 4-byte unicode character
eq('Vim(call):E474: Only UTF-8 strings allowed: \240"', eq('Vim(call):E474: Only UTF-8 strings allowed: \240"',
exc_exec('call jsondecode("\\t\\"\\xF0\\"")')) exc_exec('call json_decode("\\t\\"\\xF0\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144"', eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144"',
exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\"")')) exc_exec('call json_decode("\\t\\"\\xF0\\x90\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"', eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"',
exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xF0\\x90\\x80\\"")'))
-- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character -- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character
eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"',
exc_exec('call jsondecode("\\t\\"\\xF9\\"")')) exc_exec('call json_decode("\\t\\"\\xF9\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"',
exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xF9\\x80\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\"")'))
-- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character -- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character
eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"',
exc_exec('call jsondecode("\\t\\"\\xFC\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"',
exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\x90\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"',
exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\"")'))
eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"', eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")'))
-- Specification does not allow unquoted characters above 0x10FFFF -- Specification does not allow unquoted characters above 0x10FFFF
eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"', eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")'))
eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"', eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"',
exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")')) exc_exec('call json_decode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")'))
-- '"\xF9\x80\x80\x80\x80"', -- '"\xF9\x80\x80\x80\x80"',
-- '"\xFC\x90\x80\x80\x80\x80"', -- '"\xFC\x90\x80\x80\x80\x80"',
end) end)
it('parses surrogate pairs properly', function() it('parses surrogate pairs properly', function()
eq('\xF0\x90\x80\x80', funcs.jsondecode('"\\uD800\\uDC00"')) eq('\xF0\x90\x80\x80', funcs.json_decode('"\\uD800\\uDC00"'))
eq('\xED\xA0\x80a\xED\xB0\x80', funcs.jsondecode('"\\uD800a\\uDC00"')) eq('\xED\xA0\x80a\xED\xB0\x80', funcs.json_decode('"\\uD800a\\uDC00"'))
eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.jsondecode('"\\uD800\\t\\uDC00"')) eq('\xED\xA0\x80\t\xED\xB0\x80', funcs.json_decode('"\\uD800\\t\\uDC00"'))
eq('\xED\xA0\x80', funcs.jsondecode('"\\uD800"')) eq('\xED\xA0\x80', funcs.json_decode('"\\uD800"'))
eq('\xED\xA0\x80a', funcs.jsondecode('"\\uD800a"')) eq('\xED\xA0\x80a', funcs.json_decode('"\\uD800a"'))
eq('\xED\xA0\x80\t', funcs.jsondecode('"\\uD800\\t"')) eq('\xED\xA0\x80\t', funcs.json_decode('"\\uD800\\t"'))
eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"'))
eq('\xED\xB0\x80a', funcs.jsondecode('"\\uDC00a"')) eq('\xED\xB0\x80a', funcs.json_decode('"\\uDC00a"'))
eq('\xED\xB0\x80\t', funcs.jsondecode('"\\uDC00\\t"')) eq('\xED\xB0\x80\t', funcs.json_decode('"\\uDC00\\t"'))
eq('\xED\xB0\x80', funcs.jsondecode('"\\uDC00"')) eq('\xED\xB0\x80', funcs.json_decode('"\\uDC00"'))
eq('a\xED\xB0\x80', funcs.jsondecode('"a\\uDC00"')) eq('a\xED\xB0\x80', funcs.json_decode('"a\\uDC00"'))
eq('\t\xED\xB0\x80', funcs.jsondecode('"\\t\\uDC00"')) eq('\t\xED\xB0\x80', funcs.json_decode('"\\t\\uDC00"'))
end) end)
local sp_decode_eq = function(expected, json) local sp_decode_eq = function(expected, json)
meths.set_var('__json', json) meths.set_var('__json', json)
speq(expected, 'jsondecode(g:__json)') speq(expected, 'json_decode(g:__json)')
execute('unlet! g:__json') execute('unlet! g:__json')
end end
@@ -441,47 +441,47 @@ describe('jsondecode() function', function()
it('converts strings to latin1 when &encoding is latin1', function() it('converts strings to latin1 when &encoding is latin1', function()
restart('set encoding=latin1') restart('set encoding=latin1')
eq('\xAB', funcs.jsondecode('"\\u00AB"')) eq('\xAB', funcs.json_decode('"\\u00AB"'))
sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"') sp_decode_eq({_TYPE='string', _VAL={'\n\xAB\n'}}, '"\\u0000\\u00AB\\u0000"')
end) end)
end) end)
describe('jsonencode() function', function() describe('json_encode() function', function()
before_each(clear) before_each(clear)
it('dumps strings', function() it('dumps strings', function()
eq('"Test"', funcs.jsonencode('Test')) eq('"Test"', funcs.json_encode('Test'))
eq('""', funcs.jsonencode('')) eq('""', funcs.json_encode(''))
eq('"\\t"', funcs.jsonencode('\t')) eq('"\\t"', funcs.json_encode('\t'))
eq('"\\n"', funcs.jsonencode('\n')) eq('"\\n"', funcs.json_encode('\n'))
eq('"\\u001B"', funcs.jsonencode('\27')) eq('"\\u001B"', funcs.json_encode('\27'))
eq('"þÿþ"', funcs.jsonencode('þÿþ')) eq('"þÿþ"', funcs.json_encode('þÿþ'))
end) end)
it('dumps numbers', function() it('dumps numbers', function()
eq('0', funcs.jsonencode(0)) eq('0', funcs.json_encode(0))
eq('10', funcs.jsonencode(10)) eq('10', funcs.json_encode(10))
eq('-10', funcs.jsonencode(-10)) eq('-10', funcs.json_encode(-10))
end) end)
it('dumps floats', function() it('dumps floats', function()
eq('0.0', eval('jsonencode(0.0)')) eq('0.0', eval('json_encode(0.0)'))
eq('10.5', funcs.jsonencode(10.5)) eq('10.5', funcs.json_encode(10.5))
eq('-10.5', funcs.jsonencode(-10.5)) eq('-10.5', funcs.json_encode(-10.5))
eq('-1.0e-5', funcs.jsonencode(-1e-5)) eq('-1.0e-5', funcs.json_encode(-1e-5))
eq('1.0e50', eval('jsonencode(1.0e50)')) eq('1.0e50', eval('json_encode(1.0e50)'))
end) end)
it('dumps lists', function() it('dumps lists', function()
eq('[]', funcs.jsonencode({})) eq('[]', funcs.json_encode({}))
eq('[[]]', funcs.jsonencode({{}})) eq('[[]]', funcs.json_encode({{}}))
eq('[[], []]', funcs.jsonencode({{}, {}})) eq('[[], []]', funcs.json_encode({{}, {}}))
end) end)
it('dumps dictionaries', function() it('dumps dictionaries', function()
eq('{}', eval('jsonencode({})')) eq('{}', eval('json_encode({})'))
eq('{"d": []}', funcs.jsonencode({d={}})) eq('{"d": []}', funcs.json_encode({d={}}))
eq('{"d": [], "e": []}', funcs.jsonencode({d={}, e={}})) eq('{"d": [], "e": []}', funcs.json_encode({d={}, e={}}))
end) end)
it('cannot dump generic mapping with generic mapping keys and values', it('cannot dump generic mapping with generic mapping keys and values',
@@ -490,148 +490,148 @@ describe('jsonencode() function', function()
execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('call add(todump._VAL, [todumpv1, todumpv2])') execute('call add(todump._VAL, [todumpv1, todumpv2])')
eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)'))
end) end)
it('cannot dump generic mapping with ext key', function() it('cannot dump generic mapping with ext key', function()
execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)'))
end) end)
it('cannot dump generic mapping with array key', function() it('cannot dump generic mapping with array key', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)'))
end) end)
it('cannot dump generic mapping with UINT64_MAX key', function() it('cannot dump generic mapping with UINT64_MAX key', function()
execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump = {"_TYPE": v:msgpack_types.integer}')
execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)'))
end) end)
it('cannot dump generic mapping with floating-point key', function() it('cannot dump generic mapping with floating-point key', function()
execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}') execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call json_encode(todump)'))
end) end)
it('can dump generic mapping with STR special key and NUL', function() it('can dump generic mapping with STR special key and NUL', function()
execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}') execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('{"\\u0000": 1}', eval('jsonencode(todump)')) eq('{"\\u0000": 1}', eval('json_encode(todump)'))
end) end)
it('can dump generic mapping with BIN special key and NUL', function() it('can dump generic mapping with BIN special key and NUL', function()
execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n"]}') execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n"]}')
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
eq('{"\\u0000": 1}', eval('jsonencode(todump)')) eq('{"\\u0000": 1}', eval('json_encode(todump)'))
end) end)
it('can dump STR special mapping with NUL and NL', function() it('can dump STR special mapping with NUL and NL', function()
execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}') execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}')
eq('"\\u0000\\n"', eval('jsonencode(todump)')) eq('"\\u0000\\n"', eval('json_encode(todump)'))
end) end)
it('can dump BIN special mapping with NUL and NL', function() it('can dump BIN special mapping with NUL and NL', function()
execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n", ""]}') execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n", ""]}')
eq('"\\u0000\\n"', eval('jsonencode(todump)')) eq('"\\u0000\\n"', eval('json_encode(todump)'))
end) end)
it('cannot dump special ext mapping', function() it('cannot dump special ext mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}') execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call jsonencode(todump)')) eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call json_encode(todump)'))
end) end)
it('can dump special array mapping', function() it('can dump special array mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}') execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
eq('[5, [""]]', eval('jsonencode(todump)')) eq('[5, [""]]', eval('json_encode(todump)'))
end) end)
it('can dump special UINT64_MAX mapping', function() it('can dump special UINT64_MAX mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump = {"_TYPE": v:msgpack_types.integer}')
execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]') execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
eq('18446744073709551615', eval('jsonencode(todump)')) eq('18446744073709551615', eval('json_encode(todump)'))
end) end)
it('can dump special INT64_MIN mapping', function() it('can dump special INT64_MIN mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.integer}') execute('let todump = {"_TYPE": v:msgpack_types.integer}')
execute('let todump._VAL = [-1, 2, 0, 0]') execute('let todump._VAL = [-1, 2, 0, 0]')
eq('-9223372036854775808', eval('jsonencode(todump)')) eq('-9223372036854775808', eval('json_encode(todump)'))
end) end)
it('can dump special BOOLEAN true mapping', function() it('can dump special BOOLEAN true mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}') execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}')
eq('true', eval('jsonencode(todump)')) eq('true', eval('json_encode(todump)'))
end) end)
it('can dump special BOOLEAN false mapping', function() it('can dump special BOOLEAN false mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}') execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}')
eq('false', eval('jsonencode(todump)')) eq('false', eval('json_encode(todump)'))
end) end)
it('can dump special NIL mapping', function() it('can dump special NIL mapping', function()
execute('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}') execute('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}')
eq('null', eval('jsonencode(todump)')) eq('null', eval('json_encode(todump)'))
end) end)
it('fails to dump a function reference', function() it('fails to dump a function reference', function()
eq('Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference', eq('Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference',
exc_exec('call jsonencode(function("tr"))')) exc_exec('call json_encode(function("tr"))'))
end) end)
it('fails to dump a function reference in a list', function() it('fails to dump a function reference in a list', function()
eq('Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference', eq('Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference',
exc_exec('call jsonencode([function("tr")])')) exc_exec('call json_encode([function("tr")])'))
end) end)
it('fails to dump a recursive list', function() it('fails to dump a recursive list', function()
execute('let todump = [[[]]]') execute('let todump = [[[]]]')
execute('call add(todump[0][0], todump)') execute('call add(todump[0][0], todump)')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode(todump)')) exc_exec('call json_encode(todump)'))
end) end)
it('fails to dump a recursive dict', function() it('fails to dump a recursive dict', function()
execute('let todump = {"d": {"d": {}}}') execute('let todump = {"d": {"d": {}}}')
execute('call extend(todump.d.d, {"d": todump})') execute('call extend(todump.d.d, {"d": todump})')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode([todump])')) exc_exec('call json_encode([todump])'))
end) end)
it('can dump dict with two same dicts inside', function() it('can dump dict with two same dicts inside', function()
execute('let inter = {}') execute('let inter = {}')
execute('let todump = {"a": inter, "b": inter}') execute('let todump = {"a": inter, "b": inter}')
eq('{"a": {}, "b": {}}', eval('jsonencode(todump)')) eq('{"a": {}, "b": {}}', eval('json_encode(todump)'))
end) end)
it('can dump list with two same lists inside', function() it('can dump list with two same lists inside', function()
execute('let inter = []') execute('let inter = []')
execute('let todump = [inter, inter]') execute('let todump = [inter, inter]')
eq('[[], []]', eval('jsonencode(todump)')) eq('[[], []]', eval('json_encode(todump)'))
end) end)
it('fails to dump a recursive list in a special dict', function() it('fails to dump a recursive list in a special dict', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, todump)') execute('call add(todump._VAL, todump)')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode(todump)')) exc_exec('call json_encode(todump)'))
end) end)
it('fails to dump a recursive (val) map in a special dict', function() it('fails to dump a recursive (val) map in a special dict', function()
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
execute('call add(todump._VAL, ["", todump])') execute('call add(todump._VAL, ["", todump])')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode([todump])')) exc_exec('call json_encode([todump])'))
end) end)
it('fails to dump a recursive (val) map in a special dict, _VAL reference', function() it('fails to dump a recursive (val) map in a special dict, _VAL reference', function()
execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}') execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}')
execute('call add(todump._VAL[0][1], todump._VAL)') execute('call add(todump._VAL[0][1], todump._VAL)')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode(todump)')) exc_exec('call json_encode(todump)'))
end) end)
it('fails to dump a recursive (val) special list in a special dict', it('fails to dump a recursive (val) special list in a special dict',
@@ -639,35 +639,35 @@ describe('jsonencode() function', function()
execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}') execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
execute('call add(todump._VAL, ["", todump._VAL])') execute('call add(todump._VAL, ["", todump._VAL])')
eq('Vim(call):E724: unable to correctly dump variable with self-referencing container', eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
exc_exec('call jsonencode(todump)')) exc_exec('call json_encode(todump)'))
end) end)
it('fails when called with no arguments', function() it('fails when called with no arguments', function()
eq('Vim(call):E119: Not enough arguments for function: jsonencode', eq('Vim(call):E119: Not enough arguments for function: json_encode',
exc_exec('call jsonencode()')) exc_exec('call json_encode()'))
end) end)
it('fails when called with two arguments', function() it('fails when called with two arguments', function()
eq('Vim(call):E118: Too many arguments for function: jsonencode', eq('Vim(call):E118: Too many arguments for function: json_encode',
exc_exec('call jsonencode(["", ""], 1)')) exc_exec('call json_encode(["", ""], 1)'))
end) end)
it('converts strings from latin1 when &encoding is latin1', function() it('converts strings from latin1 when &encoding is latin1', function()
clear('set encoding=latin1') clear('set encoding=latin1')
eq('"\\u00AB"', funcs.jsonencode('\xAB')) eq('"\\u00AB"', funcs.json_encode('\xAB'))
eq('"\\u0000\\u00AB\\u0000"', eval('jsonencode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})')) eq('"\\u0000\\u00AB\\u0000"', eval('json_encode({"_TYPE": v:msgpack_types.string, "_VAL": ["\\n\xAB\\n"]})'))
end) end)
it('ignores improper values in &isprint', function() it('ignores improper values in &isprint', function()
meths.set_option('isprint', '1') meths.set_option('isprint', '1')
eq(1, eval('"\x01" =~# "\\\\p"')) eq(1, eval('"\x01" =~# "\\\\p"'))
eq('"\\u0001"', funcs.jsonencode('\x01')) eq('"\\u0001"', funcs.json_encode('\x01'))
end) end)
it('fails when using surrogate character in a UTF-8 string', function() it('fails when using surrogate character in a UTF-8 string', function()
eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xA0\x80', eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xA0\x80',
exc_exec('call jsonencode("\xED\xA0\x80")')) exc_exec('call json_encode("\xED\xA0\x80")'))
eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF', eq('Vim(call):E474: UTF-8 string contains code point which belongs to a surrogate pair: \xED\xAF\xBF',
exc_exec('call jsonencode("\xED\xAF\xBF")')) exc_exec('call json_encode("\xED\xAF\xBF")'))
end) end)
end) end)