-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
98 lines (78 loc) · 1.73 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
package datebin
import (
"bytes"
"fmt"
"strconv"
)
// 转换为 json
// Marshal to JSON
func (this Datebin) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, this.ToDatetimeString())), nil
}
// 解析 json
// Unmarshal JSON data
func (this *Datebin) UnmarshalJSON(val []byte) error {
c := Parse(string(bytes.Trim(val, `"`)))
if c.Error() == nil {
*this = c
}
return nil
}
// =============
// 日期时间
// DateTime struct
type DateTime Datebin
// 转换为 json
// Marshal to JSON
func (this DateTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, Datebin(this).ToDatetimeString())), nil
}
// 解析 json
// Unmarshal JSON data
func (this *DateTime) UnmarshalJSON(val []byte) error {
c := Parse(string(bytes.Trim(val, `"`)))
if c.Error() == nil {
*this = DateTime(c)
}
return nil
}
// =============
// 日期
// Date struct
type Date Datebin
// 转换为 json
// Marshal to JSON
func (this Date) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, Datebin(this).ToDateString())), nil
}
// 解析 json
// Unmarshal JSON data
func (this *Date) UnmarshalJSON(val []byte) error {
c := Parse(string(bytes.Trim(val, `"`)))
if c.Error() == nil {
*this = Date(c)
}
return nil
}
// =============
// 时间戳
// Timestamp struct
type Timestamp Datebin
// 转换为 json
// Marshal to JSON
func (this Timestamp) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%d`, Datebin(this).Timestamp())), nil
}
// 解析 json
// Unmarshal JSON data
func (this *Timestamp) UnmarshalJSON(val []byte) error {
ts, err := strconv.ParseInt(string(val), 10, 64)
if ts == 0 || err != nil {
return err
}
c := FromTimestamp(ts)
if c.Error() == nil {
*this = Timestamp(c)
}
return nil
}