Skip to content

Commit

Permalink
Implement json_encode
Browse files Browse the repository at this point in the history
  • Loading branch information
retorillo committed Jan 21, 2017
1 parent e494aef commit 1480389
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 38 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pack:
if [ ! -d dist ]; then mkdir dist; fi
cd .. && zip -r json.vim/dist/json.vim.zip json.vim/autoload
cd .. && unzip -l json.vim/dist/json.vim.zip
cd .. && zip -r json-ponyfill.vim/dist/json-ponyfill.vim.zip json-ponyfill.vim/autoload
cd .. && unzip -l json-ponyfill.vim/dist/json-ponyfill.vim.zip
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# json.vim
# json-ponyfill.vim

## Install (Pathogen)
Provide `json_decode` and `json_encode` to the former version of VIM.

```viml
function! s:json_decode(json)
if !exists("*json_decode")
return json_ponyfill#json_decode(a:json)
endif
return json_decode(a:json)
endfunction
function! s:json_encode(json)
if !exists("*json_encode")
return json_ponyfill#json_encode(a:json)
endif
return json_encode(a:json)
endfunction
```
git clone https://github.com/retorillo/json.vim ~/.vim/bundle/json.vim

## Install (Pathogen)

```bash
git clone https://github.com/retorillo/json-ponyfill.vim ~/.vim/bundle/json-ponyfill.vim
```

## License

MIT license
Distributed under the MIT license

Copyright (C) 2016 Retorillo
Copyright (C) 2016-2017 Retorillo
79 changes: 74 additions & 5 deletions autoload/retorillo/json.vim → autoload/json_ponyfill.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function! s:stackpop(list)
return ""
endfunction

function! retorillo#json#stackpush(list, item)
function! json_ponyfill#stackpush(list, item)
return s:stackpush(a:list, a:item)
endfunction
function! retorillo#json#stackpeek(list)
function! json_ponyfill#stackpeek(list)
return s:stackpeek(a:list)
endfunction
function! retorillo#json#stackpop(list)
function! json_ponyfill#stackpop(list)
return s:stackpop(a:list)
endfunction

Expand Down Expand Up @@ -206,6 +206,75 @@ function! s:parsejson(json, from)
throw "invalid format"
endfunction

function! retorillo#json#parse(json)
return s:parsejson(a:json, 0)["value"]
function! s:unparsenumber(val)
if type(a:val) == 0
return printf("%d", a:val)
else
return printf("%g", a:val)
endif
endfunction

function! s:unparsestring(val)
let sub = {
\ '"': '\\"',
\ '\t': '\\t',
\ '\n': '\\n',
\ '\r': '\\r',
\ }
let r = a:val
for k in keys(sub)
let r = substitute(r, k, sub[k], 'g')
endfor
return '"'.r.'"'
endfunction

function! s:unparselist(val)
let r = []
for i in a:val
call add(r, s:unparsejson(i))
unlet i
endfor
return join(['[', join(r, ','), ']'], '')
endfunction

function! s:unparsedict(val)
let r = []
for k in keys(a:val)
call add(r, s:unparsestring(k).':'.s:unparsejson(a:val[k]))
endfor
return join(['{', join(r, ','), '}'], '')
endfunction
function s:unparsebool(val)
if a:val
return 'true'
else
return 'false'
endif
endfunction

function! s:unparsejson(val)
let t = type(a:val)
if t == 0
return s:unparsenumber(a:val)
elseif t == 1
return s:unparsestring(a:val)
elseif t == 3
return s:unparselist(a:val)
elseif t == 4
return s:unparsedict(a:val)
elseif t == 5
return s:unparsenumber(a:val)
elseif t == 6
return s:unparsebool(a:val)
else
throw 'Cannot be string (vim type: '. val.')'
endif
endfunction

function! json_ponyfill#json_encode(val)
return s:unparsejson(a:val)
endfunction

