-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.go
More file actions
89 lines (72 loc) · 2.22 KB
/
json.go
File metadata and controls
89 lines (72 loc) · 2.22 KB
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
package requests
import (
"bytes"
"fmt"
"io"
"github.com/go-json-experiment/json"
)
// JSONEncoder handles encoding of JSON data.
type JSONEncoder struct {
MarshalFunc func(v any) ([]byte, error) // MarshalFunc marshals a value into JSON.
}
// Encode marshals the provided value into JSON format.
func (e *JSONEncoder) Encode(v any) (io.Reader, error) {
var data []byte
var err error
if e.MarshalFunc != nil {
data, err = e.MarshalFunc(v)
} else {
data, err = json.Marshal(v)
}
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrEncodingFailed, err)
}
buf := GetBuffer()
_, err = buf.Write(data)
if err != nil {
PutBuffer(buf)
return nil, fmt.Errorf("failed to write JSON to buffer: %w", err)
}
return &poolReader{Reader: bytes.NewReader(buf.B), poolBuf: buf}, nil
}
// ContentType returns the content type for JSON data.
func (e *JSONEncoder) ContentType() string {
return "application/json;charset=utf-8"
}
// jsonMarshal wraps JSON v2 marshal to match the expected signature.
func jsonMarshal(v any) ([]byte, error) {
return json.Marshal(v)
}
// DefaultJSONEncoder is the default JSONEncoder instance using the JSON v2 marshal function.
var DefaultJSONEncoder = &JSONEncoder{
MarshalFunc: jsonMarshal,
}
// JSONDecoder handles decoding of JSON data.
type JSONDecoder struct {
UnmarshalFunc func(data []byte, v any) error // UnmarshalFunc unmarshals JSON data into a value.
}
// Decode reads the data from the reader and unmarshals it into the provided value.
func (d *JSONDecoder) Decode(r io.Reader, v any) error {
data, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("failed to read JSON data: %w", err)
}
if d.UnmarshalFunc != nil {
if err := d.UnmarshalFunc(data, v); err != nil {
return fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return nil
}
if err := json.Unmarshal(data, v); err != nil {
return fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return nil
}
// jsonUnmarshal wraps JSON v2 unmarshal to match the expected signature.
func jsonUnmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
// DefaultJSONDecoder is the default JSONDecoder instance using the JSON v2 unmarshal function.
var DefaultJSONDecoder = &JSONDecoder{
UnmarshalFunc: jsonUnmarshal,
}