-
Notifications
You must be signed in to change notification settings - Fork 17
/
ujson.go
247 lines (211 loc) · 5.44 KB
/
ujson.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
package ujson
import (
"errors"
"log"
)
type JSON struct {
root interface{}
}
func NewFromBytes(data []byte) (*JSON, error) {
if len(data) < 2 { // Need at least "{}"
return nil, errors.New("no data passed in")
}
j := &JSON{}
dec := NewDecoder(simpleStore{}, data)
root, err := dec.Decode()
if err != nil {
return nil, err
}
j.root = root
return j, nil
}
func (j *JSON) Interface() interface{} {
return j.root
}
// Get returns a pointer to a new `Json` object
// for `key` in its `map` representation
//
// useful for chaining operations (to traverse a nested JSON):
// js.Get("top_level").Get("dict").Get("value").Int()
func (j *JSON) Get(key string) *JSON {
m, err := j.MaybeMap()
if err == nil {
if val, ok := m[key]; ok {
return &JSON{val}
}
}
return &JSON{nil}
}
// 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, err := j.MaybeMap()
if err == nil {
return a
}
return def
}
// MaybeMap type asserts to `map`
func (j *JSON) MaybeMap() (map[string]interface{}, error) {
if j == nil {
return nil, errors.New("Cannot MaybeMap on a nil pointer")
}
if m, ok := (j.root).(map[string]interface{}); ok {
return m, nil
}
return nil, errors.New("type assertion to map[string]interface{} failed")
}
// 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, err := j.MaybeString()
if err == nil {
return s
}
return def
}
// MaybeString type asserts to `string`
func (j *JSON) MaybeString() (string, error) {
if s, ok := (j.root).(string); ok {
return s, nil
}
return "", errors.New("type assertion to string failed")
}
// 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, err := j.MaybeInt64()
if err == nil {
return i
}
return def
}
// MaybeInt64 type asserts and parses an `int64`
func (j *JSON) MaybeInt64() (int64, error) {
if n, ok := (j.root).(numeric); ok {
return n.Int64()
}
if n, ok := (j.root).(int); ok {
return int64(n), nil
}
return -1, errors.New("type assertion to numeric failed")
}
// Float64 guarantees the return of an `float64` (with optional default)
//
// useful when you explicitly want an `float64` in a single value return context:
// myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(51.15))
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))
}
i, err := j.MaybeFloat64()
if err == nil {
return i
}
return def
}
// MaybeFloat64 type asserts and parses an `float64`
func (j *JSON) MaybeFloat64() (float64, error) {
if n, ok := (j.root).(numeric); ok {
return n.Float64()
}
if n, ok := (j.root).(float64); ok {
return n, nil
}
return -1, errors.New("type assertion to numeric failed")
}
// Bool guarantees the return of an `bool` (with optional default)
//
// useful when you explicitly want an `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, err := j.MaybeBool()
if err == nil {
return b
}
return def
}
// MaybeBool type asserts and parses an `bool`
func (j *JSON) MaybeBool() (bool, error) {
if b, ok := (j.root).(bool); ok {
return b, nil
}
return false, errors.New("type assertion to bool failed")
}
// Array guarantees the return of an `[]*JSON` (with optional default)
//
// useful when you explicitly want an `bool` in a single value return context:
// myFunc(js.Get("param1").Array(), js.Get("optional_param").Array([]interface{}{"string", 1, 1.1, false}))
func (j *JSON) Array(args ...[]interface{}) []*JSON {
var def []*JSON
switch len(args) {
case 0:
case 1:
for _, val := range args[0] {
def = append(def, &JSON{val})
}
default:
log.Panicf("Array() received too many arguments %d", len(args))
}
a, err := j.MaybeArray()
if err == nil {
return a
}
return def
}
// MaybeArray type asserts to `*[]interface{}`
func (j *JSON) MaybeArray() ([]*JSON, error) {
var ret []*JSON
if a, ok := (j.root).(*[]interface{}); ok {
for _, val := range *a {
ret = append(ret, &JSON{val})
}
return ret, nil
}
return nil, errors.New("type assertion to *[]interface{} failed")
}