/
json_kv.go
334 lines (283 loc) · 7.23 KB
/
json_kv.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
// Copyright 2019, Keychain Foundation Ltd.
// This file is part of the dipperin-core library.
//
// The dipperin-core library is free software: you can redistribute
// it and/or modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// The dipperin-core library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package json_kv
import (
"github.com/dipperin/dipperin-core/common/util"
"github.com/tidwall/gjson"
"strconv"
"reflect"
"errors"
"fmt"
"strings"
)
const (
objSegC = '.'
arrSegC = '^'
objSegS = string(objSegC)
arrSegS = string(arrSegC)
invalidKeyChars = objSegS + arrSegS
)
func Obj2KV(obj interface{}) (map[string]string, error) {
j := gjson.ParseBytes(util.StringifyJsonToBytes(obj))
return Json2KV(j)
}
func JsonBytes2KV(b []byte) (map[string]string, error) {
j := gjson.ParseBytes(b)
return Json2KV(j)
}
func JsonStr2KV(str string) (map[string]string, error) {
j := gjson.Parse(str)
return Json2KV(j)
}
func Json2KV(json gjson.Result) (map[string]string, error) {
result := map[string]string{}
err := json2KV("", json, result)
return result, err
}
// Determine if the key is an illegal key
func invalidJsonKey(key string) bool {
return strings.ContainsAny(key, invalidKeyChars)
}
// todo All keys in obj can't take two special characters, or you can escape special characters. Do more border checks
func json2KV(key string, json gjson.Result, result map[string]string) (err error) {
if json.IsObject() {
json.ForEach(func(key1, value gjson.Result) bool {
if key == "" {
err = json2KV(key1.Str, value, result)
} else {
err = json2KV(key + objSegS + key1.Str, value, result)
}
return true
})
} else if json.IsArray() {
index := 0
json.ForEach(func(key1, value gjson.Result) bool {
if key == "" {
err = json2KV(strconv.Itoa(index), value, result)
} else {
err = json2KV(key + arrSegS + strconv.Itoa(index), value, result)
}
index++
return true
})
} else {
if json.Type != gjson.String {
result[key] = json.String()
} else {
//result[key] = `"` + json.String() + `"`
// Handling strings that need to be escaped
result[key] = util.StringifyJson(json.String())
//fmt.Println("===", key, result[key])
}
}
return nil
}
const (
dataTypeNormal = iota
dataTypeObj
dataTypeArr
)
func KV2JsonObj(kv map[string]string, result interface{}) error {
str, err := KV2JsonStr(kv)
if err != nil {
return err
}
return util.ParseJson(str, result)
}
func KV2JsonStr(kv map[string]string) (result string, err error) {
// The children in root are parsed in kv
root := &jNode{ nType: dataTypeObj, children: []absNode{} }
if err = parseKVToJNode(root, kv); err != nil {
return
}
// Parsing jNode and finally converting it into json string
result, err = extractObj(root)
return
}
type jNode struct {
key string
nType int
value string
children []absNode
}
func (n *jNode) GetChild(cKey string) absNode {
for _, c := range n.children {
if c.Key() == cKey {
return c
}
}
return nil
}
func (n *jNode) AddChild(c absNode) {
n.children = append(n.children, c)
}
func (n *jNode) SetValue(v string) {
n.value = v
}
func (n *jNode) Key() string {
return n.key
}
func (n *jNode) Value() string {
return n.value
}
func (n *jNode) NType() int {
return n.nType
}
func (n *jNode) Children() []absNode {
return n.children
}
type nodeInfoCache struct {
n absNode
kv map[string]string
}
func parseKVToJNode(parent absNode, kv map[string]string) error {
childrenInfoCaches := map[string]*nodeInfoCache{}
for k, v := range kv {
k1, extKey, dt := extractKey(k)
// add child to parent
oldC := parent.GetChild(k1)
rv := reflect.ValueOf(oldC)
if !rv.IsValid() || rv.IsNil() {
oldC = &jNode{ key: k1, nType: dt, children: []absNode{} }
parent.AddChild(oldC)
}
if oldC.NType() != dt {
return errors.New("data type not match for key: " + k1)
}
// extract extKey and value
switch dt {
case dataTypeNormal:
oldC.SetValue(v)
default:
cache := childrenInfoCaches[k1]
if cache == nil {
cache = &nodeInfoCache{ n: oldC, kv: map[string]string{} }
childrenInfoCaches[k1] = cache
}
cache.kv[extKey] = v
}
}
for _, cache := range childrenInfoCaches {
if err := parseKVToJNode(cache.n, cache.kv); err != nil {
return err
}
}
return nil
}
func extractKey(k string) (x, extKey string, dataType int) {
kLen := len(k)
for i := 0; i < kLen; i++ {
tmpC := k[i]
if tmpC == objSegC {
return k[:i], k[i+1:], dataTypeObj
}
if tmpC == arrSegC {
return k[:i], k[i+1:], dataTypeArr
}
}
return k, "", dataTypeNormal
}
type absNode interface {
Key() string
Value() string
NType() int
Children() []absNode
GetChild(cKey string) absNode
AddChild(c absNode)
SetValue(v string)
}
func extractObj(n absNode) (string, error) {
if n.NType() != dataTypeObj {
return "", errors.New(fmt.Sprintf("extractArr expect obj data, bug got: %v", n.NType()))
}
result := "{"
for _, v := range n.Children() {
result += `"` + v.Key() + `":`
switch v.NType() {
case dataTypeObj:
if tmpObjStr, err := extractObj(v); err != nil {
return "", err
} else {
result += tmpObjStr
}
case dataTypeArr:
if tmpArrStr, err := extractArr(v); err != nil {
return "", err
} else {
result += tmpArrStr
}
case dataTypeNormal:
result += v.Value()
}
result += ","
}
// remove the last comma
resultLen := len(result)
if result[resultLen - 1] == ',' {
result = result[:resultLen - 1]
}
result += "}"
return result, nil
}
func extractArr(n absNode) (string, error) {
if n.NType() != dataTypeArr {
return "", errors.New(fmt.Sprintf("extractArr expect arr data, bug got: %v", n.NType()))
}
children := n.Children()
cLen := len(children)
dataArr := make([]string, cLen)
for _, v := range children {
index, err := strconv.Atoi(v.Key())
if index < 0 || err != nil {
return "", errors.New(fmt.Sprintf("invalid arr index: %v, err: %v", v.Key(), err))
}
if index >= cLen {
return "", errors.New(fmt.Sprintf("data index can't bigger than arr len, len: %v index: %v", cLen, v.Key()))
}
// Map structure cannot have the same key
//if dataArr[index] != "" {
// return "", errors.New(fmt.Sprintf("duplicate value at index: %v", v.Key()))
//}
switch v.NType() {
case dataTypeObj:
if tmpObjStr, err := extractObj(v); err != nil {
return "", err
} else {
dataArr[index] = tmpObjStr
}
case dataTypeArr:
if tmpArrStr, err := extractArr(v); err != nil {
return "", err
} else {
dataArr[index] = tmpArrStr
}
case dataTypeNormal:
dataArr[index] = v.Value()
}
}
// finishing results
result := "["
for i := 0; i < cLen; i++ {
result += dataArr[i] + ","
}
// remove the last comma
resultLen := len(result)
if result[resultLen - 1] == ',' {
result = result[:resultLen - 1]
}
result += "]"
return result, nil
}