/
json_util.go
109 lines (89 loc) · 1.68 KB
/
json_util.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
package main
import (
"encoding/json"
"reflect"
"strings"
)
// Equal checks equality between 2 Body-encoded data.
func Equal(vx, vy interface{}) bool {
if reflect.TypeOf(vx) != reflect.TypeOf(vy) {
return false
}
switch x := vx.(type) {
case map[string]interface{}:
y := vy.(map[string]interface{})
if len(x) != len(y) {
return false
}
for k, v := range x {
val2 := y[k]
if (v == nil) != (val2 == nil) {
return false
}
if !Equal(v, val2) {
return false
}
}
return true
case []interface{}:
y := vy.([]interface{})
if len(x) != len(y) {
return false
}
var matches int
flagged := make([]bool, len(y))
for _, v := range x {
for i, v2 := range y {
if Equal(v, v2) && !flagged[i] {
matches++
flagged[i] = true
break
}
}
}
return matches == len(x)
default:
return vx == vy
}
}
func Remove(i interface{}, path string) {
if path == "" {
return
}
var next, current string
index := strings.IndexRune(path, '.')
if index == -1 {
current = path
} else {
current = path[:index]
next = path[index+1:]
}
switch t := i.(type) {
case map[string]interface{}:
for k, v := range t {
if k == current {
// If there is no more nodes to traverse we can remove it and terminate the routine
if next == "" {
delete(t, current)
return
}
Remove(v, next)
}
}
case []interface{}:
if current == "#" {
for _, v := range t {
Remove(v, next)
}
}
}
}
// Unmarshal parses the Body-encoded data into an interface{}.
func Unmarshal(b []byte) (interface{}, error) {
var j interface{}
err := json.Unmarshal(b, &j)
if err != nil {
return nil, err
}
return j, nil
}