Skip to content

Commit 92c274c

Browse files
committed
Web.JSON: Convert control chars.
1 parent 869b9e3 commit 92c274c

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

autoload/vital/__vital__/Web/JSON.vim

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
let s:save_cpo = &cpo
22
set cpo&vim
33

4+
let s:control_chars = {
5+
\ '\': '\\',
6+
\ '"': '\"',
7+
\ "\x01": '\u0001',
8+
\ "\x02": '\u0002',
9+
\ "\x03": '\u0003',
10+
\ "\x04": '\u0004',
11+
\ "\x05": '\u0005',
12+
\ "\x06": '\u0006',
13+
\ "\x07": '\u0007',
14+
\ "\x08": '\b',
15+
\ "\x09": '\t',
16+
\ "\x0a": '\n',
17+
\ "\x0b": '\u000b',
18+
\ "\x0c": '\f',
19+
\ "\x0d": '\r',
20+
\ "\x0e": '\u000e',
21+
\ "\x0f": '\u000f',
22+
\ "\x10": '\u0010',
23+
\ "\x11": '\u0011',
24+
\ "\x12": '\u0012',
25+
\ "\x13": '\u0013',
26+
\ "\x14": '\u0014',
27+
\ "\x15": '\u0015',
28+
\ "\x16": '\u0016',
29+
\ "\x17": '\u0017',
30+
\ "\x18": '\u0018',
31+
\ "\x19": '\u0019',
32+
\ "\x1a": '\u001a',
33+
\ "\x1b": '\u001b',
34+
\ "\x1c": '\u001c',
35+
\ "\x1d": '\u001d',
36+
\ "\x1e": '\u001e',
37+
\ "\x1f": '\u001f',
38+
\ }
39+
lockvar s:control_chars
40+
441
function! s:_true() abort
542
return 1
643
endfunction
@@ -83,11 +120,9 @@ function! s:encode(val, ...) abort
83120
if type(a:val) == 0
84121
return a:val
85122
elseif type(a:val) == 1
86-
let json = '"' . escape(a:val, '\"') . '"'
87-
let json = substitute(json, "\r", '\\r', 'g')
88-
let json = substitute(json, "\n", '\\n', 'g')
89-
let json = substitute(json, "\t", '\\t', 'g')
90-
return iconv(json, &encoding, 'utf-8')
123+
let s = substitute(a:val, '[\x01-\x1f\\"]', '\=s:control_chars[submatch(0)]', 'g')
124+
let s = iconv(s, &encoding, 'utf-8')
125+
return '"' . s . '"'
91126
elseif type(a:val) == 2
92127
if s:const.true == a:val
93128
return 'true'

test/Web/JSON.vimspec

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Describe Web.JSON
2929
\ JSON.decode('"He said \"I''m a vimmer\""'),
3030
\ 'He said "I''m a vimmer"'
3131
\)
32+
" control chars
33+
Assert Equals(JSON.decode('"\u0001\u0002\u0003\u0004\u0005\u0006\u0007"'), "\x01\x02\x03\x04\x05\x06\x07")
34+
Assert Equals(JSON.decode('"\b\t\n\u000b\f\r\u000e\u000f"'), "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")
35+
Assert Equals(JSON.decode('"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"'), "\x10\x11\x12\x13\x14\x15\x16\x17")
36+
Assert Equals(JSON.decode('"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"'), "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f")
3237
" there should be iconv tests as well
3338
End
3439

@@ -91,6 +96,11 @@ Describe Web.JSON
9196
\ JSON.encode('He said "I''m a vimmer"'),
9297
\ '"He said \"I''m a vimmer\""'
9398
\)
99+
" control chars
100+
Assert Equals(JSON.encode("\x01\x02\x03\x04\x05\x06\x07"), '"\u0001\u0002\u0003\u0004\u0005\u0006\u0007"')
101+
Assert Equals(JSON.encode("\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"), '"\b\t\n\u000b\f\r\u000e\u000f"')
102+
Assert Equals(JSON.encode("\x10\x11\x12\x13\x14\x15\x16\x17"), '"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"')
103+
Assert Equals(JSON.encode("\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"), '"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"')
94104
" there should be iconv tests as well
95105
End
96106

0 commit comments

Comments
 (0)