-
Notifications
You must be signed in to change notification settings - Fork 41
/
json.go
162 lines (153 loc) · 3.27 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jsondiff
// findKey finds and return the object value that match key.
// It assumes to be on an opening curly bracket.
// The function expects a compact JSON input.
func findKey(json string, key string) string {
for i := 1; i < len(json); {
ki := i
i = squashString(json, i)
k := json[ki+1 : i-1]
i++ // skip semicolon
vi := i
i = squashValue(json, i)
if key == k {
return json[vi:i]
}
i++ // skip comma
}
return ""
}
// findIndex finds and return the array value at the given index.
// It assumes to be on opening square bracket.
// The function expects a compact JSON input.
func findIndex(json string, idx int) string {
for i, j := 1, 0; i < len(json); {
vi := i
i = squashValue(json, i)
if j == idx {
return json[vi:i]
}
i++ // skip comma
j++ // next elem index
}
return ""
}
func squashValue(json string, i int) int {
switch json[i] {
case 't', 'n': // true, null
i += 4
case 'f': // false
i += 5
case '"': // string
i = squashString(json, i)
case '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // number
i = squashNumber(json, i)
case '[', '{': // array, object
i = squashObjectOrArray(json, i)
}
return i
}
// squashNumber reads b until it encounter a character
// than can follow a properly formatted number.
func squashNumber(json string, i int) int {
i++
for ; i < len(json); i++ {
if json[i] <= ' ' || json[i] == ',' || json[i] == ']' || json[i] == '}' {
break
}
}
return i
}
// squashString reads b until it encounter a non-escaped
// double quote, which indicate the sep of the string.
func squashString(json string, i int) int {
i++ // assume to be on opening quote
for ; i < len(json); i++ {
if json[i] == '"' && json[i-1] != '\\' {
break
}
}
i++ // move to closing quote
return i
}
// note: taken from https://github.com/tidwall/gjson
func squashObjectOrArray(json string, i int) int {
depth := 1
i++
L:
for ; i < len(json); i++ {
if json[i] >= '"' && json[i] <= '}' {
switch json[i] {
case '"':
i++
s2 := i
for ; i < len(json); i++ {
if json[i] > '\\' {
continue
}
if json[i] == '"' {
if json[i-1] == '\\' {
n := 0
for j := i - 2; j > s2-1; j-- {
if json[j] != '\\' {
break
}
n++
}
if n%2 == 0 {
continue
}
}
break
}
}
case '{', '[':
depth++
case '}', ']':
depth--
if depth == 0 {
i++
break L
}
}
}
}
return i
}
// compact removes insignificant space characters from the
// input JSON byte slice and returns the compacted result.
func compact(json []byte) []byte {
b := make([]byte, 0, len(json))
return _compact(json, b)
}
// compactInPlace is similar to compact, but it reuses the input
// JSON buffer to avoid allocations.
func compactInPlace(json []byte) []byte {
return _compact(json, json)
}
func _compact(src, dst []byte) []byte {
dst = dst[:0]
for i := 0; i < len(src); i++ {
if src[i] > ' ' {
dst = append(dst, src[i])
if src[i] == '"' {
i++
for ; i < len(src); i++ {
dst = append(dst, src[i])
if src[i] == '"' {
j := i - 1
for ; ; j-- {
if src[j] != '\\' {
break
}
}
if (j-i)%2 != 0 {
break
}
}
}
}
}
}
return dst
}