-
Notifications
You must be signed in to change notification settings - Fork 0
/
jzon.go
376 lines (301 loc) · 8.95 KB
/
jzon.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
// Package jzon implements parsing and encoding of JSON (Javascript Object Notation)
// defined in ECMA-404. The most different feature between jzon and encoding/json is
// that jzon defines a explicit structure to notate a JSON object and supplies some
// utility methods to manipulate JSON objects. Package jzon is NOT compatible with
// encoding/json completely. The main design goal of jzon is enhanced validators.
package jzon
import (
"errors"
"fmt"
)
// ValueType is the alias of int
type ValueType = int
// Any is the alias of interface{}
type Any = interface{}
// Jzon defines a JSON node
type Jzon struct {
Type ValueType
data Any
}
// Types
const (
JzTypeStr ValueType = iota
JzTypeInt
JzTypeFlt
JzTypeBol
JzTypeObj
JzTypeArr
JzTypeNul
)
var typeStrings = []string{
"JzTypeStr",
"JzTypeFlt",
"JzTypeInt",
"JzTypeBol",
"JzTypeObj",
"JzTypeArr",
"JzTypeNul",
}
// New allocates an empty Jzon node on the heap
func New(t ValueType) *Jzon {
v := Jzon{}
v.Type = t
switch t {
case JzTypeStr:
case JzTypeInt:
case JzTypeFlt:
case JzTypeBol:
case JzTypeObj:
v.data = make(map[string]*Jzon)
case JzTypeArr:
v.data = make([]*Jzon, 0)
case JzTypeNul:
}
return &v
}
// Parse parses string to Jzon, any errors occurred in the parsing will be thrown out
func Parse(json []byte) (jz *Jzon, err error) {
// in the implements of function `parse()` we don't handle any
// exceptions about slice bounds out of range. here we simply
// throw the error recovered from those unhandled exceptions
defer func() {
e := recover()
if e != nil {
err = fmt.Errorf("maybe out of bound: %v", e)
}
}()
pos.col = 0
pos.row = 0
jz, rem, err := parse(json)
if len(rem) == 0 {
return jz, err
}
rem = trimWhiteSpaces(rem)
if len(rem) == 0 {
return jz, err
}
return nil, expectString("end of file", rem)
}
// Object returns object value, if it's not an object, an error will be thrown out
func (jz *Jzon) Object() (m map[string]*Jzon, err error) {
if jz.Type != JzTypeObj {
return m, expectTypeOf(JzTypeObj, jz.Type)
}
return jz.data.(map[string]*Jzon), nil
}
// Array returns array value, if it's not an array, an error will be thrown out
func (jz *Jzon) Array() (a []*Jzon, err error) {
if jz.Type != JzTypeArr {
return a, expectTypeOf(JzTypeArr, jz.Type)
}
return jz.data.([]*Jzon), nil
}
// String returns string value, if it's not a string, an error will be thrown out
func (jz *Jzon) String() (s string, err error) {
if jz.Type != JzTypeStr {
return s, expectTypeOf(JzTypeStr, jz.Type)
}
return jz.data.(string), nil
}
// Integer returns integer value, if it's not an integer, an error will be thrown out
func (jz *Jzon) Integer() (n int64, err error) {
if jz.Type != JzTypeInt {
return n, expectTypeOf(JzTypeInt, jz.Type)
}
return jz.data.(int64), nil
}
// Float returns float64 value, if it's not a float, an error will be thrown out
func (jz *Jzon) Float() (f float64, err error) {
if jz.Type != JzTypeFlt {
return f, expectTypeOf(JzTypeInt, jz.Type)
}
return jz.data.(float64), nil
}
// Null returns nil value, if it's not a boolean, an error will be thrown out
func (jz *Jzon) Null() (n Any, err error) {
if jz.Type != JzTypeNul {
return nil, expectTypeOf(JzTypeNul, jz.Type)
}
return nil, nil
}
// Bool returns bool value, if it's not a null, an error will be thrown out
func (jz *Jzon) Bool() (b bool, err error) {
if jz.Type != JzTypeBol {
return b, expectTypeOf(JzTypeBol, jz.Type)
}
return jz.data.(bool), nil
}
// Length returns the length of an object or an array, if it is an object,
// just returns the number of keys, otherwise an error will be thrown out
func (jz *Jzon) Length() (l int, err error) {
if jz.Type == JzTypeArr {
return len(jz.data.([]*Jzon)), nil
}
if jz.Type == JzTypeObj {
return len(jz.data.(map[string]*Jzon)), nil
}
return -1, errors.New("expect node of type JzTypeArr or JzTypeObj" +
", but the real type is " + typeStrings[jz.Type])
}
// ValueOf finds the value of the key in an object, if it's not an object
// or the key does not exist in this object, an error will be thrown out
func (jz *Jzon) ValueOf(k string) (v *Jzon, err error) {
if jz.Type != JzTypeObj {
return v, expectTypeOf(JzTypeObj, jz.Type)
}
v, ok := jz.data.(map[string]*Jzon)[k]
if !ok {
err = errors.New("key doesn't exist")
return
}
return v, nil
}
// ValueAt finds the value at the index in an array, if it's not an
// array or the index is out of bound, an error will be thrown out
func (jz *Jzon) ValueAt(i int) (v *Jzon, err error) {
if jz.Type != JzTypeArr {
return v, expectTypeOf(JzTypeArr, jz.Type)
}
if i < 0 || i >= len(jz.data.([]*Jzon)) {
err = errors.New("index is out of bound")
return
}
return jz.data.([]*Jzon)[i], nil
}
// Keys returns all keys as an string slice in object,
// if it's not an object, an error will be thrown out
func (jz *Jzon) Keys() (ks []string, err error) {
if jz.Type != JzTypeObj {
return ks, expectTypeOf(JzTypeObj, jz.Type)
}
for k := range jz.data.(map[string]*Jzon) {
ks = append(ks, k)
}
return ks, nil
}
// Has returns if this object has the given key, if
// it's not an object, an error will be thrown out
func (jz *Jzon) Has(k string) (has bool, err error) {
if jz.Type != JzTypeObj {
return has, expectTypeOf(JzTypeArr, jz.Type)
}
_, ok := jz.data.(map[string]*Jzon)[k]
return ok, nil
}
// IsNull returns whether it equals to null
func (jz *Jzon) IsNull() bool {
return jz.Type == JzTypeNul
}
// Insert inserts a key with a node in an object, or replaces the value for the key
// when the key already exists. if it's not an object, an error will be thrown out
func (jz *Jzon) Insert(k string, v *Jzon) (err error) {
if jz.Type != JzTypeObj {
return expectTypeOf(JzTypeObj, jz.Type)
}
jz.data.(map[string]*Jzon)[k] = v
return nil
}
// Append appends a node after an array, if it's not an array, an error will be thrown out
func (jz *Jzon) Append(v *Jzon) (err error) {
if jz.Type != JzTypeArr {
return expectTypeOf(JzTypeArr, jz.Type)
}
jz.data = append(jz.data.([]*Jzon), v)
return nil
}
// Delete removes a key in an object, it's safe to delete a key which
// doesn't exist, if it's not an object, an error will be thrown out
func (jz *Jzon) Delete(k string) (err error) {
if jz.Type != JzTypeObj {
return expectTypeOf(JzTypeObj, jz.Type)
}
delete(jz.data.(map[string]*Jzon), k)
return nil
}
// Remove removes an index in an array, it's safe to delete an index
// doesn't exist, if it's not an array, an error will be thrown out
func (jz *Jzon) Remove(i int) (err error) {
if jz.Type != JzTypeArr {
return expectTypeOf(JzTypeArr, jz.Type)
}
if i > len(jz.data.([]*Jzon)) || i < 0 {
return errors.New("index is out of bounds")
}
newArr := jz.data.([]*Jzon)[0:i]
for _, v := range jz.data.([]*Jzon)[i:] {
newArr = append(newArr, v)
}
jz.data = newArr
return nil
}
// AMap is just map for array, if it's not an array, an error will be thrown out
func (jz *Jzon) AMap(itFunc func(g *Jzon) Any) (res []Any, err error) {
if jz.Type != JzTypeArr {
return res, expectTypeOf(JzTypeArr, jz.Type)
}
res = make([]Any, 0)
for _, node := range jz.data.([]*Jzon) {
res = append(res, itFunc(node))
}
return res, nil
}
// AFilter is just filter for array, if it's not an array, an error will be thrown out
func (jz *Jzon) AFilter(predictFunc func(g *Jzon) bool) (res []*Jzon, err error) {
if jz.Type != JzTypeArr {
return res, expectTypeOf(JzTypeArr, jz.Type)
}
res = make([]*Jzon, 0)
for _, node := range jz.data.([]*Jzon) {
if predictFunc(node) {
res = append(res, node)
}
}
return res, nil
}
// AReduce is just reduce for array, if it's not an object, an error will be thrown out
func (jz *Jzon) AReduce(init Any, acc func(a *Jzon, b Any) Any) (res Any, err error) {
if jz.Type != JzTypeArr {
return res, expectTypeOf(JzTypeArr, jz.Type)
}
res = init
for _, node := range jz.data.([]*Jzon) {
res = acc(node, res)
}
return res, nil
}
// OMap is just map for object, if it's not an object, an error will be thrown out
func (jz *Jzon) OMap(itFunc func(key string, g *Jzon) Any) (res []Any, err error) {
if jz.Type != JzTypeObj {
return res, expectTypeOf(JzTypeObj, jz.Type)
}
res = make([]Any, 0)
for k, v := range jz.data.(map[string]*Jzon) {
res = append(res, itFunc(k, v))
}
return res, nil
}
// OFilter is just filter for object, if it's not an object, an error will be thrown out
func (jz *Jzon) OFilter(predictFunc func(key string, value *Jzon) bool) (res *Jzon, err error) {
if jz.Type != JzTypeObj {
return res, expectTypeOf(JzTypeObj, jz.Type)
}
var tmp = *jz
for k, v := range tmp.data.(map[string]*Jzon) {
if !predictFunc(k, v) {
tmp.Delete(k)
}
}
res = &tmp
return res, nil
}
// Map is flat map which retrieves on each children node of itself
func (jz *Jzon) Map(mapFunc func(string, *Jzon) Any) (res Any, err error) {
switch jz.Type {
case JzTypeArr:
return jz.AMap(func(j *Jzon) (res Any) { return mapFunc("", j) })
case JzTypeObj:
return jz.OMap(mapFunc)
default:
return mapFunc("", jz), err
}
}