-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonport.go
454 lines (410 loc) · 9.28 KB
/
jsonport.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package jsonport
import (
"errors"
"fmt"
"io"
"io/ioutil"
)
const (
zero = Number("0")
ParseMemberNamesOnly = "__member_names_only__"
)
// Json represents everything of json
type Json struct {
atoi bool
tob bool
err error
tp Type
m []kv // tp: OBJECT
a []Json // tp: ARRAY
b []byte // tp: NUMBER or STRING
t bool // tp: BOOL
}
type kv struct {
s string
k []byte
v Json
}
func (e *kv) key() string {
if len(e.s) != 0 {
return e.s
}
if len(e.k) == 0 {
return ""
}
e.s = unquote(e.k)
return e.s
}
var (
ErrMoreBytes = errors.New("not all bytes unmarshaled")
)
// Unmarshal parses data to Json.
// When specified keys, jsonport skips unused field for performance
func Unmarshal(data []byte, keys ...interface{}) (Json, error) {
if len(keys) != 0 {
j, _, err := parsePath(data, keys...)
return j, err
}
j, i, err := parse(data)
if err != nil {
return j, err
}
n := skipspace(data[i:])
if n+i != len(data) {
return j, ErrMoreBytes
}
return j, nil
}
// DecodeFrom parses data from reader to json
func DecodeFrom(r io.Reader) (Json, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return Json{}, nil
}
return Unmarshal(b)
}
// Type returns the Type of current json value
func (j Json) Type() Type {
return j.tp
}
func (j Json) IsObject() bool {
return j.tp == OBJECT
}
func (j Json) IsNumber() bool {
return j.tp == NUMBER
}
func (j Json) IsArray() bool {
return j.tp == ARRAY
}
func (j Json) IsString() bool {
return j.tp == STRING
}
func (j Json) IsBool() bool {
return j.tp == BOOL
}
func (j Json) IsNull() bool {
return j.tp == NULL
}
// Error returns error of current context
func (j Json) Error() error {
return j.err
}
// StringAsNumber enables conversion like {"id": "123"},
// j.GetInt("id") is returned 123 instead of an err,
// j.GetString("id") is returned "123" as expected.
func (j *Json) StringAsNumber() {
j.atoi = true
}
// AllAsBool enables conversion of Bool():
// STRING, ARRAY, OBJECT: true if Len() != 0
// NUMBER: true if Float() != 0
// NULL: false
func (j *Json) AllAsBool() {
j.tob = true
}
func (j Json) mismatch(t Type) error {
if j.err != nil {
return j.err
}
return fmt.Errorf("type mismatch: expected %s, found %s", t, j.tp)
}
// String converts current json value to string
func (j Json) String() (string, error) {
if j.tp != STRING {
return "", j.mismatch(STRING)
}
return unquote(j.b), nil
}
func (j Json) number() (Number, error) {
if j.tp == NUMBER || (j.atoi && j.tp == STRING) {
return nn(j.b), nil
}
return zero, j.mismatch(NUMBER)
}
// Float converts current json value to float64
func (j Json) Float() (float64, error) {
n, err := j.number()
if err != nil {
return 0, err
}
return n.Float64()
}
// Int converts current json value to int64
// result will be negative if pasring from uint64 like `1<<63`
func (j Json) Int() (int64, error) {
n, err := j.number()
if err != nil {
return 0, err
}
i, err := n.Int64()
if err != nil {
if f, err2 := n.Float64(); err2 == nil {
if f < 9.223372036e+21 && f > -9.223372036e+21 {
return int64(f), nil
}
}
}
return i, err
}
// Bool converts current json value to bool
func (j Json) Bool() (bool, error) {
if j.tp == BOOL {
return j.t, nil
}
if !j.tob {
return false, j.mismatch(BOOL)
}
switch j.tp {
case STRING, ARRAY, OBJECT:
l, err := j.Len()
return l > 0, err
case NUMBER:
f, err := j.Float()
return f != 0, err
}
return false, nil
}
// Len returns the length of json value.
// STRING: the number of bytes
// ARRAY: the number of elements
// OBJECT: the number of pairs
func (j Json) Len() (int, error) {
switch j.tp {
case STRING:
return len(j.b), nil
case ARRAY:
return len(j.a), nil
case OBJECT:
return len(j.m), nil
}
return 0, errLen
}
// Keys returns the field names of json object.
// error is returned if value type not equal to OBJECT.
func (j Json) Keys() ([]string, error) {
if j.tp != OBJECT {
return nil, j.mismatch(OBJECT)
}
ret := make([]string, 0, len(j.m))
for i := range j.m {
ret = append(ret, j.m[i].key())
}
return ret, nil
}
// Values returns the field values of json object.
// error is returned if value type not equal to OBJECT.
func (j Json) Values() ([]Json, error) {
if j.tp != OBJECT {
return nil, j.mismatch(OBJECT)
}
ret := make([]Json, 0, len(j.m))
for i := range j.m {
ret = append(ret, j.returnj(j.m[i].v))
}
return ret, nil
}
// Member returns the member value specified by `name`
// a NULL type Json is returned if member not found
// Json.Error() is set if type not equal to OBJECT
func (j Json) Member(name string) Json {
if j.tp != OBJECT {
return Json{err: j.mismatch(OBJECT)}
}
for i := range j.m {
if j.m[i].key() == name {
return j.returnj(j.m[i].v)
}
}
v := Json{tp: NULL}
return j.returnj(v)
}
// Element returns the (i+1)th element of array.
// a NULL type Json is returned if index out of range.
// Json.Error() is set if type not equal to ARRAY.
func (j Json) Element(i int) Json {
if j.tp != ARRAY {
return Json{err: j.mismatch(ARRAY)}
}
var v Json
if i < 0 || i >= len(j.a) {
v.tp = NULL
} else {
v = j.a[i]
}
return j.returnj(v)
}
func (j *Json) returnj(v Json) Json {
v.atoi = j.atoi
v.tob = j.tob
return v
}
// Array converts current json value to []Json.
// error is returned if value type not equal to ARRAY.
func (j Json) Array() ([]Json, error) {
if j.tp != ARRAY {
return nil, j.mismatch(ARRAY)
}
return j.a, nil
}
// IntArray converts current json value to []int64.
// error is returned if any element type not equal to NUMBER.
func (j Json) IntArray() ([]int64, error) {
arr, err := j.Array()
if err != nil {
return nil, err
}
ret := make([]int64, len(arr))
for i, e := range arr {
n, err := e.Int()
if err != nil {
return nil, err
}
ret[i] = n
}
return ret, nil
}
// FloatArray converts current json value to []float64.
// error is returned if any element type not equal to NUMBER.
func (j Json) FloatArray() ([]float64, error) {
arr, err := j.Array()
if err != nil {
return nil, err
}
ret := make([]float64, len(arr))
for i, e := range arr {
n, err := e.Float()
if err != nil {
return nil, err
}
ret[i] = n
}
return ret, nil
}
// BoolArray converts current json value to []bool.
// error is returned if any element type not equal to NUMBER.
func (j Json) BoolArray() ([]bool, error) {
arr, err := j.Array()
if err != nil {
return nil, err
}
ret := make([]bool, len(arr))
for i, e := range arr {
ret[i], err = e.Bool()
if err != nil {
return nil, err
}
}
return ret, nil
}
// StringArray converts current json value to []string.
// error is returned if any element type not equal to STRING.
func (j Json) StringArray() ([]string, error) {
arr, err := j.Array()
if err != nil {
return nil, err
}
ret := make([]string, len(arr))
for i, e := range arr {
ret[i], err = e.String()
if err != nil {
return nil, err
}
}
return ret, nil
}
// Get returns Json object by key sequence.
// key with the type of string is equal to j.Member(k),
// key with the type of number is equal to j.Element(k),
// j.Get("key", 1) is equal to j.Member("key").Element(1).
// a NULL type Json returned with err if:
// - key type not supported. (neither number nor string)
// - json value type mismatch.
func (j Json) Get(keys ...interface{}) Json {
for _, k := range keys {
if j.err != nil {
return j
}
switch t := k.(type) {
// reflect.ValueOf(t).Int() or Uint() ?
// without reflection here.
case int:
j = j.Element(t)
case int8:
j = j.Element(int(t))
case int16:
j = j.Element(int(t))
case int32:
j = j.Element(int(t))
case int64:
j = j.Element(int(t))
case uint:
j = j.Element(int(t))
case uint8:
j = j.Element(int(t))
case uint16:
j = j.Element(int(t))
case uint32:
j = j.Element(int(t))
case uint64:
j = j.Element(int(t))
case string:
j = j.Member(t)
default:
return Json{err: errKeyType}
}
}
return j
}
// GetBool convert json value specified by keys to bool,
// it is equal to Get(keys...).Bool()
func (j Json) GetBool(keys ...interface{}) (bool, error) {
v := j.Get(keys...)
return v.Bool()
}
// GetString convert json value specified by keys to string,
// it is equal to Get(keys...).String()
func (j Json) GetString(keys ...interface{}) (string, error) {
v := j.Get(keys...)
return v.String()
}
// GetFloat convert json value specified by keys to float64,
// it is equal to Get(keys...).Float()
func (j Json) GetFloat(keys ...interface{}) (float64, error) {
v := j.Get(keys...)
return v.Float()
}
// GetInt convert json value specified by keys to int64,
// it is equal to Get(keys...).Int()
func (j Json) GetInt(keys ...interface{}) (int64, error) {
v := j.Get(keys...)
return v.Int()
}
// EachOf convert every elements specified by keys in json value to ARRAY.
// Json.Error() is set if an error occurred.
// it is equal to `Json{[e.Get(keys...) for e in j.Array()]}`
func (j Json) EachOf(keys ...interface{}) Json {
var arr []Json
var err error
t := j.Type()
if t == ARRAY {
arr, err = j.Array()
} else if t == OBJECT {
arr, err = j.Values()
} else {
err = fmt.Errorf("type %s not supported EachOf()", j.Type())
}
if err != nil {
return Json{err: err}
}
var ret Json
ret.a = make([]Json, len(arr))
ret.tp = ARRAY
for i, e := range arr {
v := e.Get(keys...)
if v.err != nil {
return Json{err: v.err}
}
ret.a[i] = v
}
return j.returnj(ret)
}