-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.go
58 lines (50 loc) · 905 Bytes
/
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
package sql
import (
"bytes"
"database/sql/driver"
"errors"
)
type Json []byte
// Value
func (j Json) Value() (driver.Value, error) {
if j.IsNull() {
return nil, nil
}
return string(j), nil
}
// Scan
func (j *Json) Scan(value interface{}) error {
if value == nil {
*j = nil
return nil
}
s, ok := value.([]byte)
if !ok {
errors.New("Invalid Scan Source")
}
*j = append((*j)[0:0], s...)
return nil
}
// MarshalJSON
func (m Json) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON
func (m *Json) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("null point exception")
}
*m = append((*m)[0:0], data...)
return nil
}
// IsNull
func (j Json) IsNull() bool {
return len(j) == 0 || string(j) == "null"
}
// Equals
func (j Json) Equals(j1 Json) bool {
return bytes.Equal([]byte(j), []byte(j1))
}