-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
167 lines (145 loc) · 2.51 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
package lazyjson
import (
"encoding/json"
)
type JSON struct {
a []interface{}
m map[string]interface{}
v interface{}
}
var empty = JSON{}
func (nj JSON) K(key string) JSON {
if nj.empty() {
return empty
}
return newJSON(nj.m[key])
}
func (nj JSON) I(index int) JSON {
if nj.empty() || index < 0 || index >= len(nj.a) {
return empty
}
return newJSON(nj.a[index])
}
func (nj JSON) Int(defaultV int) int {
if v, ok := i2int(nj.v); ok {
return v
}
if v, ok := i2float(nj.v); ok {
return int(v)
}
return defaultV
}
func (nj JSON) Float(defaultV float64) float64 {
if v, ok := i2float(nj.v); ok {
return v
}
return defaultV
}
func (nj JSON) String(defaultV string) string {
if v, ok := i2string(nj.v); ok {
return v
}
return defaultV
}
func (nj JSON) Bool(defaultV bool) bool {
if v, ok := i2bool(nj.v); ok {
return v
}
return defaultV
}
func (nj JSON) MarshalJSON() ([]byte, error) {
if nj.m != nil {
return json.Marshal(nj.m)
} else if nj.a != nil {
return json.Marshal(nj.a)
} else {
return json.Marshal(nj.v)
}
}
func (nj *JSON) UnmarshalJSON(data []byte) error {
var i interface{}
if err := json.Unmarshal(data, &i); err != nil {
return err
}
switch v := i.(type) {
case map[string]interface{}:
nj.m = v
case []interface{}:
nj.a = v
default:
nj.v = i
}
return nil
}
func (nj JSON) Size() int {
if nj.empty() {
return 0
}
if nj.m != nil {
return len(nj.m)
}
if nj.a != nil {
return len(nj.a)
}
return 1
}
func (nj JSON) empty() bool {
return nj.a == nil && nj.m == nil && nj.v == nil
}
func newJSON(i interface{}) JSON {
if i == nil {
return empty
}
switch v := i.(type) {
case map[string]interface{}:
return JSON{m: v}
case []interface{}:
return JSON{a: v}
default:
return JSON{v: i}
}
}
func i2int(i interface{}) (int, bool) {
switch v := i.(type) {
case int:
return v, true
case int8:
return int(v), true
case int32:
return int(v), true
case int64:
return int(v), true
case uint:
return int(v), true
case uint8:
return int(v), true
case uint32:
return int(v), true
case uint64:
return int(v), true
}
return 0, false
}
func i2bool(i interface{}) (bool, bool) {
switch v := i.(type) {
case bool:
return v, true
}
return false, false
}
func i2float(i interface{}) (float64, bool) {
switch v := i.(type) {
case float32:
return float64(v), true
case float64:
return v, true
}
return 0, false
}
func i2string(i interface{}) (string, bool) {
switch v := i.(type) {
case string:
return v, true
}
return "", false
}