diff --git a/runtime/marshal_json.go b/runtime/marshal_json.go index b3a21418be4..f9d3a585a4c 100644 --- a/runtime/marshal_json.go +++ b/runtime/marshal_json.go @@ -9,6 +9,9 @@ import ( // with the standard "encoding/json" package of Golang. // Although it is generally faster for simple proto messages than JSONPb, // it does not support advanced features of protobuf, e.g. map, oneof, .... +// +// The NewEncoder and NewDecoder types return *json.Encoder and +// *json.Decoder respectively. type JSONBuiltin struct{} // ContentType always Returns "application/json". diff --git a/runtime/marshal_jsonpb.go b/runtime/marshal_jsonpb.go index 0a0d130bba6..550681a78a7 100644 --- a/runtime/marshal_jsonpb.go +++ b/runtime/marshal_jsonpb.go @@ -14,6 +14,9 @@ import ( // JSONPb is a Marshaler which marshals/unmarshals into/from JSON // with the "github.com/golang/protobuf/jsonpb". // It supports fully functionality of protobuf unlike JSONBuiltin. +// +// The NewDecoder method returns a DecoderWrapper, so the underlying +// *json.Decoder methods can be used. type JSONPb jsonpb.Marshaler // ContentType always returns "application/json". @@ -84,8 +87,6 @@ func (j *JSONPb) marshalNonProtoField(v interface{}) ([]byte, error) { } // Unmarshal unmarshals JSON "data" into "v" -// Currently it can marshal only proto.Message. -// TODO(yugui) Support fields of primitive types in a message. func (j *JSONPb) Unmarshal(data []byte, v interface{}) error { return unmarshalJSONPb(data, v) } @@ -93,7 +94,17 @@ func (j *JSONPb) Unmarshal(data []byte, v interface{}) error { // NewDecoder returns a Decoder which reads JSON stream from "r". func (j *JSONPb) NewDecoder(r io.Reader) Decoder { d := json.NewDecoder(r) - return DecoderFunc(func(v interface{}) error { return decodeJSONPb(d, v) }) + return DecoderWrapper{Decoder: d} +} + +// DecoderWrapper is a wrapper around a *json.Decoder that adds +// support for protos to the Decode method. +type DecoderWrapper struct { + *json.Decoder +} + +func (d DecoderWrapper) Decode(v interface{}) error { + return decodeJSONPb(d.Decoder, v) } // NewEncoder returns an Encoder which writes JSON stream into "w".