-
Notifications
You must be signed in to change notification settings - Fork 6
/
json.go
77 lines (67 loc) · 1.45 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
// Code generated by queryx, DO NOT EDIT.
package queryx
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
type JSON struct {
Val map[string]interface{}
Valid bool
Set bool
}
func NewJSON(v map[string]interface{}) JSON {
return JSON{Val: v, Valid: true, Set: true}
}
func NewNullableJSON(v interface{}) JSON {
if v != nil {
return NewJSON(v.(map[string]interface{}))
}
return JSON{Set: true}
}
// Scan implements the Scanner interface.
func (j *JSON) Scan(value interface{}) error {
if value == nil {
j.Val, j.Valid = nil, false
return nil
}
b, ok := value.([]byte)
if !ok {
return fmt.Errorf("JSON scan source was not []byte")
}
return json.Unmarshal(b, &j.Val)
}
// Value implements the driver Valuer interface.
func (j JSON) Value() (driver.Value, error) {
return json.Marshal(j.Val)
}
// MarshalJSON implements the json.Marshaler interface.
func (j JSON) MarshalJSON() ([]byte, error) {
if !j.Valid {
return json.Marshal(nil)
}
return json.Marshal(j.Val)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (j *JSON) UnmarshalJSON(data []byte) error {
j.Set = true
s := string(data)
if s == "{}" || s == "null" {
return nil
}
j.Valid = true
m := map[string]interface{}{}
err := json.Unmarshal(data, &m)
if err != nil {
return err
}
j.Val = m
return nil
}
// String implements the stringer interface.
func (j JSON) String() string {
if !j.Valid {
return "null"
}
return fmt.Sprintf("%+v", j.Val)
}