-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.vim
34 lines (31 loc) · 1.05 KB
/
json.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
" JSON parse / stringify
"
" Borrowed from tpope/vim-rhubarb plugin
"
" Copyright (c) Tim Pope. Distributed under the same terms as Vim itself. See :help license.
function JSON#parse(string)
let [null, false, true] = ['', 0, 1]
let stripped = substitute(a:string,'\C"\(\\.\|[^"\\]\)*"','','g')
if stripped !~# "[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \n\r\t]"
try
return eval(substitute(a:string,"[\r\n]"," ",'g'))
catch
endtry
endif
call s:throw("invalid JSON: ".stripped)
endfunction
function! JSON#stringify(object)
if type(a:object) == type('')
return '"' . substitute(a:object, "[\001-\031\"\\\\]", '\=printf("\\u%04x", char2nr(submatch(0)))', 'g') . '"'
elseif type(a:object) == type([])
return '['.join(map(copy(a:object), 's:json_generate(v:val)'),', ').']'
elseif type(a:object) == type({})
let pairs = []
for key in keys(a:object)
call add(pairs, s:json_generate(key) . ': ' . s:json_generate(a:object[key]))
endfor
return '{' . join(pairs, ', ') . '}'
else
return string(a:object)
endif
endfunction