Skip to content

Commit da847e3

Browse files
committed
Web.JSON: Obsolute constants true, false and null.
1 parent 70d99ca commit da847e3

File tree

3 files changed

+32
-82
lines changed

3 files changed

+32
-82
lines changed

autoload/vital/__vital__/Web/JSON.vim

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,13 @@ let s:control_chars = {
5353
lockvar s:float_constants s:float_nan s:float_inf
5454
lockvar s:special_constants s:control_chars
5555

56-
function! s:_true() abort
57-
return v:true
58-
endfunction
59-
60-
function! s:_false() abort
61-
return v:false
62-
endfunction
63-
64-
function! s:_null() abort
65-
return v:null
66-
endfunction
67-
68-
function! s:_resolve(val, prefix) abort
69-
let t = type(a:val)
70-
if t == type('')
71-
let m = matchlist(a:val, '^' . a:prefix . '\(null\|true\|false\)$')
72-
if !empty(m)
73-
return s:const[m[1]]
74-
endif
75-
elseif t == type([]) || t == type({})
76-
return map(a:val, 's:_resolve(v:val, a:prefix)')
77-
endif
78-
return a:val
79-
endfunction
80-
8156
function! s:_vital_created(module) abort
8257
" define constant variables
8358
if !exists('s:const')
8459
let s:const = {}
85-
let s:const.true = function('s:_true')
86-
let s:const.false = function('s:_false')
87-
let s:const.null = function('s:_null')
60+
let s:const.true = v:true
61+
let s:const.false = v:false
62+
let s:const.null = v:null
8863
lockvar s:const
8964
endif
9065
call extend(a:module, s:const)
@@ -107,7 +82,6 @@ endfunction
10782
" @vimlint(EVL102, 1, l:Infinity)
10883
function! s:decode(json, ...) abort
10984
let settings = extend({
110-
\ 'use_token': 0,
11185
\ 'allow_nan': 1,
11286
\}, get(a:000, 0, {}))
11387
let json = iconv(a:json, 'utf-8', &encoding)
@@ -121,17 +95,8 @@ function! s:decode(json, ...) abort
12195
if settings.allow_nan
12296
let [NaN,Infinity] = [s:float_nan,s:float_inf]
12397
endif
124-
if settings.use_token
125-
let prefix = '__Web.JSON__'
126-
while stridx(json, prefix) != -1
127-
let prefix .= '_'
128-
endwhile
129-
let [null,true,false] = [prefix.'null',prefix.'true',prefix.'false']
130-
sandbox return s:_resolve(eval(json), prefix)
131-
else
132-
let [null,true,false] = [s:const.null(),s:const.true(),s:const.false()]
133-
sandbox return eval(json)
134-
endif
98+
let [null, true, false] = [v:null, v:true, v:false]
99+
sandbox return eval(json)
135100
endfunction
136101
" @vimlint(EVL102, 0, l:null)
137102
" @vimlint(EVL102, 0, l:true)

doc/vital/Web/JSON.txt

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,39 @@ INTERFACE *Vital.Web.JSON-interface*
2323
CONSTS *Vital.Web.JSON-consts*
2424

2525
true *Vital.Web.JSON.true*
26-
It is used to indicate 'true' in JSON string. It is represented as a
27-
|Funcref| thus if you assign the value to a variable which name does not
28-
start with a capital, "s:", "w:", "t:" or "b:" will raise an exception.
29-
This returns 1 when you use it as a function.
26+
It is |v:true|. This is left for backward compatibility.
3027

3128
false *Vital.Web.JSON.false*
32-
It is used to indicate 'false' in JSON string. It is represented as a
33-
|Funcref| thus if you assign the value to a variable which name does not
34-
start with a capital, "s:", "w:", "t:" or "b:" will raise an exception.
35-
This returns 0 when you use it as a function.
29+
It is |v:false|. This is left for backward compatibility.
3630

3731
null *Vital.Web.JSON.null*
38-
It is used to indicate 'null' in JSON string. It is represented as a
39-
|Funcref| thus if you assign the value to a variable which name does not
40-
start with a capital, "s:", "w:", "t:" or "b:" will raise an exception.
41-
This returns 0 when you use it as a function.
32+
It is |v:null|. This is left for backward compatibility.
4233

4334
------------------------------------------------------------------------------
4435
FUNCTIONS *Vital.Web.JSON-functions*
4536

4637
encode({object}[, {settings}]) *Vital.Web.JSON.encode()*
47-
Encode an object into a JSON string. Special tokens
48-
(e.g. |Vital.Web.JSON.true|) are encoded into corresponding javascript
49-
tokens (e.g. 'true').
50-
>
51-
echo s:JSON.encode([s:JSON.true, s:JSON.false, s:JSON.null])
52-
" => '[true, false, null]'
53-
<
38+
Encode an object into a JSON string.
39+
Vim values are converted as follows:
40+
|Number| decimal number
41+
|Float| floating point number
42+
Float nan "NaN"
43+
Float inf "Infinity"
44+
Float -inf "-Infinity"
45+
|String| in double quotes (possibly null)
46+
|List| as an array (possibly null); when
47+
used recursively: []
48+
|Dict| as an object (possibly null); when
49+
used recursively: {}
50+
|Blob| as an array of the individual bytes
51+
v:false "false"
52+
v:true "true"
53+
v:none "null"
54+
v:null "null"
55+
|Funcref| not possible, error
56+
|job| not possible, error
57+
|channel| not possible, error
58+
5459
{settings} is a |Dictionary| which allows the following:
5560

5661
'indent'
@@ -94,17 +99,6 @@ decode({json}[, {settings}]) *Vital.Web.JSON.decode()*
9499
Decode a JSON string into an object that vim can treat.
95100
{settings} is a |Dictionary| which allows the following:
96101

97-
'use_token'
98-
Use special tokens (e.g. |Vital.Web.JSON.true|) to represent
99-
corresponding javascript tokens (e.g. 'true').
100-
Otherwise |v:true|, |v:false| or |v:null| are used to represent these.
101-
The default value is 0.
102-
>
103-
echo s:JSON.decode('[true, false, null]')
104-
" => [1, 0, 0]
105-
echo s:JSON.decode('[true, false, null]', {'use_token': 1})
106-
" => [s:JSON.true, s:JSON.false, s:JSON.null]
107-
<
108102
'allow_nan'
109103
If 'allows_nan' is 0, it will raise an exception when deserializing
110104
float constants ('NaN', 'Infinity', '-Infinity').

test/Web/JSON.vimspec

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Describe Web.JSON
55

66
Describe .constants()
77
It should have constant variables which indicate the special tokens
8-
Assert Match(string(JSON.true), "function('\.*_true')")
9-
Assert Match(string(JSON.false), "function('\.*_false')")
10-
Assert Match(string(JSON.null), "function('\.*_null')")
8+
Assert Same(JSON.true, v:true)
9+
Assert Same(JSON.false, v:false)
10+
Assert Same(JSON.null, v:null)
1111
End
1212
End
1313

@@ -85,18 +85,9 @@ Describe Web.JSON
8585
End
8686

8787
It decodes special tokens (true/false/null)
88-
" true/false/null
89-
Assert Equals(JSON.decode('true'), 1)
90-
Assert Equals(JSON.decode('false'), 0)
91-
Assert Equals(JSON.decode('null'), 0)
9288
Assert Same(JSON.decode('true'), v:true)
9389
Assert Same(JSON.decode('false'), v:false)
9490
Assert Same(JSON.decode('null'), v:null)
95-
96-
let s = { 'use_token': 1 }
97-
Assert Equals(JSON.decode('true', s), JSON.true)
98-
Assert Equals(JSON.decode('false', s), JSON.false)
99-
Assert Equals(JSON.decode('null', s), JSON.null)
10091
End
10192

10293
It decodes special floats (NaN/Infinity/-Infinity)

0 commit comments

Comments
 (0)