marshal_jsonpb.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package runtime
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "reflect"
  8. "github.com/golang/protobuf/jsonpb"
  9. "github.com/golang/protobuf/proto"
  10. )
  11. // JSONPb is a Marshaler which marshals/unmarshals into/from JSON
  12. // with the "github.com/golang/protobuf/jsonpb".
  13. // It supports fully functionality of protobuf unlike JSONBuiltin.
  14. type JSONPb jsonpb.Marshaler
  15. // ContentType always returns "application/json".
  16. func (*JSONPb) ContentType() string {
  17. return "application/json"
  18. }
  19. // Marshal marshals "v" into JSON
  20. // Currently it can marshal only proto.Message.
  21. // TODO(yugui) Support fields of primitive types in a message.
  22. func (j *JSONPb) Marshal(v interface{}) ([]byte, error) {
  23. if _, ok := v.(proto.Message); !ok {
  24. return j.marshalNonProtoField(v)
  25. }
  26. var buf bytes.Buffer
  27. if err := j.marshalTo(&buf, v); err != nil {
  28. return nil, err
  29. }
  30. return buf.Bytes(), nil
  31. }
  32. func (j *JSONPb) marshalTo(w io.Writer, v interface{}) error {
  33. p, ok := v.(proto.Message)
  34. if !ok {
  35. buf, err := j.marshalNonProtoField(v)
  36. if err != nil {
  37. return err
  38. }
  39. _, err = w.Write(buf)
  40. return err
  41. }
  42. return (*jsonpb.Marshaler)(j).Marshal(w, p)
  43. }
  44. // marshalNonProto marshals a non-message field of a protobuf message.
  45. // This function does not correctly marshals arbitary data structure into JSON,
  46. // but it is only capable of marshaling non-message field values of protobuf,
  47. // i.e. primitive types, enums; pointers to primitives or enums; maps from
  48. // integer/string types to primitives/enums/pointers to messages.
  49. func (j *JSONPb) marshalNonProtoField(v interface{}) ([]byte, error) {
  50. rv := reflect.ValueOf(v)
  51. for rv.Kind() == reflect.Ptr {
  52. if rv.IsNil() {
  53. return []byte("null"), nil
  54. }
  55. rv = rv.Elem()
  56. }
  57. if rv.Kind() == reflect.Map {
  58. m := make(map[string]*json.RawMessage)
  59. for _, k := range rv.MapKeys() {
  60. buf, err := j.Marshal(rv.MapIndex(k).Interface())
  61. if err != nil {
  62. return nil, err
  63. }
  64. m[fmt.Sprintf("%v", k.Interface())] = (*json.RawMessage)(&buf)
  65. }
  66. if j.Indent != "" {
  67. return json.MarshalIndent(m, "", j.Indent)
  68. }
  69. return json.Marshal(m)
  70. }
  71. if enum, ok := rv.Interface().(protoEnum); ok && !j.EnumsAsInts {
  72. return json.Marshal(enum.String())
  73. }
  74. return json.Marshal(rv.Interface())
  75. }
  76. // Unmarshal unmarshals JSON "data" into "v"
  77. // Currently it can marshal only proto.Message.
  78. // TODO(yugui) Support fields of primitive types in a message.
  79. func (j *JSONPb) Unmarshal(data []byte, v interface{}) error {
  80. return unmarshalJSONPb(data, v)
  81. }
  82. // NewDecoder returns a Decoder which reads JSON stream from "r".
  83. func (j *JSONPb) NewDecoder(r io.Reader) Decoder {
  84. d := json.NewDecoder(r)
  85. return DecoderFunc(func(v interface{}) error { return decodeJSONPb(d, v) })
  86. }
  87. // NewEncoder returns an Encoder which writes JSON stream into "w".
  88. func (j *JSONPb) NewEncoder(w io.Writer) Encoder {
  89. return EncoderFunc(func(v interface{}) error { return j.marshalTo(w, v) })
  90. }
  91. func unmarshalJSONPb(data []byte, v interface{}) error {
  92. d := json.NewDecoder(bytes.NewReader(data))
  93. return decodeJSONPb(d, v)
  94. }
  95. func decodeJSONPb(d *json.Decoder, v interface{}) error {
  96. p, ok := v.(proto.Message)
  97. if !ok {
  98. return decodeNonProtoField(d, v)
  99. }
  100. return jsonpb.UnmarshalNext(d, p)
  101. }
  102. func decodeNonProtoField(d *json.Decoder, v interface{}) error {
  103. rv := reflect.ValueOf(v)
  104. if rv.Kind() != reflect.Ptr {
  105. return fmt.Errorf("%T is not a pointer", v)
  106. }
  107. for rv.Kind() == reflect.Ptr {
  108. if rv.IsNil() {
  109. rv.Set(reflect.New(rv.Type().Elem()))
  110. }
  111. if rv.Type().ConvertibleTo(typeProtoMessage) {
  112. return jsonpb.UnmarshalNext(d, rv.Interface().(proto.Message))
  113. }
  114. rv = rv.Elem()
  115. }
  116. if rv.Kind() == reflect.Map {
  117. if rv.IsNil() {
  118. rv.Set(reflect.MakeMap(rv.Type()))
  119. }
  120. conv, ok := convFromType[rv.Type().Key().Kind()]
  121. if !ok {
  122. return fmt.Errorf("unsupported type of map field key: %v", rv.Type().Key())
  123. }
  124. m := make(map[string]*json.RawMessage)
  125. if err := d.Decode(&m); err != nil {
  126. return err
  127. }
  128. for k, v := range m {
  129. result := conv.Call([]reflect.Value{reflect.ValueOf(k)})
  130. if err := result[1].Interface(); err != nil {
  131. return err.(error)
  132. }
  133. bk := result[0]
  134. bv := reflect.New(rv.Type().Elem())
  135. if err := unmarshalJSONPb([]byte(*v), bv.Interface()); err != nil {
  136. return err
  137. }
  138. rv.SetMapIndex(bk, bv.Elem())
  139. }
  140. return nil
  141. }
  142. if _, ok := rv.Interface().(protoEnum); ok {
  143. var repr interface{}
  144. if err := d.Decode(&repr); err != nil {
  145. return err
  146. }
  147. switch repr.(type) {
  148. case string:
  149. // TODO(yugui) Should use proto.StructProperties?
  150. return fmt.Errorf("unmarshaling of symbolic enum %q not supported: %T", repr, rv.Interface())
  151. case float64:
  152. rv.Set(reflect.ValueOf(int32(repr.(float64))).Convert(rv.Type()))
  153. return nil
  154. default:
  155. return fmt.Errorf("cannot assign %#v into Go type %T", repr, rv.Interface())
  156. }
  157. }
  158. return d.Decode(v)
  159. }
  160. type protoEnum interface {
  161. fmt.Stringer
  162. EnumDescriptor() ([]byte, []int)
  163. }
  164. var typeProtoMessage = reflect.TypeOf((*proto.Message)(nil)).Elem()