khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | package runtime |
| 2 | |
| 3 | import ( |
| 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. |
| 15 | type JSONBuiltin struct{} |
| 16 | |
| 17 | // ContentType always Returns "application/json". |
| 18 | func (*JSONBuiltin) ContentType() string { |
| 19 | return "application/json" |
| 20 | } |
| 21 | |
| 22 | // Marshal marshals "v" into JSON |
| 23 | func (j *JSONBuiltin) Marshal(v interface{}) ([]byte, error) { |
| 24 | return json.Marshal(v) |
| 25 | } |
| 26 | |
| 27 | // Unmarshal unmarshals JSON data into "v". |
| 28 | func (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". |
| 33 | func (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". |
| 38 | func (j *JSONBuiltin) NewEncoder(w io.Writer) Encoder { |
| 39 | return json.NewEncoder(w) |
| 40 | } |
| 41 | |
| 42 | // Delimiter for newline encoded JSON streams. |
| 43 | func (j *JSONBuiltin) Delimiter() []byte { |
| 44 | return []byte("\n") |
| 45 | } |