forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.go
179 lines (154 loc) · 4.61 KB
/
json.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package carbon
import (
"bytes"
"fmt"
"strconv"
)
// ToDateTimeString 日期时间字符串
type ToDateTimeString struct {
Carbon
}
// ToDateString 日期字符串
type ToDateString struct {
Carbon
}
// ToTimeString 时间字符串
type ToTimeString struct {
Carbon
}
// ToTimestamp 秒级时间戳
type ToTimestamp struct {
Carbon
}
// ToTimestampWithSecond 秒级时间戳
type ToTimestampWithSecond struct {
Carbon
}
// ToTimestampWithMillisecond 毫秒级时间戳
type ToTimestampWithMillisecond struct {
Carbon
}
// ToTimestampWithMicrosecond 微秒级时间戳
type ToTimestampWithMicrosecond struct {
Carbon
}
// ToTimestampWithNanosecond 纳秒级时间戳
type ToTimestampWithNanosecond struct {
Carbon
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToDateTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.ToDateTimeString())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToDateTimeString) UnmarshalJSON(b []byte) error {
c := Parse(string(bytes.Trim(b, `"`)))
if c.Error == nil {
*t = ToDateTimeString{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToDateString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.ToDateString())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToDateString) UnmarshalJSON(b []byte) error {
c := Parse(string(bytes.Trim(b, `"`)))
if c.Error == nil {
*t = ToDateString{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.ToTimeString())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimeString) UnmarshalJSON(b []byte) error {
c := ParseByFormat(string(bytes.Trim(b, `"`)), "H:i:s")
if c.Error == nil {
*t = ToTimeString{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, t.ToTimestamp())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if ts == 0 || err != nil {
return err
}
c := CreateFromTimestamp(ts)
if c.Error == nil {
*t = ToTimestamp{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimestampWithSecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, t.ToTimestampWithSecond())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimestampWithSecond) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if ts == 0 || err != nil {
return err
}
c := CreateFromTimestamp(ts)
if c.Error == nil {
*t = ToTimestampWithSecond{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, t.ToTimestampWithMillisecond())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimestampWithMillisecond) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if ts == 0 || err != nil {
return err
}
c := CreateFromTimestamp(ts)
if c.Error == nil {
*t = ToTimestampWithMillisecond{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, t.ToTimestampWithMicrosecond())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimestampWithMicrosecond) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if ts == 0 || err != nil {
return err
}
c := CreateFromTimestamp(ts)
if c.Error == nil {
*t = ToTimestampWithMicrosecond{c}
}
return nil
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (t ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, t.ToTimestampWithNanosecond())), nil
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (t *ToTimestampWithNanosecond) UnmarshalJSON(b []byte) error {
ts, err := strconv.ParseInt(string(b), 10, 64)
if ts == 0 || err != nil {
return err
}
c := CreateFromTimestamp(ts)
if c.Error == nil {
*t = ToTimestampWithNanosecond{c}
}
return nil
}