-
Notifications
You must be signed in to change notification settings - Fork 5
/
jsonutils.go
174 lines (148 loc) · 3.86 KB
/
jsonutils.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
package json
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
)
// JSONUtils a json utils object
type JSONUtils struct {
message []byte
maxLevel int
obj interface{}
skipSimpleArray bool
}
// KvPair a kv pair
type KvPair struct {
Key string
Value string
}
// New create a new json utils object and parse json to object
func New(message []byte, maxLevel int, skipSimpleArray bool) (*JSONUtils, error) {
var obj interface{}
if err := json.Unmarshal(message, &obj); err != nil {
return nil, err
}
return &JSONUtils{
message: message,
obj: obj,
maxLevel: maxLevel,
skipSimpleArray: skipSimpleArray,
}, nil
}
// ToKvPairsArray convert to an array with all kv pair
func (ju *JSONUtils) ToKvPairsArray() []KvPair {
return ju.createKvPairs(ju.obj, 1)
}
// ToKvPairs convert to a map with kv
func (ju *JSONUtils) ToKvPairs() map[string]string {
kvPairs := make(map[string]string)
for _, kv := range ju.ToKvPairsArray() {
kvPairs[kv.Key] = kv.Value
}
return kvPairs
}
func (ju *JSONUtils) createKvPairs(obj interface{}, level int) []KvPair {
kvPairs := make([]KvPair, 0)
objValue := reflect.ValueOf(obj)
if !objValue.IsValid() {
return kvPairs
}
objType := objValue.Type()
switch objType.Kind() {
case reflect.Map:
for _, key := range objValue.MapKeys() {
keyStr := fmt.Sprintf("%s", key)
value := objValue.MapIndex(key).Interface()
subValues := ju.recursiveSubValue(keyStr, value, level+1)
if len(subValues) == 0 {
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: "(null)",
})
}
kvPairs = append(kvPairs, subValues...)
}
case reflect.Slice, reflect.Array:
for i := 0; i < objValue.Len(); i++ {
keyStr := fmt.Sprintf("[%d]", i)
value := objValue.Index(i).Interface()
subValues := ju.recursiveSubValue(keyStr, value, level+1)
if len(subValues) == 0 {
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: "(null)",
})
}
kvPairs = append(kvPairs, subValues...)
}
default:
}
return kvPairs
}
func (ju *JSONUtils) recursiveSubValue(keyStr string, value interface{}, level int) []KvPair {
kvPairs := make([]KvPair, 0)
reflectValue := reflect.ValueOf(value)
if !reflectValue.IsValid() {
return kvPairs
}
valueType := reflectValue.Type().Kind()
switch valueType {
case reflect.Slice, reflect.Map, reflect.Array:
if ju.maxLevel > 0 && level > ju.maxLevel {
valueJSON, _ := json.Marshal(value)
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: string(valueJSON),
})
break
}
// skip simple array
if reflectValue.Len() > 0 && ju.skipSimpleArray && valueType != reflect.Map {
subValue := reflect.ValueOf(reflectValue.Index(0).Interface())
subValueKind := subValue.Kind()
if subValueKind != reflect.Map && subValueKind != reflect.Slice && subValueKind != reflect.Array {
valueJSON, _ := json.Marshal(value)
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: string(valueJSON),
})
break
}
}
walkSub := ju.createKvPairs(value, level)
// if sub value is empty, set value to [] or {}
if len(walkSub) == 0 {
emptyValue := "[]"
if valueType == reflect.Map {
emptyValue = "{}"
}
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: emptyValue,
})
}
for _, kv := range walkSub {
kvPairs = append(kvPairs, KvPair{
Key: keyStr + "." + kv.Key,
Value: kv.Value,
})
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: fmt.Sprintf("%d", value),
})
case reflect.Float32, reflect.Float64:
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: strconv.FormatFloat(value.(float64), 'f', -1, 64),
})
default:
kvPairs = append(kvPairs, KvPair{
Key: keyStr,
Value: fmt.Sprintf("%s", value),
})
}
return kvPairs
}