-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjson_test.go
123 lines (101 loc) · 3.5 KB
/
json_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
package jsontime
import (
"testing"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)
func setup() (jsoniter.API, *CustomTimeExtension) {
json := jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
}.Froze()
timeExtension := NewCustomTimeExtension()
json.RegisterExtension(timeExtension)
return json, timeExtension
}
func TestTimeFormat(t *testing.T) {
json, timeExtension := setup()
type Book struct {
Id int `json:"id"`
PublishedAt *time.Time `json:"published_at"`
UpdatedAt *time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}
timeZone, err := time.LoadLocation("Asia/Shanghai")
assert.Nil(t, err)
timeExtension.SetDefaultTimeFormat(time.RFC3339, timeZone)
t2018 := time.Date(2018, 1, 1, 0, 0, 0, 0, timeZone)
book1 := Book{
Id: 1,
UpdatedAt: &t2018,
CreatedAt: t2018,
}
bytes, err := json.Marshal(book1)
assert.Nil(t, err)
assert.Equal(t, `{"id":1,"published_at":null,"updated_at":"2018-01-01T00:00:00+08:00","created_at":"2018-01-01T00:00:00+08:00"}`, string(bytes))
book2 := Book{}
err = json.Unmarshal(bytes, &book2)
assert.Nil(t, err)
assert.Equal(t, book1, book2)
}
func TestLocale(t *testing.T) {
json, timeExtension := setup()
timeZoneShanghai, _ := time.LoadLocation("Asia/Shanghai")
timeExtension.AddLocaleAlias("shanghai", timeZoneShanghai)
type Book struct {
Id int `json:"id"`
PublishedAt time.Time `json:"published_at" time_location:"UTC"`
UpdatedAt *time.Time `json:"updated_at" time_location:"shanghai"`
CreatedAt time.Time `json:"created_at" time_location:"shanghai"`
}
book := Book{
Id: 0,
PublishedAt: time.Date(2018, 1, 1, 0, 0, 0, 0, timeZoneShanghai),
UpdatedAt: nil,
CreatedAt: time.Date(2018, 1, 1, 0, 0, 0, 0, timeZoneShanghai),
}
bytes, err := json.Marshal(book)
assert.Nil(t, err)
assert.Equal(t, `{"id":0,"published_at":"2017-12-31T16:00:00Z","updated_at":null,"created_at":"2018-01-01T00:00:00+08:00"}`, string(bytes))
}
func TestUnMarshalZero(t *testing.T) {
json, _ := setup()
type Book struct {
Id int `json:"id"`
UpdatedAt *time.Time `json:"updated_at" time_location:"UTC"`
CreatedAt time.Time `json:"created_at" time_location:"UTC"`
}
book := Book{}
jsonBytes := []byte(`{"id":0,"updated_at":null,"created_at":"0000-00-00 00:00:00"}`)
err := json.Unmarshal(jsonBytes, &book)
assert.NotNil(t, err)
}
func TestAlias(t *testing.T) {
json, timeExtension := setup()
type Book struct {
Id int `json:"id"`
PublishedAt *time.Time `json:"published_at" time_format:"sql_datetime"`
UpdatedAt *time.Time `json:"updated_at" time_format:"sql_datetime"`
CreatedAt time.Time `json:"created_at" time_format:"sql_datetime"`
}
timeZoneShanghai, err := time.LoadLocation("Asia/Shanghai")
assert.Nil(t, err)
timeExtension.AddTimeFormatAlias("sql_datetime", "2006-01-02 15:04:05")
timeExtension.AddLocaleAlias("shanghai", timeZoneShanghai)
timeExtension.SetDefaultTimeFormat(time.RFC3339, timeZoneShanghai)
t2018 := time.Date(2018, 1, 1, 0, 0, 0, 0, timeZoneShanghai)
book1 := Book{
Id: 1,
UpdatedAt: &t2018,
CreatedAt: t2018,
}
bytes, err := json.Marshal(book1)
assert.Nil(t, err)
assert.Equal(t, `{"id":1,"published_at":null,"updated_at":"2018-01-01 00:00:00","created_at":"2018-01-01 00:00:00"}`, string(bytes))
book2 := Book{}
err = json.Unmarshal(bytes, &book2)
assert.Nil(t, err)
assert.Equal(t, book1, book2)
}