forked from likexian/simplejson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplejson_test.go
137 lines (100 loc) · 3.36 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
/*
* Go module for JSON parser
* http://www.likexian.com/
*
* Copyright 2012-2014, Kexian Li
* Released under the Apache License, Version 2.0
*
*/
package simplejson
import (
"fmt"
"testing"
"github.com/bmizerany/assert"
)
type JsonResult struct {
Result Result `json:"result"`
Status Status `json:"status"`
}
type Result struct {
List []int64 `json:"list"`
Online bool `json:"online"`
Rate float64 `json:"rate"`
}
type Status struct {
Code int64 `json:"code"`
Message string `json:"message"`
}
func TestSimplejson(t *testing.T) {
data_result := Result{}
data_result.List = []int64{0, 1, 2, 3, 4}
data_result.Online = true
data_result.Rate = 0.8
data_status := Status{}
data_status.Code = 1
data_status.Message = "success"
data_json_result := JsonResult{}
data_json_result.Result = data_result
data_json_result.Status = data_status
data_json := Json{}
data_json.Data = data_json_result
data, err := Dumps(&data_json)
assert.Equal(t, nil, err)
assert.Equal(t, data, `{"result":{"list":[0,1,2,3,4],"online":true,"rate":0.8},"status":{"code":1,"message":"success"}}`)
json, err := Loads(data)
assert.NotEqual(t, nil, json)
assert.Equal(t, nil, err)
code, _ := json.Get("status").Get("code").Int()
assert.Equal(t, 1, code)
message, _ := json.Get("status").Get("message").String()
assert.Equal(t, "success", message)
exists := json.Has("result")
assert.Equal(t, true, exists)
deepexists := json.Get("result").Has("online")
assert.Equal(t, true, deepexists)
nexists := json.Has("not-exists")
assert.Equal(t, false, nexists)
json.Set("not-exists", "do-exists")
sexists := json.Has("not-exists")
assert.Equal(t, true, sexists)
json.Del("not-exists")
dexists := json.Has("not-exists")
assert.Equal(t, false, dexists)
list, _ := json.Get("result").Get("list").Array()
assert.NotEqual(t, nil, list)
for k, v := range list {
assert.Equal(t, k, int(v.(float64)))
}
online, _ := json.Get("result").Get("online").Bool()
assert.Equal(t, true, online)
rate, _ := json.Get("result").Get("rate").Float64()
assert.Equal(t, 0.80, rate)
result, err := Dumps(json)
assert.Equal(t, nil, err)
assert.Equal(t, data, result)
json.Set("name", "Li Kexian")
json.Set("link", "http://www.likexian.com/")
name, _ := json.Get("name").String()
assert.Equal(t, "Li Kexian", name)
bytes, err := Dump("simplejson.json", json)
assert.NotEqual(t, 0, bytes)
assert.Equal(t, nil, err)
njson, err := Load("simplejson.json")
assert.Equal(t, json, njson)
assert.Equal(t, nil, err)
new_json := New()
new_json.Set("new", true)
new_exists := new_json.Has("new")
assert.Equal(t, true, new_exists)
new_value, _ := new_json.Get("new").Bool()
assert.Equal(t, true, new_value)
new_json.Set("int", 100)
int_value, _ := new_json.Get("int").Int()
assert.Equal(t, "int", fmt.Sprintf("%T", int_value))
uint_value, _ := new_json.Get("int").Uint()
assert.Equal(t, "uint", fmt.Sprintf("%T", uint_value))
int64_value, _ := new_json.Get("int").Int64()
assert.Equal(t, "int64", fmt.Sprintf("%T", int64_value))
uint64_value, _ := new_json.Get("int").Uint64()
assert.Equal(t, "uint64", fmt.Sprintf("%T", uint64_value))
}