Skip to content

Commit c661d01

Browse files
committed
Web.JSON: Obsolute constants true, false and null.
1 parent 3044278 commit c661d01

File tree

3 files changed

+32
-90
lines changed

3 files changed

+32
-90
lines changed

autoload/vital/__vital__/Web/JSON.vim

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,13 @@ let s:special_constants = {
5656
\ }
5757
lockvar s:special_constants
5858

59-
function! s:_true() abort
60-
return v:true
61-
endfunction
62-
63-
function! s:_false() abort
64-
return v:false
65-
endfunction
66-
67-
function! s:_null() abort
68-
return v:null
69-
endfunction
70-
71-
function! s:_resolve(val, prefix) abort
72-
let t = type(a:val)
73-
if t == type('')
74-
let m = matchlist(a:val, '^' . a:prefix . '\(null\|true\|false\)$')
75-
if !empty(m)
76-
return s:const[m[1]]
77-
endif
78-
elseif t == type([]) || t == type({})
79-
return map(a:val, 's:_resolve(v:val, a:prefix)')
80-
endif
81-
return a:val
82-
endfunction
83-
8459
function! s:_vital_created(module) abort
8560
" define constant variables
8661
if !exists('s:const')
8762
let s:const = {}
88-
let s:const.true = function('s:_true')
89-
let s:const.false = function('s:_false')
90-
let s:const.null = function('s:_null')
63+
let s:const.true = v:true
64+
let s:const.false = v:false
65+
let s:const.null = v:null
9166
lockvar s:const
9267
endif
9368
call extend(a:module, s:const)
@@ -110,7 +85,6 @@ endfunction
11085
" @vimlint(EVL102, 1, l:Infinity)
11186
function! s:decode(json, ...) abort
11287
let settings = extend({
113-
\ 'use_token': 0,
11488
\ 'allow_nan': 1,
11589
\}, get(a:000, 0, {}))
11690
let json = iconv(a:json, 'utf-8', &encoding)
@@ -124,17 +98,8 @@ function! s:decode(json, ...) abort
12498
if settings.allow_nan
12599
let [NaN,Infinity] = [s:float_nan,s:float_inf]
126100
endif
127-
if settings.use_token
128-
let prefix = '__Web.JSON__'
129-
while stridx(json, prefix) != -1
130-
let prefix .= '_'
131-
endwhile
132-
let [null,true,false] = map(['null','true','false'], 'prefix . v:val')
133-
sandbox return s:_resolve(eval(json), prefix)
134-
else
135-
let [null,true,false] = [s:const.null(),s:const.true(),s:const.false()]
136-
sandbox return eval(json)
137-
endif
101+
let [null, true, false] = [v:null, v:true, v:false]
102+
sandbox return eval(json)
138103
endfunction
139104
" @vimlint(EVL102, 0, l:null)
140105
" @vimlint(EVL102, 0, l:true)
@@ -176,14 +141,6 @@ function! s:_encode(val, settings) abort
176141
let s = iconv(a:val, a:settings.from_encoding, 'utf-8')
177142
let s = substitute(s, '[\x01-\x1f\\"]', '\=s:control_chars[submatch(0)]', 'g')
178143
return '"' . s . '"'
179-
elseif t == 2
180-
if s:const.true == a:val
181-
return 'true'
182-
elseif s:const.false == a:val
183-
return 'false'
184-
elseif s:const.null == a:val
185-
return 'null'
186-
endif
187144
elseif t == 3
188145
return s:_encode_list(a:val, a:settings)
189146
elseif t == 4

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)