@@ -15,8 +15,43 @@ let s:special_constants = {
15
15
\ ' v:null' : ' null' ,
16
16
\ ' v:none' : ' null' ,
17
17
\ }
18
+ let s: control_chars = {
19
+ \ ' \' : ' \\' ,
20
+ \ ' "' : ' \"' ,
21
+ \ " \x01 " : ' \u0001' ,
22
+ \ " \x02 " : ' \u0002' ,
23
+ \ " \x03 " : ' \u0003' ,
24
+ \ " \x04 " : ' \u0004' ,
25
+ \ " \x05 " : ' \u0005' ,
26
+ \ " \x06 " : ' \u0006' ,
27
+ \ " \x07 " : ' \u0007' ,
28
+ \ " \x08 " : ' \b' ,
29
+ \ " \x09 " : ' \t' ,
30
+ \ " \x0a " : ' \n' ,
31
+ \ " \x0b " : ' \u000b' ,
32
+ \ " \x0c " : ' \f' ,
33
+ \ " \x0d " : ' \r' ,
34
+ \ " \x0e " : ' \u000e' ,
35
+ \ " \x0f " : ' \u000f' ,
36
+ \ " \x10 " : ' \u0010' ,
37
+ \ " \x11 " : ' \u0011' ,
38
+ \ " \x12 " : ' \u0012' ,
39
+ \ " \x13 " : ' \u0013' ,
40
+ \ " \x14 " : ' \u0014' ,
41
+ \ " \x15 " : ' \u0015' ,
42
+ \ " \x16 " : ' \u0016' ,
43
+ \ " \x17 " : ' \u0017' ,
44
+ \ " \x18 " : ' \u0018' ,
45
+ \ " \x19 " : ' \u0019' ,
46
+ \ " \x1a " : ' \u001a' ,
47
+ \ " \x1b " : ' \u001b' ,
48
+ \ " \x1c " : ' \u001c' ,
49
+ \ " \x1d " : ' \u001d' ,
50
+ \ " \x1e " : ' \u001e' ,
51
+ \ " \x1f " : ' \u001f' ,
52
+ \ }
18
53
lockvar s: float_constants s: float_nan s: float_inf
19
- lockvar s: special_constants
54
+ lockvar s: special_constants s: control_chars
20
55
21
56
function ! s: _true () abort
22
57
return v: true
@@ -75,8 +110,7 @@ function! s:decode(json, ...) abort
75
110
\ ' use_token' : 0 ,
76
111
\ ' allow_nan' : 1 ,
77
112
\} , get (a: 000 , 0 , {}))
78
- let json = iconv (a: json , ' utf-8' , &encoding )
79
- let json = join (split (json, " \n " ), ' ' )
113
+ let json = join (split (a: json , " \n " ), ' ' )
80
114
let json = substitute (json, ' \\u34;' , ' \\"' , ' g' )
81
115
let json = substitute (json, ' \\u\(\x\x\x\x\)' , ' \=s:string.nr2enc_char("0x".submatch(1))' , ' g' )
82
116
if settings.allow_nan
@@ -110,11 +144,8 @@ function! s:encode(val, ...) abort
110
144
if t == 0
111
145
return a: val
112
146
elseif t == 1
113
- let json = ' "' . escape (a: val , ' \"' ) . ' "'
114
- let json = substitute (json, " \r " , ' \\r' , ' g' )
115
- let json = substitute (json, " \n " , ' \\n' , ' g' )
116
- let json = substitute (json, " \t " , ' \\t' , ' g' )
117
- return iconv (json, &encoding , ' utf-8' )
147
+ let s = substitute (a: val , ' [\x01-\x1f\\"]' , ' \=s:control_chars[submatch(0)]' , ' g' )
148
+ return ' "' . s . ' "'
118
149
elseif t == 2
119
150
if s: const .true == a: val
120
151
return ' true'
0 commit comments