-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplex_test.go
More file actions
60 lines (57 loc) · 1.54 KB
/
examplex_test.go
File metadata and controls
60 lines (57 loc) · 1.54 KB
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
package xjson
import (
"log"
"testing"
"time"
)
func ExampleEmptyArrayAndEmptyObject () {
response := struct {
Books []string `json:"books"`
Map map[string]string `json:"map"`
}{}
data, err := Marshal(response) ; if err != nil {panic(err)}
log.Print(string(data)) // {"books":null, map:{}}
}
func TestExampleEmptyArrayAndEmptyObject(t *testing.T) {
emptyObjectAndArrayNotNull = true
defer func() {
emptyObjectAndArrayNotNull = false
}()
ExampleEmptyArrayAndEmptyObject()
}
func ExampleIntFloatAutoConvertString () {
request := struct {
Page int `json:"page"`
Price float64 `json:"price"`
}{}
err := Unmarshal([]byte(`{"page":"1", "price": "1.05"}`), &request) ; if err != nil {panic(err)}
log.Printf("%+v", request) // {Page:1 Price:1.05}
}
func TestExampleIntFloatAutoConvertString(t *testing.T) {
autoNumberConvertString = true
defer func() {
autoNumberConvertString = false
}()
ExampleIntFloatAutoConvertString()
}
func ExampleChinaTime() {
{
request := struct {
SendTime ChinaTime `json:"sendTime"`
}{}
err := Unmarshal([]byte(`{"sendTime": "2020-12-10 15:39:25"}`), &request) ; if err != nil {panic(err)}
log.Print(request.SendTime.String()) // 2020-12-10 15:39:25 +0800 CST
}
{
response := struct {
CreateTime ChinaTime `json:"createTime"`
}{
CreateTime: NewChinaTime(time.Date(2000,1,1,0,0,0,0, time.UTC)),
}
data, err := Marshal(response) ; if err != nil {panic(err)}
log.Print(string(data)) // {"createTime":"2000-01-01 08:00:00"}
}
}
func TestExampleChinaTime(t *testing.T) {
ExampleChinaTime()
}