-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
251 lines (222 loc) · 5.72 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright 2020 ChronoWave Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Package parser declares an expression parser with support for macro
* expansion.
*/
package codec
import (
"bytes"
"math/bits"
"strconv"
"github.com/buger/jsonparser"
"github.com/chronowave/chronowave/ssd"
"github.com/chronowave/chronowave/ssd/internal"
)
func ParseJson(json []byte) (*ssd.ParsedBlock, error) {
f := &ssd.ParsedBlock{
Meta: ssd.NewEntityMeta(),
Entity: bytes.NewBuffer(make([]byte, 0, len(json))),
Content: ssd.NewTextColumnar(),
}
for {
// offset to json buffer, number of object,
var exec func(*ssd.ParsedBlock, [][]byte, []byte) (int, error)
open:
for _, b := range json {
switch b {
case '{':
exec = parseObject
break open
case '[':
exec = parseArray
break open
}
}
if exec == nil {
break
}
// end of entity, } or ], note: eoe would be at before closing } or ], including white space
eoe, err := exec(f, nil, json)
if err != nil {
return f, err
}
json = json[eoe+1:]
}
return f, nil
}
// offset in json doc, number of objects, error if any
func parseObject(f *ssd.ParsedBlock, path [][]byte, data []byte) (int, error) {
if path == nil {
// root
if f.Count > 0 {
// end of previous object
f.Entity.WriteByte(ssd.SOH)
f.Entity.WriteByte(ssd.EOO)
}
f.Count++
}
// eoo -> end of object, offset in json doc
eoo := 0
handle := func(key []byte, value []byte, valueType jsonparser.ValueType, offset int) error {
eoo = offset
return parseObjectValue(f, valueType, append(path, replaceSlashWithUnderScore(key)), value)
}
err := jsonparser.ObjectEach(data, handle)
if err != nil {
return eoo, err
}
return eoo, nil
}
func parseArray(f *ssd.ParsedBlock, path [][]byte, data []byte) (int, error) {
var objerr error
eoa, err := jsonparser.ArrayEach(data,
func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if dataType == jsonparser.Object {
if _, err := parseObject(f, path, value); err != nil {
objerr = err
}
}
})
if err == nil {
err = objerr
}
return eoa, err
}
func parseArrayValue(f *ssd.ParsedBlock, path [][]byte, data []byte) (int, error) {
// start of array element
pathCode := f.Meta.GenerateCode(path)
if f.Entity.Len() > 0 {
f.Entity.WriteByte(ssd.SOH)
}
f.Entity.Write(pathCode)
f.Entity.WriteByte(ssd.SOA)
sep := false
handle := func(value []byte, valueType jsonparser.ValueType, offset int, err error) {
if sep {
f.Entity.WriteByte(ssd.SOH)
f.Entity.WriteByte(ssd.AED)
}
parseObjectValue(f, valueType, path, value)
sep = true
}
eoa, err := jsonparser.ArrayEach(data, handle)
// end of array
f.Entity.WriteByte(ssd.SOH)
f.Entity.Write(pathCode)
f.Entity.WriteByte(ssd.EOA)
return eoa, err
}
// https://tools.ietf.org/html/rfc7159#section-3
func parseObjectValue(f *ssd.ParsedBlock, valueType jsonparser.ValueType, path [][]byte, value []byte) error {
pathCode := f.Meta.GenerateCode(path)
writePath := func() {
if f.Entity.Len() > 0 {
f.Entity.WriteByte(ssd.SOH)
}
f.Entity.Write(pathCode)
}
var err error
switch valueType {
case jsonparser.String:
// start of string value
writePath()
f.Entity.WriteByte(ssd.TEXT)
if len(value) == 0 {
// empty string
f.Columnar.Text = append(f.Columnar.Text, 0)
} else {
f.Columnar.Text = append(f.Columnar.Text, f.Content.Add(string(fragmentLongText(value, internal.FragmentSize))))
}
case jsonparser.Number:
// start of number value
writePath()
dot, rank := byte('.'), [8]uint{}
for _, b := range value {
rank[bits.OnesCount8(dot^b)]++
}
if rank[0] > 0 {
if v, e := strconv.ParseFloat(string(value), 64); e == nil {
// encode as float64
f.Entity.WriteByte(ssd.FLT64)
f.Columnar.Float64 = append(f.Columnar.Float64, v)
} else {
err = e
}
} else {
if v, e := strconv.ParseInt(string(value), 10, 64); e == nil {
// encode as int64
f.Entity.WriteByte(ssd.INT64)
f.Columnar.Int64 = append(f.Columnar.Int64, v)
} else {
err = e
}
}
case jsonparser.Object:
_, err = parseObject(f, path, value)
case jsonparser.Array:
_, err = parseArrayValue(f, path, value)
case jsonparser.Boolean:
writePath()
f.Entity.WriteByte(ssd.BOOL)
if value[0] == 't' || value[0] == 'T' {
f.Columnar.Bool = append(f.Columnar.Bool, true)
} else {
f.Columnar.Bool = append(f.Columnar.Bool, false)
}
case jsonparser.Null:
writePath()
f.Entity.WriteByte(ssd.NULL)
case jsonparser.Unknown:
err = jsonparser.UnknownValueTypeError
}
return err
}
func replaceSlashWithUnderScore(key []byte) []byte {
for i, b := range key {
if b == '/' || b <= ssd.EOA {
key[i] = '_'
}
}
return key
}
func fragmentLongText(value []byte, fsz int) []byte {
i, fragments := len(value)-1, (len(value)-1)/fsz
if fragments > 0 {
fragmented := make([]byte, len(value)+fragments)
copy(fragmented, value)
value = fragmented
for j := len(value) - 1; fragments > 0; j-- {
b := value[i]
if ssd.IsControlCharacter(b) {
value[j] = ' '
} else {
value[j] = b
}
if i%fsz == 0 {
j--
value[j] = ssd.FRAG
fragments--
}
i--
}
}
for ; i >= 0; i-- {
if ssd.IsControlCharacter(value[i]) {
value[i] = ' '
}
}
return value
}