forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
116 lines (109 loc) · 3.21 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
package carbon
import (
"encoding/json"
"fmt"
"testing"
)
type Person struct {
ID int64 `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Birthday ToDateTimeString `json:"birthday"`
GraduatedAt ToDateString `json:"graduated_at"`
CreatedAt ToTimeString `json:"created_at"`
UpdatedAt ToTimestamp `json:"updated_at"`
DateTime1 ToTimestampWithSecond `json:"date_time1"`
DateTime2 ToTimestampWithMillisecond `json:"date_time2"`
DateTime3 ToTimestampWithMicrosecond `json:"date_time3"`
DateTime4 ToTimestampWithNanosecond `json:"date_time4"`
}
func TestCarbon_MarshalJSON1(t *testing.T) {
person := Person{
ID: 1,
Name: "gouguoyin",
Age: 18,
Birthday: ToDateTimeString{Now().SubYears(18)},
GraduatedAt: ToDateString{Parse("2020-08-05 13:14:15")},
CreatedAt: ToTimeString{Parse("2021-08-05 13:14:15")},
UpdatedAt: ToTimestamp{Parse("2022-08-05 13:14:15")},
DateTime1: ToTimestampWithSecond{Parse("2023-08-05 13:14:15")},
DateTime2: ToTimestampWithMillisecond{Parse("2024-08-05 13:14:15")},
DateTime3: ToTimestampWithMicrosecond{Parse("2025-08-05 13:14:15")},
DateTime4: ToTimestampWithNanosecond{Parse("2025-08-05 13:14:15")},
}
data, err := json.Marshal(&person)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Person1 output by json:\n%s\n", data)
}
func TestCarbon_MarshalJSON2(t *testing.T) {
person := Person{
ID: 1,
Name: "gouguoyin",
Age: 18,
Birthday: ToDateTimeString{Parse("")},
GraduatedAt: ToDateString{Parse("0")},
CreatedAt: ToTimeString{Parse("0000-00-00")},
UpdatedAt: ToTimestamp{Parse("00:00:00")},
DateTime1: ToTimestampWithSecond{NewCarbon()},
DateTime2: ToTimestampWithMillisecond{Carbon{}},
DateTime3: ToTimestampWithMicrosecond{Parse("")},
DateTime4: ToTimestampWithNanosecond{Parse("")},
}
data, err := json.Marshal(&person)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Person2 output by json:\n%s\n", data)
}
func TestCarbon_UnmarshalJSON1(t *testing.T) {
str := `{
"id":1,
"name":"gouguoyin",
"age":18,
"birthday":"2003-07-16 16:22:02",
"graduated_at":"2020-08-05",
"created_at":"13:14:15",
"updated_at":1659676455,
"date_time1":1691212455,
"date_time2":1722834855000,
"date_time3":1754370855000000,
"date_time4":1754370855000000000
}`
person := new(Person)
err := json.Unmarshal([]byte(str), &person)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Json string parse to person:\n%+v\n", *person)
}
func TestCarbon_UnmarshalJSON2(t *testing.T) {
str := `{
"id":0,
"name":"",
"age":0,
"birthday":"",
"graduated_at":"",
"created_at":"",
"updated_at":0,
"date_time1":0,
"date_time2":0,
"date_time3":0,
"date_time4":0
}`
person := new(Person)
err := json.Unmarshal([]byte(str), &person)
if err != nil {
t.Fatal(err)
}
fmt.Printf("Json string parse to person:\n%+v\n", *person)
}
func TestError_UnmarshalJSON(t *testing.T) {
str := `xxx`
person := new(Person)
err := json.Unmarshal([]byte(str), &person)
if err != nil {
fmt.Println("catch an exception in UnmarshalJSON():", err)
}
}