blob: f9d3a585a4c05e0baa8261e09a10b7e7bed562ed [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001package runtime
2
3import (
4 "encoding/json"
5 "io"
6)
7
8// JSONBuiltin is a Marshaler which marshals/unmarshals into/from JSON
9// with the standard "encoding/json" package of Golang.
10// Although it is generally faster for simple proto messages than JSONPb,
11// it does not support advanced features of protobuf, e.g. map, oneof, ....
12//
13// The NewEncoder and NewDecoder types return *json.Encoder and
14// *json.Decoder respectively.
15type JSONBuiltin struct{}
16
17// ContentType always Returns "application/json".
18func (*JSONBuiltin) ContentType() string {
19 return "application/json"
20}
21
22// Marshal marshals "v" into JSON
23func (j *JSONBuiltin) Marshal(v interface{}) ([]byte, error) {
24 return json.Marshal(v)
25}
26
27// Unmarshal unmarshals JSON data into "v".
28func (j *JSONBuiltin) Unmarshal(data []byte, v interface{}) error {
29 return json.Unmarshal(data, v)
30}
31
32// NewDecoder returns a Decoder which reads JSON stream from "r".
33func (j *JSONBuiltin) NewDecoder(r io.Reader) Decoder {
34 return json.NewDecoder(r)
35}
36
37// NewEncoder returns an Encoder which writes JSON stream into "w".
38func (j *JSONBuiltin) NewEncoder(w io.Writer) Encoder {
39 return json.NewEncoder(w)
40}
41
42// Delimiter for newline encoded JSON streams.
43func (j *JSONBuiltin) Delimiter() []byte {
44 return []byte("\n")
45}