-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
56 lines (47 loc) · 1.09 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
package miso
import (
"io"
"unicode"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
)
func init() {
extra.SetNamingStrategy(LowercaseNamingStrategy)
}
// Parse JSON using jsoniter.
func ParseJson(body []byte, ptr any) error {
e := jsoniter.Unmarshal(body, ptr)
return e
}
// Write JSON using jsoniter.
func WriteJson(body any) ([]byte, error) {
return jsoniter.Marshal(body)
}
// Write JSON as string using jsoniter.
func SWriteJson(body any) (string, error) {
if v, ok := body.(string); ok {
return v, nil
}
buf, err := WriteJson(body)
if err != nil {
return "", err
}
return UnsafeByt2Str(buf), nil
}
// Decode JSON using jsoniter.
func DecodeJson(reader io.Reader, ptr any) error {
return jsoniter.NewDecoder(reader).Decode(ptr)
}
// Encode JSON using jsoniter.
func EncodeJson(writer io.Writer, body any) error {
return jsoniter.NewEncoder(writer).Encode(body)
}
// Change first rune to lower case.
func LowercaseNamingStrategy(name string) string {
ru := []rune(name)
if len(ru) < 1 {
return name
}
ru[0] = unicode.ToLower(ru[0])
return string(ru)
}