flycutter-zfz / vim

Automatically exported from code.google.com/p/vim
0 stars 0 forks source link

Add support for marshalling JSON #383

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Could vim have a built-in tojson({value}) and fromjson({json}) helpers to 
serialize and deserialize JSON?

JSON is used in lots of vim plugins like 
https://github.com/Valloric/YouCompleteMe, 
https://github.com/google/vim-maktaba, 
https://github.com/MarcWeber/vim-addon-manager, and eventually Vundle 
(https://github.com/VundleVim/Vundle.vim/pull/560). These can either use slow 
hacks or depend on python support, but it would be best if vim just had native, 
performant support for JSON marshalling built in.

Expected behavior
  :echo tojson({'a': [1, 'foo'], 'b': 2.1}) ==# '{"a": [1, "foo"], "b": 2.1}'
  1
  :echo fromjson("[1.0, {}, []]") ==# [1.0, {}, []]
  1
  :echo tojson(fromjson('[null, true, false]')) ==# '[null, true, false]'
  1

Note in the last example there needs to be a way to represent null, true, and 
false unambiguously even though vim doesn't have these primitives. Also, 
fromjson() could use an option to translate into standard vim equivalents like 
'', 1, and 0.

Original issue reported on code.google.com by daviebd...@gmail.com on 13 Jul 2015 at 11:30

GoogleCodeExporter commented 9 years ago
`null` is usually parsed in as zero, not as an empty string (depends on the 
plugin author obviously; I use zero, same does NeoVim for msgpack Nil). 
tojson/fromjson names break usual naming: it is mostly either get/set{smth} 
(getline/setline, etc) or {smth}{action} (foldopen, foldclose, …), so 
jsondump/parse are better. To dump/parse null, true, false unambigiously I 
propose a second argument: a dictionary looking like

    :let specialsdict = {'null': {}, 'true': {}, 'false': {}}
    :echo jsonparse("true", specialsdict) is# specialdicts.true
    1
    :echo jsondump([specialsdict.null, {}, 0, ''], specialsdict)
    [null, {}, 0, ""]

Note that 99% of JSON you may parse with eval like in VAM (it has a regex that 
verifies that parsing is safe). But you don’t get normal errors in this case. 
It also does not work with surrogate pairs.

Original comment by zyx....@gmail.com on 14 Jul 2015 at 5:28