Skip to content

Commit 34afccc

Browse files
committed
Web.JSON: Default to use vim-variables (e.g. v:true).
1 parent 80e1bbe commit 34afccc

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

autoload/vital/__vital__/Web/JSON.vim

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,24 @@ let s:float_nan = 0.0 / 0
4848
let s:float_inf = 1.0 / 0
4949
lockvar s:float_constants s:float_nan s:float_inf
5050

51+
let s:special_constants = {
52+
\ 'v:true': 'true',
53+
\ 'v:false': 'false',
54+
\ 'v:null': 'null',
55+
\ 'v:none': 'null',
56+
\ }
57+
lockvar s:special_constants
58+
5159
function! s:_true() abort
52-
return 1
60+
return v:true
5361
endfunction
5462

5563
function! s:_false() abort
56-
return 0
64+
return v:false
5765
endfunction
5866

5967
function! s:_null() abort
60-
return 0
68+
return v:null
6169
endfunction
6270

6371
function! s:_resolve(val, prefix) abort
@@ -169,6 +177,8 @@ function! s:encode(val, ...) abort
169177
throw 'vital: Web.JSON: Invalid float value: ' . val
170178
endif
171179
return val
180+
elseif t == 6 || t == 7
181+
return get(s:special_constants, a:val)
172182
else
173183
return string(a:val)
174184
endif

doc/vital/Web/JSON.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ decode({json}[, {settings}]) *Vital.Web.JSON.decode()*
8585
'use_token'
8686
Use special tokens (e.g. |Vital.Web.JSON.true|) to represent
8787
corresponding javascript tokens (e.g. 'true').
88-
Otherwise 1 or 0 are used to represent these.
88+
Otherwise |v:true|, |v:false| or |v:null| are used to represent these.
8989
The default value is 0.
9090
>
9191
echo s:JSON.decode('[true, false, null]')

test/Web/JSON.vimspec

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Describe Web.JSON
7575
Assert Equals(JSON.decode('true'), 1)
7676
Assert Equals(JSON.decode('false'), 0)
7777
Assert Equals(JSON.decode('null'), 0)
78+
Assert Same(JSON.decode('true'), v:true)
79+
Assert Same(JSON.decode('false'), v:false)
80+
Assert Same(JSON.decode('null'), v:null)
7881

7982
let s = { 'use_token': 1 }
8083
Assert Equals(JSON.decode('true', s), JSON.true)
@@ -241,6 +244,11 @@ Describe Web.JSON
241244
Assert Equals(JSON.encode(JSON.true), 'true')
242245
Assert Equals(JSON.encode(JSON.false), 'false')
243246
Assert Equals(JSON.encode(JSON.null), 'null')
247+
248+
Assert Equals(JSON.encode(v:true), 'true')
249+
Assert Equals(JSON.encode(v:false), 'false')
250+
Assert Equals(JSON.encode(v:null), 'null')
251+
Assert Equals(JSON.encode(v:none), 'null')
244252
End
245253

246254
It encodes special floats (NaN/Infinity/-Infinity)

0 commit comments

Comments
 (0)