-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_to_json.go
More file actions
88 lines (71 loc) · 1.49 KB
/
sql_to_json.go
File metadata and controls
88 lines (71 loc) · 1.49 KB
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
package eqtypes
import (
"database/sql"
"encoding/json"
"time"
)
type JsonNullTime struct {
sql.NullTime
}
//goland:noinspection GoMixedReceiverTypes
func (v JsonNullTime) MarshalJSON() ([]byte, error) {
if v.Valid {
return json.Marshal(v.Time)
}
return json.Marshal(nil)
}
//goland:noinspection GoMixedReceiverTypes
func (v *JsonNullTime) UnmarshalJSON(data []byte) error {
// Unmarshalling into a pointer will let us detect null
var x *time.Time
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x != nil {
v.Valid = true
v.Time = *x
} else {
v.Valid = false
}
return nil
}
type JsonNullString struct {
sql.NullString
}
//goland:noinspection GoMixedReceiverTypes
func (v JsonNullString) MarshalJSON() ([]byte, error) {
if v.Valid {
return json.Marshal(v.String)
}
return json.Marshal("")
}
//goland:noinspection GoMixedReceiverTypes
func (v *JsonNullString) UnmarshalJSON(data []byte) error {
var x *string
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x != nil {
v.Valid = true
v.String = *x
} else {
v.Valid = false
}
return nil
}
type UnixTimestamp struct {
time.Time
}
//goland:noinspection GoMixedReceiverTypes
func (v UnixTimestamp) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Unix())
}
//goland:noinspection GoMixedReceiverTypes
func (v *UnixTimestamp) UnmarshalJSON(data []byte) error {
var t time.Time
if err := json.Unmarshal(data, &t); err != nil {
return err
}
*v = UnixTimestamp{t}
return nil
}