function! json_ponyfill#json_decode(json)
return s:parsejson(a:json, 0)['value']
endfunction
65 changes: 47 additions & 18 deletions t/json.vim
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
describe "JSON Parse"
it "String"
Expect retorillo#json#parse('"foo"') == "foo"
describe "json_encode"
it "encode string"
Expect "\\n" == '\n'
Expect substitute('foo\n', '\n', '\\n', 'g') == 'foo\n'
Expect json_ponyfill#json_encode("foo\n") == "\"foo\\n\""
end
it "Number"
Expect retorillo#json#parse("12") == 12
Expect retorillo#json#parse("12.345") == 12.345
it "encode number"
Expect json_ponyfill#json_encode(12) == '12'
Expect json_ponyfill#json_encode(12.345) == '12.345'
end
it "Boolean"
Expect retorillo#json#parse("true") == 1
Expect retorillo#json#parse("false") == 0
it "encode array"
Expect json_ponyfill#json_encode(['foo', 'bar', 'baz']) == '["foo","bar","baz"]'
end
it "Array"
Expect retorillo#json#parse('["foo", "bar", "baz"]')
it "encode nested array"
Expect json_ponyfill#json_encode(['foo', 'bar', ['baz', 'foobar']]) == '["foo","bar",["baz","foobar"]]'
end
it "encode empty object"
Expect json_ponyfill#json_encode({}) == "{}"
end
it "encode simple object"
Expect json_ponyfill#json_encode({"foo": "bar"}) == '{"foo":"bar"}'
end
it "encode nested object"
Expect json_ponyfill#json_encode({
\ 'foo': ['bar', [1, 2, 3] , { 'foobar': 12.345 }]
\ }) == '{"foo":["bar",[1,2,3],{"foobar":12.345}]}'
end
end

describe "json_decode"
it "decode string"
Expect json_ponyfill#json_decode('"foo"') == "foo"
end
it "decode number"
Expect json_ponyfill#json_decode("12") == 12
Expect json_ponyfill#json_decode("12.345") == 12.345
end
it "decode boolean"
Expect json_ponyfill#json_decode("true") == 1
Expect json_ponyfill#json_decode("false") == 0
end
it "decode array"
Expect json_ponyfill#json_decode('["foo", "bar", "baz"]')
\ == ["foo", "bar", "baz"]
Expect retorillo#json#parse('[1, 2, 3]')
Expect json_ponyfill#json_decode('[1, 2, 3]')
\ == [1, 2, 3]
end
it "Object"
Expect retorillo#json#parse('{ "foo": "bar" }')
it "decode object"
Expect json_ponyfill#json_decode('{ "foo": "bar" }')
\ == { "foo": "bar" }
Expect retorillo#json#parse('{ "foo": { "bar": "baz" } }')
Expect json_ponyfill#json_decode('{ "foo": { "bar": "baz" } }')
\ == { "foo": { "bar": "baz" } }
Expect retorillo#json#parse('{ "foo" : { "bar" : "baz", "baz": [ 1, 2, 3 ] } }')
Expect json_ponyfill#json_decode('{ "foo" : { "bar" : "baz", "baz": [ 1, 2, 3 ] } }')
\ == { "foo": { "bar": "baz", "baz": [1, 2, 3] } }
end
it "Object (Failing)"
it "decode object (Failing)"
let exception = ""
try
call retorillo#json#parse('{ "foo" ":" "baz" }')
call json_ponyfill#json_decode('{ "foo" ":" "baz" }')
catch /^colon is expected/
let exception = v:exception
finally
Expand Down
16 changes: 8 additions & 8 deletions t/stack.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ describe "Stack"
unlet g:stack
end
it "json#stackpeek"
Expect retorillo#json#stackpeek(g:stack) == "bar"
Expect json_ponyfill#stackpeek(g:stack) == "bar"
end
it "retorillo#json#stackpush"
call retorillo#json#stackpush(g:stack, "baz")
Expect retorillo#json#stackpeek(g:stack) == "baz"
it "json_ponyfill#stackpush"
call json_ponyfill#stackpush(g:stack, "baz")
Expect json_ponyfill#stackpeek(g:stack) == "baz"
end
it "retorillo#json#stackpop"
Expect retorillo#json#stackpop(g:stack) == "bar"
it "json_ponyfill#stackpop"
Expect json_ponyfill#stackpop(g:stack) == "bar"
Expect len(g:stack) == 1
Expect retorillo#json#stackpop(g:stack) == "foo"
Expect json_ponyfill#stackpop(g:stack) == "foo"
Expect len(g:stack) == 0
Expect retorillo#json#stackpop(g:stack) == ""
Expect json_ponyfill#stackpop(g:stack) == ""
end
end

0 comments on commit 1480389

Please sign in to comment.