-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
32 lines (25 loc) · 864 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
package encoders
import (
"encoding/json"
"io"
)
var _ Marshaller = (*jsonEncoder)(nil)
var _ WriterEncoder = (*jsonEncoder)(nil)
// jsonEncoder implements Marshaller for the JSON format.
type jsonEncoder struct{}
// Write writes the JSON encoding of v to w.
func (jsonEncoder) Write(w io.Writer, v interface{}) error {
return json.NewEncoder(w).Encode(v)
}
// Marshal returns the JSON encoding of v.
func (jsonEncoder) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
// Unmarshal parses the JSON-encoded data and stores the results
// in the value pointed to by v. If v is nil or not a pointer,
// Unmarshal returns a json.InvalidUnmarshalError.
func (jsonEncoder) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
// JSON holds a json encoder instance implementing Marshaller.
var JSON = &jsonEncoder{}