-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
104 lines (85 loc) · 2.05 KB
/
utils.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
99
100
101
102
103
104
package json
import (
"encoding/json"
"errors"
)
//
var (
_nullBytes = []byte("null")
_trueBytes = []byte("true")
_falseBytes = []byte("false")
_quoteBytes = []byte("\"")
_backslashBytes = []byte("\\")
_emptyObjectBytes = []byte("{}")
)
func NullBytes() []byte { return _nullBytes }
func TrueBytes() []byte { return _trueBytes }
func FalseBytes() []byte { return _falseBytes }
func QuoteBytes() []byte { return _quoteBytes }
func BackslashBytes() []byte { return _backslashBytes }
func EmptyObjectBytes() []byte { return _emptyObjectBytes }
func IsNullBytes(b []byte) bool {
return len(b) == 4 && b[0] == 'n' && b[1] == 'u' && b[2] == 'l' && b[3] == 'l'
}
func IsTrueBytes(b []byte) bool {
return len(b) == 4 && b[0] == 't' && b[1] == 'r' && b[2] == 'u' && b[3] == 'e'
}
func IsFalseBytes(b []byte) bool {
return len(b) == 5 && b[0] == 'f' && b[1] == 'a' && b[2] == 'l' && b[3] == 's' && b[4] == 'e'
}
func IsQuoteBytes(b []byte) bool {
return len(b) == 1 && b[0] == '"'
}
func IsBackslashBytes(b []byte) bool {
return len(b) == 1 && b[0] == '\\'
}
func BoolBytes(v bool) []byte {
if v {
return _trueBytes
} else {
return _falseBytes
}
}
//
func MarshalString(v any) (string, error) {
b, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(b), nil
}
func MarshalIndentString(v any, prefix, indent string) (string, error) {
b, err := json.MarshalIndent(v, prefix, indent)
if err != nil {
return "", err
}
return string(b), nil
}
func MarshalPretty(v any) (string, error) {
return MarshalIndentString(v, "", " ")
}
//
func UnmarshalAs[T any](b []byte) (v T, err error) {
err = json.Unmarshal(b, &v)
return
}
//
func ReadDelim(dec *json.Decoder) (json.Delim, error) {
token, err := dec.Token()
if err != nil {
return 0, err
}
if delim, ok := token.(json.Delim); ok {
return delim, nil
}
return 0, errors.New("expected delim")
}
func EndDelim(d json.Delim) json.Delim {
switch d {
case '{':
return '}'
case '[':
return ']'
}
panic(errors.New("unreachable"))
}