Skip to content

Commit

Permalink
Alternative processing by python
Browse files Browse the repository at this point in the history
  • Loading branch information
retorillo committed Apr 5, 2017
1 parent 2de2ce8 commit dfc71c7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@ endfunction
git clone https://github.com/retorillo/json-ponyfill.vim ~/.vim/bundle/json-ponyfill.vim
```

## Displaying progress bar
## Performance problem and workarounds

`json_ponyfill#json_decode` will take a long time when parsing large JSON.
`json_ponyfill#json_decode` and `json_ponyfill#json_encode` will take a long
time working with large JSON.

Of course my code may be not sophisticated, but originally Vim Script is not
good for large processing.

In this case, consider to use `json_ponlyfill#json_decode(json, { 'progress': 1 })`
Consider to use the following workarounds:

### Using python if possible

Use `json_ponyfill#json_decode(json, { 'python': 1 })`
to use python if possible.

Python can process faster than Vim Script.

### Displaying progress bar

Use `json_ponyfill#json_decode(json, { 'progress': 1 })`
to display progress bar like below:

```
Expand All @@ -42,6 +54,12 @@ json_decode 50% [=========================.........................]
wipe previously printed all echo message out. See `:h redraw`, `:h messages`,
`:h echomsg`, and `:h echoerr` to learn more.

### Combining the above workarounds

Of course, the above options can be combined like:
`json_ponyfill#json_decode(json, { 'progress': 1, 'python': 1 }`.
But note that progress option will be ignored if python is available.

## Unit testing (For plugin developers)

`test/test.vim` is a bit useful snippet for unit testing.
Expand Down
25 changes: 25 additions & 0 deletions autoload/json_ponyfill.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let s:cpo = &cpo
set cpo&vim

function! json_ponyfill#json_decode(val, ...)
let option = len(a:000) > 0 ? a:000[0] : {}
if has_key(option, 'python')
\ && option.python
\ && json_ponyfill#python#ready()
return json_ponyfill#python#json_decode(a:val)
endif
return json_ponyfill#native#json_decode(a:val, option)
endfunction

function! json_ponyfill#json_encode(val, ...)
let option = len(a:000) > 0 ? a:000[0] : {}
if has_key(option, 'python')
\ && option.python
\ && json_ponyfill#python#ready()
return json_ponyfill#python#json_encode(a:val)
endif
return json_ponyfill#native#json_encode(a:val, option)
endfunction

let &cpo = s:cpo
unlet s:cpo
42 changes: 42 additions & 0 deletions autoload/json_ponyfill/python.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let s:cpo = &cpo
set cpo&vim

function! json_ponyfill#python#ready()
if !has('python')
return 0
endif
python << endpython
import vim;
try:
__import__('json');
vim.command('let ready = 1')
except ImportError:
vim.command('let ready = 0')
endpython
return ready
endfunction

function! json_ponyfill#python#json_decode(value)
let bind = {}
python << endpython
import vim;
import json;
bind = vim.bindeval('bind');
bind.update({'value': json.loads(vim.eval('a:value'))});
endpython
return bind.value
endfunction

function! json_ponyfill#python#json_encode(value)
let bind = {}
python << endpython
import vim;
import json;
bind = vim.bindeval('bind');
bind.update({'value': json.dumps(vim.eval('a:value'))});
endpython
return bind.value
endfunction

let &cpo = s:cpo
unlet s:cpo
15 changes: 12 additions & 3 deletions test/test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ call s:describe("json_ponyfill#stack")
call s:itshouldeql("json#stackpeek",
\ json_ponyfill#stack#peek(s:stack),
\ "bar")

let s:stack = [ "foo", "bar" ]
call json_ponyfill#stack#push(s:stack, "baz")
call s:itshouldeql("json_ponyfill#stack#push",
\ json_ponyfill#stack#peek(s:stack),
\ "baz")

let s:stack = [ "foo", "bar" ]
call s:itshouldeql('json_ponyfill#stack#pop #1',
\ json_ponyfill#stack#pop(s:stack),
Expand Down Expand Up @@ -141,7 +141,7 @@ call s:describe("json_ponyfill#native#json_encode")
call s:itshouldeql('decode object #3',
\ json_ponyfill#native#json_decode('{ "foo" : { "bar" : "baz", "baz": [ 1, 2, 3 ] } }'),
\ { "foo": { "bar": "baz", "baz": [1, 2, 3] } })

let throwexception = 0
try
call json_ponyfill#native#json_decode('{ "foo" ":" "baz" }')
Expand All @@ -153,5 +153,14 @@ call s:describe("json_ponyfill#native#json_encode")
endtry
call s:enddescribe()

call s:describe("json_ponyfill#python")
if json_ponyfill#python#ready()
call s:itshouldeql('decode', json_ponyfill#python#json_decode('{ "foo": "bar" }'), { "foo": "bar" })
call s:itshouldeql('encode', json_ponyfill#python#json_encode({ "foo": "bar" }), '{"foo": "bar"}')
else
s:failing('No python or json module. Skipped python testcase.', '-python', '+python')
endif
call s:enddescribe()

let &cpo = s:cpo
unlet s:cpo

0 comments on commit dfc71c7

Please sign in to comment.