-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.go
296 lines (258 loc) · 6.71 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
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
package lytics
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
// Given a set of bytes, convert to json map[string]interface{}, then
// convert
func FlattenJson(jsonBytes []byte) (map[string][]string, error) {
jsonMap := make(map[string]interface{})
if err := json.Unmarshal(jsonBytes, &jsonMap); err != nil {
return nil, err
}
return FlattenJsonMap(jsonMap)
}
// Attempt to flatten A map of map[string]interface{} into url Values
func FlattenJsonMapIntoQs(uv url.Values, jsonMap map[string]interface{}, pre string) error {
for k, v := range jsonMap {
if err := flattenJsonValue(uv, v, pre+k); err != nil {
return err
}
}
return nil
}
// The map m should be a map that resulted from json.Unmarshal(), or the behavior is
// undefined (that's bad).
func FlattenJsonMap(m map[string]interface{}) (map[string][]string, error) {
outMap := make(map[string][]string, len(m))
if err := flattenJson("", outMap, m); err != nil {
return nil, err
}
return outMap, nil
}
func flattenJson(prefix string, into map[string][]string, toFlatten interface{}) error {
typ := reflect.TypeOf(toFlatten)
kind := typ.Kind()
rVal := reflect.ValueOf(toFlatten)
switch {
case isScalar(kind):
into[prefix] = []string{scalarToString(&rVal)}
return nil
case kind == reflect.Slice:
sl := toFlatten.([]interface{})
containsOnlyScalars := true
for _, elem := range sl {
if !isScalar(reflect.ValueOf(elem).Type().Kind()) {
containsOnlyScalars = false
}
}
if containsOnlyScalars {
into[prefix] = scalarSliceToStrings(&rVal)
} else {
for i := 0; i < len(sl); i++ {
recursePrefix := strcat(prefix, "[", strconv.Itoa(i), "]")
err := flattenJson(recursePrefix, into, sl[i])
if err != nil {
return err
}
}
}
case kind == reflect.Map:
for _, mapKey := range rVal.MapKeys() {
mapVal := rVal.MapIndex(mapKey)
var recursePrefix string
if prefix == "" {
recursePrefix = mapKey.String()
} else {
recursePrefix = strcat(prefix, ".", mapKey.String())
}
if mapVal.IsNil() {
into[recursePrefix] = []string{}
} else {
if err := flattenJson(recursePrefix, into, mapVal.Interface()); err != nil {
return err
}
}
}
default:
panic(fmt.Sprintf("Unexpected type %s kind %s", rVal.Type(), kind))
}
return nil
}
// The input must be a reflect.Value that is a slice whose elements are scalars.
// Only works for scalar types that would be produced by JSON decoding into a
// map[string]interface{}.
func scalarSliceToStrings(rVal *reflect.Value) []string {
sliceLen := rVal.Len()
strs := make([]string, sliceLen)
for i := 0; i < sliceLen; i++ {
sliceElem := rVal.Index(i).Elem()
strs[i] = scalarToString(&sliceElem)
}
return strs
}
// Only works for scalar types that would be produced by JSON decoding into a
// map[string]interface{}.
func scalarToString(rVal *reflect.Value) string {
switch rVal.Type().Kind() {
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(rVal.Float(), 'f', -1, 64)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%d", rVal.Interface())
case reflect.String:
return rVal.String()
case reflect.Bool:
if rVal.Bool() {
return "true"
} else {
return "false"
}
}
panic(fmt.Sprintf("Unexpected scalar type %s", rVal.Type()))
}
// Only works for types that would be produced by JSON decoding into a
// map[string]interface{}.
func isScalar(k reflect.Kind) bool {
switch k {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Int,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Bool,
reflect.String, reflect.Float32, reflect.Float64:
return true
}
return false
}
func strcat(strs ...string) string {
return strings.Join(strs, "")
}
func flattenJsonValue(into url.Values, toFlatten interface{}, key string) error {
if toFlatten == nil {
// ??
// into.Set(key,"")
return nil
}
typ := reflect.TypeOf(toFlatten)
kind := typ.Kind()
rVal := reflect.ValueOf(toFlatten)
if isScalar(kind) {
into[key] = []string{scalarToString(&rVal)}
return nil
}
switch val := toFlatten.(type) {
// case []interface{}:
// sva := make([]string, 0)
// for _, av := range x {
// if err := flattenJsonValue(n, av, key); err != nil {
// return nil
// }
// }
// if len(sva) > 0 {
// uv[key] = sva
// }
case map[string]interface{}:
if len(val) > 0 {
if err := FlattenJsonMapIntoQs(into, val, key+"."); err != nil {
return err
}
}
case bool:
if val == true {
into.Set(key, "t")
} else {
into.Set(key, "f")
}
default:
// what types don't we support?
// []interface{}
}
return nil
}
// Specialized Embeddable time that formats to Lytics Standard Api format
// which is string unix milliseconds since eopch
//
// http://stackoverflow.com/questions/20475321/override-the-layout-used-by-json-marshal-to-format-time-time
// https://github.com/lytics/lio/wiki/api
type JsonTime struct {
time.Time
}
func NewJsonTime(t time.Time) JsonTime {
return JsonTime{t}
}
func (t *JsonTime) UnmarshalJSON(by []byte) error {
var tsval string
err := json.Unmarshal(by, &tsval)
if err == nil {
ts, err := strconv.ParseInt(tsval, 10, 64)
if err == nil {
t.Time = time.Unix(0, ts*1e6)
}
}
return err
}
func (t JsonTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatInt(t.Time.UnixNano()/1e6, 10) + `"`), nil
}
// Timestamp in secs
type JsonUnixTime struct {
time.Time
}
func NewJsonUnixTime(t time.Time) JsonUnixTime {
return JsonUnixTime{t}
}
func (t *JsonUnixTime) UnmarshalJSON(by []byte) error {
var tsint int64
err := json.Unmarshal(by, &tsint)
if err == nil {
t.Time = time.Unix(tsint, 0)
return nil
} else {
var tsval string
err := json.Unmarshal(by, &tsval)
if err == nil {
ts, err := strconv.ParseInt(tsval, 10, 64)
if err == nil {
t.Time = time.Unix(ts, 0)
return nil
}
}
}
return err
}
func (t JsonUnixTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatInt(t.Time.UnixNano()/1e6, 10) + `"`), nil
}
// Timestamp type handles json timestamp values
type Timestamp struct {
time.Time
}
func (t *Timestamp) MarshalJSON() ([]byte, error) {
return []byte(t.String()), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
var tsint int64
err := json.Unmarshal(b, &tsint)
if err != nil {
var tsval string
if err = json.Unmarshal(b, &tsval); err != nil {
return err
}
tsint, err = strconv.ParseInt(tsval, 10, 64)
if err != nil {
return err
}
}
t.Time = time.Unix(tsint, 0)
return nil
}
func (t *Timestamp) String() string {
if t.IsZero() {
return ""
}
ts := t.Unix()
return fmt.Sprint(ts)
}