-
Notifications
You must be signed in to change notification settings - Fork 0
/
unflatten.go
172 lines (146 loc) · 3.28 KB
/
unflatten.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
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"github.com/valyala/bytebufferpool"
)
var (
bComma = []byte{','}
bQuote = []byte{'"'}
bQuoteColon = []byte{'"', ':'}
bArrOpen = []byte{'['}
bArrClose = []byte{']'}
bObjOpen = []byte{'{'}
bObjClose = []byte{'}'}
breaks = [256]bool{'\t': true, '.': true, '[': true, ']': true}
)
func unflatten(in io.Reader, out io.Writer, stream bool) {
var (
err error
// stack of object keys or array indices
keys []*bytebufferpool.ByteBuffer
// same stack but denotes if array or object
isarr []bool
line []byte
// ix of char in line, of end of current field, of current field
i, j, field int
onarr bool
prev byte
)
scan := bufio.NewScanner(in)
for scan.Scan() {
line = scan.Bytes()
field = -1
onarr = false
if !bytes.HasPrefix(line, []byte("json")) {
fail(fmt.Errorf("bad line %q", line))
}
CHARSCAN:
for i = 4; i < len(line); i++ { // skip "json" prefix
switch line[i] {
case '.':
field++
onarr = false
case ']':
case '[':
field++
onarr = line[i+1] != '"'
case '\t':
// compute if things should be closed
closeFields(out, isarr[field+1:])
for _, key := range keys[field+1:] {
bytebufferpool.Put(key)
}
keys = keys[:field+1]
isarr = isarr[:field+1]
_, err = out.Write(line[i+1:])
fail(err)
break CHARSCAN
default: // a-z, 0-9 etc
if line[i] == '"' { // json["x x"]
prev = 0
for j = i + 1; line[j] != '"' && prev != '\\'; j++ {
prev = line[j]
}
j++
} else {
// read field name
for j = i; !breaks[line[j]]; j++ {
}
}
switch {
case field < 0:
fail(fmt.Errorf("bad line %q", line))
case field >= len(keys): // go deeper
openField(out, onarr)
if !onarr {
objKey(out, line[i:j])
}
key := bytebufferpool.Get()
_, err = key.Write(line[i:j])
fail(err)
keys = append(keys, key)
isarr = append(isarr, onarr)
case onarr != isarr[field]:
fail(fmt.Errorf("type switch between array and object: %s", line))
case !bytes.Equal(keys[field].B, line[i:j]): // new field name
closeFields(out, isarr[field+1:])
for _, key := range keys[field:] {
bytebufferpool.Put(key)
}
keys = keys[:field]
isarr = isarr[:field+1]
_, err = out.Write(bComma)
fail(err)
if !onarr {
objKey(out, line[i:j])
}
key := bytebufferpool.Get()
_, err = key.Write(line[i:j])
fail(err)
keys = append(keys, key)
// isarr stays the same, no change to types
default: // same key
}
i = j - 1
}
}
}
closeFields(out, isarr)
fail(scan.Err())
}
func closeFields(out io.Writer, isarr []bool) {
var err error
for i := len(isarr) - 1; i >= 0; i-- {
if isarr[i] {
_, err = out.Write(bArrClose)
} else {
_, err = out.Write(bObjClose)
}
fail(err)
}
}
func openField(out io.Writer, onarr bool) {
var err error
if onarr {
_, err = out.Write(bArrOpen)
fail(err)
} else {
_, err = out.Write(bObjOpen)
fail(err)
}
}
func objKey(out io.Writer, key []byte) {
var err error
_, err = out.Write(bQuote)
fail(err)
if key[0] == '"' {
key = key[1 : len(key)-1]
}
_, err = out.Write(key)
fail(err)
_, err = out.Write(bQuoteColon)
fail(err)
}