Use native json support when available

This commit is contained in:
Tim Pope
2017-06-07 18:50:58 -04:00
parent feac2474db
commit 5b883e14ff

View File

@@ -75,6 +75,9 @@ function! s:credentials() abort
endfunction
function! rhubarb#json_parse(string) abort
if exists('*json_decode')
return json_decode(a:string)
endif
let [null, false, true] = ['', 0, 1]
let stripped = substitute(a:string,'\C"\(\\.\|[^"\\]\)*"','','g')
if stripped !~# "[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \n\r\t]"
@@ -87,6 +90,9 @@ function! rhubarb#json_parse(string) abort
endfunction
function! rhubarb#json_generate(object) abort
if exists('*json_encode')
return json_encode(a:object)
endif
if type(a:object) == type('')
return '"' . substitute(a:object, "[\001-\031\"\\\\]", '\=printf("\\u%04x", char2nr(submatch(0)))', 'g') . '"'
elseif type(a:object) == type([])