-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplejson_test.go
276 lines (253 loc) · 6.44 KB
/
simplejson_test.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
package simplejson
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"log"
"testing"
)
var jsonStr = map[string]string{
"simpleObject": `{"name": "test1"}`,
"simpleArray": `["test2.01", "test2.02"]`,
"simpleArrayObject": `[{"name": "test3.小明"},{"name": "test3.小花"}]`,
"userProfileAddressMobile": `
{
"id": 10,
"name": "test4",
"age": 18,
"address": {
"country": "中国",
"city": "成都"
},
"mobile": [
"15400012301",
"15400012302",
15400012303
]
}
`,
"simpleArrayArray": `[["test5.01"], ["test5.02", "test5.03"]]`,
"userProfile": `
{
"id": 10,
"name": "test4",
"age": 18,
"address": {
"country": "中国",
"city": "成都"
},
"mobile": [
"15400012301",
"15400012302",
15400012303
],
"books": [
{
"bookNo": "book1",
"bookName": "c程序设计",
"authors": [
"小明",
"哈哈"
]
},
{
"bookNo": "book2",
"bookName": "go程序设计",
"authors": [
"小红",
"小黑"
]
}
]
}
`,
}
// TestNewJson 测试创建json
func TestNewJson(t *testing.T) {
body := jsonStr["simpleObject"]
m := map[string]interface{}{
"name": "test1",
}
// 创建json对象
j, err := NewJson([]byte(body))
assert.Equal(t, err, nil)
assert.Equal(t, j, &Json{m})
}
// TestNew 测试New函数
func TestNew(t *testing.T) {
j := New()
assert.Equal(t, j, &Json{map[string]interface{}{}})
}
// TestJson_Get 测试获取值json对象值
func TestJson_Get(t *testing.T) {
body := jsonStr["userProfileAddressMobile"]
j, err := NewJson([]byte(body))
v, err := j.Get("name").String()
assert.Equal(t, err, nil)
assert.Equal(t, v, "test4")
v1, err := j.Get("address").Get("city").String()
assert.Equal(t, err, nil)
assert.Equal(t, v1, "成都")
}
// 测试获取多级键的值
func TestJson_Get2(t *testing.T) {
body := jsonStr["userProfileAddressMobile"]
j, err := NewJson([]byte(body))
v, err := j.Get("address").Get("city").String()
assert.Equal(t, err, nil)
assert.Equal(t, v, "成都")
}
// TestJson_GetArrayIndex 测试获取json数组值
func TestJson_GetArrayIndex(t *testing.T) {
body := jsonStr["simpleArray"]
j, err := NewJson([]byte(body))
v, err := j.GetArrayIndex(0).String()
assert.Equal(t, err, nil)
assert.Equal(t, err, nil)
assert.Equal(t, v, "test2.01")
v1, err := j.GetArrayIndex(1).String()
assert.Equal(t, err, nil)
assert.Equal(t, v1, "test2.02")
}
// TestJson_GetArrayIndex2 测试获取多层数组值
func TestJson_GetArrayIndex2(t *testing.T) {
body := jsonStr["simpleArrayArray"]
j, err := NewJson([]byte(body))
v, err := j.GetArrayIndex(1).GetArrayIndex(0).String()
assert.Equal(t, err, nil)
assert.Equal(t, v, "test5.02")
}
// 测试简单的json对象操作
func TestJson_MustSetSimpleObject(t *testing.T) {
body := jsonStr["simpleObject"]
j, err := NewJson([]byte(body))
j.MustSet("test1.change value", "name")
v, err := j.Get("name").String()
assert.Equal(t, v, "test1.change value")
assert.Equal(t, err, nil)
}
// 测试简单MustSet数组操作
func TestJson_MustSetSimpleArray(t *testing.T) {
body := jsonStr["simpleArray"]
j, err := NewJson([]byte(body))
j.MustSet("test2.02.change value", 1)
v, err := j.GetArrayIndex(1).String()
assert.Equal(t, v, "test2.02.change value")
assert.Equal(t, err, nil)
}
// 测试设置多级json对象数组值
func TestJson_MustSetKeys(t *testing.T) {
body := jsonStr["userProfile"]
j, err := NewJson([]byte(body))
log.Println("aaaaaa", j.data)
j.MustSet("小明set.object.array.object.array", "books", 0, "authors", 0)
j.MustSet("小明setNew.object.array.object.array", "books", 0, "authors", -1)
j.MustSet("c程序设计.set.object.array.object", "books", 0, "bookName")
v, err := j.Get("books").GetArrayIndex(0).Get("authors").GetArrayIndex(0).String()
assert.Equal(t, v, "小明set.object.array.object.array")
assert.Equal(t, err, nil)
v1, err := j.Get("books").GetArrayIndex(0).Get("authors").GetArrayIndex(2).String()
assert.Equal(t, v1, "小明setNew.object.array.object.array")
assert.Equal(t, err, nil)
v2, err := j.Get("books").GetArrayIndex(0).Get("bookName").String()
assert.Equal(t, v2, "c程序设计.set.object.array.object")
assert.Equal(t, err, nil)
}
// 测试删除json对象
func TestJson_DelSimpleObject(t *testing.T) {
body := jsonStr["simpleObject"]
j, err := NewJson([]byte(body))
// 删除map键
j.Del("name")
log.Println(j.data)
assert.Equal(t, err, nil)
assert.Equal(t, j.data, make(map[string]interface{}))
}
// 测试删除json数组索引
func TestJson_DelSimpleArray(t *testing.T) {
body := jsonStr["simpleArray"]
j, err := NewJson([]byte(body))
// 删除数组第2个元素
j.Del(1)
log.Println(j.data)
assert.Equal(t, j.data, []interface{}{"test2.01"})
// 删除数据第一个元素
j.Del(0)
assert.Equal(t, j.data, []interface{}{})
assert.Equal(t, err, nil)
}
// 测试删除json字段值
func TestJson_Del(t *testing.T) {
body := jsonStr["userProfile"]
j, err := NewJson([]byte(body))
j.Del("address", "city").Del("books", 0, "authors", 1)
assert.Equal(t, j.data, map[string]interface{}{
"id": json.Number("10"),
"name": "test4",
"age": json.Number("18"),
"address": map[string]interface{}{
"country": "中国",
},
"mobile": []interface{}{
"15400012301",
"15400012302",
json.Number("15400012303"),
},
"books": []interface{}{
map[string]interface{}{
"bookNo": "book1",
"bookName": "c程序设计",
"authors": []interface{}{
"小明",
},
},
map[string]interface{}{
"bookNo": "book2",
"bookName": "go程序设计",
"authors": []interface{}{
"小红",
"小黑",
},
},
},
},
)
assert.Equal(t, err, nil)
}
// 删除json字段, 支持多级键和连贯操作
func TestJson_MustDel(t *testing.T) {
body := jsonStr["userProfile"]
j, err := NewJson([]byte(body))
j.MustDel("address", "city").MustDel("books", 0, "authors", 1)
assert.Equal(t, j.data, map[string]interface{}{
"id": json.Number("10"),
"name": "test4",
"age": json.Number("18"),
"address": map[string]interface{}{
"country": "中国",
},
"mobile": []interface{}{
"15400012301",
"15400012302",
json.Number("15400012303"),
},
"books": []interface{}{
map[string]interface{}{
"bookNo": "book1",
"bookName": "c程序设计",
"authors": []interface{}{
"小明",
},
},
map[string]interface{}{
"bookNo": "book2",
"bookName": "go程序设计",
"authors": []interface{}{
"小红",
"小黑",
},
},
},
},
)
assert.Equal(t, err, nil)
}