-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
141 lines (129 loc) · 3.16 KB
/
parse.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
package json_diff
import (
"bytes"
"encoding/json"
"regexp"
"strings"
)
var keyReplaceRegexp = regexp.MustCompile(`~0*1`)
// key 中的
// "/" 会被替换成 "~1"
// "~1" 会被替换成 "~01"
// "~01" 会被替换为 "~001"
// "~001" 会被替换为 "~0001"
// 依此类推
func keyReplace(key string) string {
resList := keyReplaceRegexp.FindAllStringIndex(key, -1)
buff := bytes.NewBufferString("")
pre := 0
for _, res := range resList {
buff.WriteString(key[pre:res[0]])
buff.WriteRune('~')
for i := 1; i < res[1]-res[0]; i++ {
buff.WriteRune('0')
}
buff.WriteRune('1')
pre = res[1]
}
buff.WriteString(key[pre:])
return strings.ReplaceAll(buff.String(), "/", "~1")
}
func keyRestore(key string) string {
key = strings.ReplaceAll(key, "~1", "/")
resList := keyReplaceRegexp.FindAllStringIndex(key, -1)
buff := bytes.NewBufferString("")
pre := 0
for _, res := range resList {
buff.WriteString(key[pre:res[0]])
buff.WriteRune('~')
for i := 3; i < res[1]-res[0]; i++ {
buff.WriteRune('0')
}
buff.WriteRune('1')
pre = res[1]
}
buff.WriteString(key[pre:])
return buff.String()
}
func parse(v interface{}, level int64) *JsonNode {
var root *JsonNode
switch v.(type) {
case map[string]interface{}:
value := v.(map[string]interface{})
root = &JsonNode{Type: JsonNodeTypeObject, Level: level, ChildrenMap: make(map[string]*JsonNode)}
for key, va := range value {
key = keyReplace(key)
n := parse(va, level+1)
n.Key = key
// n.Father = root
root.ChildrenMap[key] = n
// root.Children = append(root.Children, n)
}
case []interface{}:
root = &JsonNode{Type: JsonNodeTypeSlice, Level: level}
value := v.([]interface{})
for _, va := range value {
n := parse(va, level+1)
// n.Father = root
root.Children = append(root.Children, n)
}
default:
root = &JsonNode{Type: JsonNodeTypeValue, Level: level}
root.Type = JsonNodeTypeValue
root.Value = v
}
return root
}
func Parse(input []byte) *JsonNode {
var v interface{}
if err := json.Unmarshal(input, &v); err != nil {
panic(err)
}
return parse(v, 0)
}
func marshalValue(root *JsonNode) interface{} {
return root.Value
}
func marshalObject(root *JsonNode) map[string]interface{} {
dict := make(map[string]interface{})
for k, v := range root.ChildrenMap {
switch v.Type {
case JsonNodeTypeObject:
dict[k] = marshalObject(v)
case JsonNodeTypeSlice:
dict[k] = marshalSlice(v)
case JsonNodeTypeValue:
dict[k] = marshalValue(v)
}
}
return dict
}
func marshalSlice(root *JsonNode) []interface{} {
res := make([]interface{}, len(root.Children))
for i, child := range root.Children {
switch child.Type {
case JsonNodeTypeValue:
res[i] = marshalValue(child)
case JsonNodeTypeSlice:
res[i] = marshalSlice(child)
case JsonNodeTypeObject:
res[i] = marshalObject(child)
}
}
return res
}
func Unmarshal(input []byte) *JsonNode {
return Parse(input)
}
func Marshal(root *JsonNode) ([]byte, error) {
var dict interface{}
switch root.Type {
case JsonNodeTypeObject:
dict = marshalObject(root)
case JsonNodeTypeSlice:
dict = marshalSlice(root)
case JsonNodeTypeValue:
dict = marshalValue(root)
}
return json.Marshal(dict)
}