forked from bitly/go-simplejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplejson.go
460 lines (393 loc) · 9.71 KB
/
simplejson.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
455
456
457
458
459
460
package simplejson
import (
"encoding/json"
"log"
)
// returns the current implementation version
func Version() string {
return "0.5.0-alpha"
}
type JSON struct {
data interface{}
}
// NewJson returns a pointer to a new `JSON` object
// after unmarshaling `body` bytes
func NewJSON(body []byte) (*JSON, error) {
j := new(JSON)
err := j.UnmarshalJSON(body)
if err != nil {
return nil, err
}
return j, nil
}
// New returns a pointer to a new, empty `JSON` object
func New() *JSON {
return &JSON{
data: make(map[string]interface{}),
}
}
// Interface returns the underlying data
func (j *JSON) Interface() interface{} {
return j.data
}
// Encode returns its marshaled data as `[]byte`
func (j *JSON) Encode() ([]byte, error) {
return j.MarshalJSON()
}
// EncodePretty returns its marshaled data as `[]byte` with indentation
func (j *JSON) EncodePretty() ([]byte, error) {
return json.MarshalIndent(&j.data, "", " ")
}
// Implements the json.Marshaler interface.
func (j *JSON) MarshalJSON() ([]byte, error) {
return json.Marshal(&j.data)
}
// Set modifies `JSON` map by `key` and `value`
// Useful for changing single key/value in a `JSON` object easily.
func (j *JSON) Set(key string, val interface{}) {
m, ok := j.CheckMap()
if !ok {
return
}
m[key] = val
}
// SetPath modifies `JSON`, recursively checking/creating map keys for the supplied path,
// and then finally writing in the value
func (j *JSON) SetPath(branch []string, val interface{}) {
if len(branch) == 0 {
j.data = val
return
}
// in order to insert our branch, we need map[string]interface{}
if _, ok := (j.data).(map[string]interface{}); !ok {
// have to replace with something suitable
j.data = make(map[string]interface{})
}
curr := j.data.(map[string]interface{})
for i := 0; i < len(branch)-1; i++ {
b := branch[i]
// key exists?
if _, ok := curr[b]; !ok {
n := make(map[string]interface{})
curr[b] = n
curr = n
continue
}
// make sure the value is the right sort of thing
if _, ok := curr[b].(map[string]interface{}); !ok {
// have to replace with something suitable
n := make(map[string]interface{})
curr[b] = n
}
curr = curr[b].(map[string]interface{})
}
// add remaining k/v
curr[branch[len(branch)-1]] = val
}
// Del modifies `JSON` map by deleting `key` if it is present.
func (j *JSON) Del(key string) {
m, ok := j.CheckMap()
if !ok {
return
}
delete(m, key)
}
// getKey returns a pointer to a new `JSON` object
// for `key` in its `map` representation
// and a bool identifying success or failure
func (j *JSON) getKey(key string) (*JSON, bool) {
m, ok := j.CheckMap()
if ok {
if val, ok := m[key]; ok {
return &JSON{val}, true
}
}
return nil, false
}
// getIndex returns a pointer to a new `JSON` object
// for `index` in its `array` representation
// and a bool identifying success or failure
func (j *JSON) getIndex(index int) (*JSON, bool) {
a, ok := j.CheckArray()
if ok {
if len(a) > index {
return &JSON{a[index]}, true
}
}
return nil, false
}
// Get searches for the item as specified by the branch
// within a nested JSON and returns a new JSON pointer
// the pointer is always a valid JSON, allowing for chained operations
//
// newJs := js.Get("top_level", "entries", 3, "dict")
func (j *JSON) Get(branch ...interface{}) *JSON {
jin, ok := j.CheckGet(branch...)
if ok {
return jin
}
return &JSON{nil}
}
// CheckGet is like Get, except it also returns a bool
// indicating whenever the branch was found or not
// the JSON pointer mai be nil
//
// newJs, ok := js.Get("top_level", "entries", 3, "dict")
func (j *JSON) CheckGet(branch ...interface{}) (*JSON, bool) {
jin := j
var ok bool
for _, p := range branch {
switch p.(type) {
case string:
jin, ok = jin.getKey(p.(string))
case int:
jin, ok = jin.getIndex(p.(int))
default:
ok = false
}
if !ok {
return nil, false
}
}
return jin, true
}
// CheckJSONMap returns a copy of a JSON map, but with values as Jsons
func (j *JSON) CheckJSONMap() (map[string]*JSON, bool) {
m, ok := j.CheckMap()
if !ok {
return nil, false
}
jm := make(map[string]*JSON)
for key, val := range m {
jm[key] = &JSON{val}
}
return jm, true
}
// CheckJSONArray returns a copy of an array, but with each value as a JSON
func (j *JSON) CheckJSONArray() ([]*JSON, bool) {
a, ok := j.CheckArray()
if !ok {
return nil, false
}
ja := make([]*JSON, len(a))
for key, val := range a {
ja[key] = &JSON{val}
}
return ja, true
}
// CheckMap type asserts to `map`
func (j *JSON) CheckMap() (map[string]interface{}, bool) {
if m, ok := (j.data).(map[string]interface{}); ok {
return m, true
}
return nil, false
}
// CheckArray type asserts to an `array`
func (j *JSON) CheckArray() ([]interface{}, bool) {
if a, ok := (j.data).([]interface{}); ok {
return a, true
}
return nil, false
}
// CheckBool type asserts to `bool`
func (j *JSON) CheckBool() (bool, bool) {
if s, ok := (j.data).(bool); ok {
return s, true
}
return false, false
}
// CheckString type asserts to `string`
func (j *JSON) CheckString() (string, bool) {
if s, ok := (j.data).(string); ok {
return s, true
}
return "", false
}
// JSONArray guarantees the return of a `[]*JSON` (with optional default)
func (j *JSON) JSONArray(args ...[]*JSON) []*JSON {
var def []*JSON
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("JSONArray() received too many arguments %d", len(args))
}
a, ok := j.CheckJSONArray()
if ok {
return a
}
return def
}
// JSONMap guarantees the return of a `map[string]*JSON` (with optional default)
func (j *JSON) JSONMap(args ...map[string]*JSON) map[string]*JSON {
var def map[string]*JSON
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("JSONMap() received too many arguments %d", len(args))
}
a, ok := j.CheckJSONMap()
if ok {
return a
}
return def
}
// Array guarantees the return of a `[]interface{}` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
// for i, v := range js.Get("results").Array() {
// fmt.Println(i, v)
// }
func (j *JSON) Array(args ...[]interface{}) []interface{} {
var def []interface{}
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Array() received too many arguments %d", len(args))
}
a, ok := j.CheckArray()
if ok {
return a
}
return def
}
// Map guarantees the return of a `map[string]interface{}` (with optional default)
//
// useful when you want to interate over map values in a succinct manner:
// for k, v := range js.Get("dictionary").Map() {
// fmt.Println(k, v)
// }
func (j *JSON) Map(args ...map[string]interface{}) map[string]interface{} {
var def map[string]interface{}
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Map() received too many arguments %d", len(args))
}
a, ok := j.CheckMap()
if ok {
return a
}
return def
}
// String guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
// myFunc(js.Get("param1").String(), js.Get("optional_param").String("my_default"))
func (j *JSON) String(args ...string) string {
var def string
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("String() received too many arguments %d", len(args))
}
s, ok := j.CheckString()
if ok {
return s
}
return def
}
// Int guarantees the return of an `int` (with optional default)
//
// useful when you explicitly want an `int` in a single value return context:
// myFunc(js.Get("param1").Int(), js.Get("optional_param").Int(5150))
func (j *JSON) Int(args ...int) int {
var def int
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Int() received too many arguments %d", len(args))
}
i, ok := j.CheckInt()
if ok {
return i
}
return def
}
// Float64 guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
// myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(5.150))
func (j *JSON) Float64(args ...float64) float64 {
var def float64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Float64() received too many arguments %d", len(args))
}
f, ok := j.CheckFloat64()
if ok {
return f
}
return def
}
// Bool guarantees the return of a `bool` (with optional default)
//
// useful when you explicitly want a `bool` in a single value return context:
// myFunc(js.Get("param1").Bool(), js.Get("optional_param").Bool(true))
func (j *JSON) Bool(args ...bool) bool {
var def bool
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Bool() received too many arguments %d", len(args))
}
b, ok := j.CheckBool()
if ok {
return b
}
return def
}
// Int64 guarantees the return of an `int64` (with optional default)
//
// useful when you explicitly want an `int64` in a single value return context:
// myFunc(js.Get("param1").Int64(), js.Get("optional_param").Int64(5150))
func (j *JSON) Int64(args ...int64) int64 {
var def int64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Int64() received too many arguments %d", len(args))
}
i, ok := j.CheckInt64()
if ok {
return i
}
return def
}
// UInt64 guarantees the return of an `uint64` (with optional default)
//
// useful when you explicitly want an `uint64` in a single value return context:
// myFunc(js.Get("param1").Uint64(), js.Get("optional_param").Uint64(5150))
func (j *JSON) Uint64(args ...uint64) uint64 {
var def uint64
switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("Uint64() received too many arguments %d", len(args))
}
i, ok := j.CheckUint64()
if ok {
return i
}
return def
}