marshal_json.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package runtime
  2. import (
  3. "encoding/json"
  4. "io"
  5. )
  6. // JSONBuiltin is a Marshaler which marshals/unmarshals into/from JSON
  7. // with the standard "encoding/json" package of Golang.
  8. // Although it is generally faster for simple proto messages than JSONPb,
  9. // it does not support advanced features of protobuf, e.g. map, oneof, ....
  10. type JSONBuiltin struct{}
  11. // ContentType always Returns "application/json".
  12. func (*JSONBuiltin) ContentType() string {
  13. return "application/json"
  14. }
  15. // Marshal marshals "v" into JSON
  16. func (j *JSONBuiltin) Marshal(v interface{}) ([]byte, error) {
  17. return json.Marshal(v)
  18. }
  19. // Unmarshal unmarshals JSON data into "v".
  20. func (j *JSONBuiltin) Unmarshal(data []byte, v interface{}) error {
  21. return json.Unmarshal(data, v)
  22. }
  23. // NewDecoder returns a Decoder which reads JSON stream from "r".
  24. func (j *JSONBuiltin) NewDecoder(r io.Reader) Decoder {
  25. return json.NewDecoder(r)
  26. }
  27. // NewEncoder returns an Encoder which writes JSON stream into "w".
  28. func (j *JSONBuiltin) NewEncoder(w io.Writer) Encoder {
  29. return json.NewEncoder(w)
  30. }