-
Notifications
You must be signed in to change notification settings - Fork 1
/
types_json.go
67 lines (57 loc) · 1.39 KB
/
types_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
package sqlutil
import (
"bytes"
"database/sql/driver"
"fmt"
"github.com/jxskiss/gopkg/v2/easy/ezmap"
"github.com/jxskiss/gopkg/v2/internal/unsafeheader"
"github.com/jxskiss/gopkg/v2/perf/json"
)
//nolint:unused
var (
null = []byte("null")
emptyObject = []byte("{}")
)
// JSON holds a map[string]any value, it implements
// sql/driver.Valuer and sql.Scanner. It uses JSON to do serialization.
//
// JSON embeds a gemap.Map, thus all methods defined on gemap.Map is also
// available from a JSON instance.
type JSON struct {
ezmap.Map
}
// Value implements driver.Valuer interface.
func (p JSON) Value() (driver.Value, error) {
if p.Map == nil {
return emptyObject, nil
}
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(p.Map)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Scan implements sql.Scanner interface.
func (p *JSON) Scan(src any) error {
var data []byte
switch v := src.(type) {
case []byte:
data = v
case string:
data = unsafeheader.StringToBytes(v)
default:
return fmt.Errorf("sqlutil.JSON.Scan: want []byte/string but got %T", src)
}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
return dec.Decode(&p.Map)
}
func (p JSON) MarshalJSON() ([]byte, error) {
return json.Marshal(p.Map)
}
func (p *JSON) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &p.Map)
}