forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
95 lines (87 loc) · 2.84 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
package carbon
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 DateTime `json:"birthday1"`
Birthday2 DateTimeMilli `json:"birthday2"`
Birthday3 DateTimeMicro `json:"birthday3"`
Birthday4 DateTimeNano `json:"birthday4"`
GraduatedAt1 Date `json:"graduated_at1"`
GraduatedAt2 DateMilli `json:"graduated_at2"`
GraduatedAt3 DateMicro `json:"graduated_at3"`
GraduatedAt4 DateNano `json:"graduated_at4"`
CreatedAt1 Timestamp `json:"created_at1"`
CreatedAt2 TimestampMilli `json:"created_at2"`
CreatedAt3 TimestampMicro `json:"created_at3"`
CreatedAt4 TimestampNano `json:"created_at4"`
}
var person Person
func TestCarbon_MarshalJSON(t *testing.T) {
person = Person{
Name: "gouguoyin",
Age: 18,
Birthday1: DateTime{Now().SubYears(18)},
Birthday2: DateTimeMilli{Now().SubYears(18)},
Birthday3: DateTimeMicro{Now().SubYears(18)},
Birthday4: DateTimeNano{Now().SubYears(18)},
GraduatedAt1: Date{Parse("2020-08-05 13:14:15")},
GraduatedAt2: DateMilli{Parse("2020-08-05 13:14:15.999")},
GraduatedAt3: DateMicro{Parse("2020-08-05 13:14:15.999999")},
GraduatedAt4: DateNano{Parse("2020-08-05 13:14:15.999999999")},
CreatedAt1: Timestamp{Parse("2023-08-05 13:14:15")},
CreatedAt2: TimestampMilli{Parse("2024-08-05 13:14:15.999")},
CreatedAt3: TimestampMicro{Parse("2025-08-05 13:14:15.999999")},
CreatedAt4: TimestampNano{Parse("2025-08-05 13:14:15.999999999")},
}
data, err := json.Marshal(&person)
assert.Nil(t, err)
fmt.Printf("Person output by json:\n%s\n", data)
}
func TestCarbon_UnmarshalJSON(t *testing.T) {
str := `{
"name": "gouguoyin",
"age": 18,
"birthday1": "2003-07-16 16:22:02",
"birthday2": "2003-07-16 16:22:02.999",
"birthday3": "2003-07-16 16:22:02.999999",
"birthday4": "2003-07-16 16:22:02.999999999",
"graduated_at1": "2020-08-05",
"graduated_at2": "2020-08-05.999",
"graduated_at3": "2020-08-05.999999",
"graduated_at4": "2020-08-05.999999999",
"created_at1": 1596604455,
"created_at2": 1596604455999,
"created_at3": 1596604455999999,
"created_at4": 1596604455999999999
}`
err := json.Unmarshal([]byte(str), &person)
assert.Nil(t, err)
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
func TestError_Json(t *testing.T) {
str := `{
"name": "",
"age": 0,
"birthday1": "",
"birthday2": "",
"birthday3": "",
"birthday4": "",
"graduated_at1": "xxx",
"graduated_at2": "xxx",
"graduated_at3": "xxx",
"graduated_at4": "xxx",
"created_at1": 0,
"created_at2": 0,
"created_at3": 0,
"created_at4": 0
}`
err := json.Unmarshal([]byte(str), &person)
assert.NotNil(t, err)
fmt.Printf("Json string parse to person:\n%+v\n", person)
}