jsonpb: New package.
This is the official JSON support for protocol buffers,
matching the standard format for proto3.
diff --git a/jsonpb/jsonpb.go b/jsonpb/jsonpb.go
new file mode 100644
index 0000000..f3e53d4
--- /dev/null
+++ b/jsonpb/jsonpb.go
@@ -0,0 +1,429 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2015 The Go Authors. All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+/*
+Package jsonpb provides marshalling/unmarshalling functionality between
+protocol buffer and JSON objects.
+
+Compared to encoding/json, this library:
+ - encodes int64, uint64 as strings
+ - optionally encodes enums as strings
+*/
+package jsonpb
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "reflect"
+ "sort"
+ "strconv"
+ "strings"
+
+ "github.com/golang/protobuf/proto"
+)
+
+var (
+ byteArrayType = reflect.TypeOf([]byte{})
+)
+
+// Marshaller is a configurable object for converting between
+// protocol buffer objects and a JSON representation for them
+type Marshaller struct {
+ // Use string values for enums (as opposed to integer values)
+ EnumsAsString bool
+
+ // A string to indent each level by. The presence of this field will
+ // also cause a space to appear between the field separator and
+ // value, and for newlines to be appear between fields and array
+ // elements.
+ Indent string
+}
+
+// Marshal marshals a protocol buffer into JSON.
+func (m *Marshaller) Marshal(out io.Writer, pb proto.Message) error {
+ writer := &errWriter{writer: out}
+ return m.marshalObject(writer, pb, "")
+}
+
+// MarshalToString converts a protocol buffer object to JSON string.
+func (m *Marshaller) MarshalToString(pb proto.Message) (string, error) {
+ var buf bytes.Buffer
+ if err := m.Marshal(&buf, pb); err != nil {
+ return "", err
+ }
+ return buf.String(), nil
+}
+
+// marshalObject writes a struct to the Writer.
+func (m *Marshaller) marshalObject(out *errWriter, v proto.Message, indent string) error {
+ out.write("{")
+ if m.Indent != "" {
+ out.write("\n")
+ }
+
+ s := reflect.ValueOf(v).Elem()
+ writeBeforeField := ""
+ for i := 0; i < s.NumField(); i++ {
+ value := s.Field(i)
+ valueField := s.Type().Field(i)
+ fieldName, omitFieldIfNil := parseFieldOptions(valueField)
+
+ // Fields which should not be serialized will specify a json tag with '-'
+ // TODO: proto3 objects should have default values omitted.
+ if fieldName == "-" {
+ continue
+ } else if omitFieldIfNil {
+ // IsNil will panic on most value kinds.
+ skip := false
+ switch value.Kind() {
+ case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
+ skip = value.IsNil()
+ }
+ if skip {
+ continue
+ }
+ }
+
+ out.write(writeBeforeField)
+ if m.Indent != "" {
+ out.write(indent)
+ out.write(m.Indent)
+ }
+ out.write(`"`)
+ out.write(fieldName)
+ out.write(`":`)
+ if m.Indent != "" {
+ out.write(" ")
+ }
+
+ if err := m.marshalValue(out, value, valueField, indent); err != nil {
+ return err
+ }
+
+ if m.Indent != "" {
+ writeBeforeField = ",\n"
+ } else {
+ writeBeforeField = ","
+ }
+ }
+
+ if m.Indent != "" {
+ out.write("\n")
+ out.write(indent)
+ }
+ out.write("}")
+ return out.err
+}
+
+// marshalValue writes the value to the Writer.
+func (m *Marshaller) marshalValue(out *errWriter, v reflect.Value,
+ structField reflect.StructField, indent string) error {
+
+ var err error
+ v = reflect.Indirect(v)
+
+ // Handle repeated elements.
+ if v.Type() != byteArrayType && v.Kind() == reflect.Slice {
+ out.write("[")
+ comma := ""
+ for i := 0; i < v.Len(); i++ {
+ sliceVal := v.Index(i)
+ out.write(comma)
+ if m.Indent != "" {
+ out.write("\n")
+ out.write(indent)
+ out.write(m.Indent)
+ out.write(m.Indent)
+ }
+ m.marshalValue(out, sliceVal, structField, indent+m.Indent)
+ comma = ","
+ }
+ if m.Indent != "" {
+ out.write("\n")
+ out.write(indent)
+ out.write(m.Indent)
+ }
+ out.write("]")
+ return out.err
+ }
+
+ // Handle enumerations.
+ protoInfo := structField.Tag.Get("protobuf")
+ if m.EnumsAsString && strings.Contains(protoInfo, ",enum=") {
+ // Unknown enum values will are stringified by the proto library as their
+ // value. Such values should _not_ be quoted or they will be intrepreted
+ // as an enum string instead of their value.
+ enumStr := v.Interface().(fmt.Stringer).String()
+ var valStr string
+ if v.Kind() == reflect.Ptr {
+ valStr = strconv.Itoa(int(v.Elem().Int()))
+ } else {
+ valStr = strconv.Itoa(int(v.Int()))
+ }
+ isKnownEnum := enumStr != valStr
+ if isKnownEnum {
+ out.write(`"`)
+ }
+ out.write(enumStr)
+ if isKnownEnum {
+ out.write(`"`)
+ }
+ return out.err
+ }
+
+ // Handle nested messages.
+ if v.Kind() == reflect.Struct {
+ return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent)
+ }
+
+ // Handle maps.
+ // NOTE: Since Go randomizes map iteration, we sort keys for stable output.
+ if v.Kind() == reflect.Map {
+ out.write(`{`)
+ keys := v.MapKeys()
+ sort.Sort(mapKeys(keys))
+ for i, k := range keys {
+ if i > 0 {
+ out.write(`,`)
+ }
+ if m.Indent != "" {
+ out.write("\n")
+ out.write(indent)
+ out.write(m.Indent)
+ out.write(m.Indent)
+ }
+
+ b, err := json.Marshal(k.Interface())
+ if err != nil {
+ return err
+ }
+ s := string(b)
+
+ // If the JSON is not a string value, encode it again to make it one.
+ if !strings.HasPrefix(s, `"`) {
+ b, err := json.Marshal(s)
+ if err != nil {
+ return err
+ }
+ s = string(b)
+ }
+
+ out.write(s)
+ out.write(`:`)
+ if m.Indent != "" {
+ out.write(` `)
+ }
+
+ if err := m.marshalValue(out, v.MapIndex(k), structField, indent+m.Indent); err != nil {
+ return err
+ }
+ }
+ if m.Indent != "" {
+ out.write("\n")
+ out.write(indent)
+ out.write(m.Indent)
+ }
+ out.write(`}`)
+ return out.err
+ }
+
+ // Default handling defers to the encoding/json library.
+ b, err := json.Marshal(v.Interface())
+ if err != nil {
+ return err
+ }
+ needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
+ if needToQuote {
+ out.write(`"`)
+ }
+ out.write(string(b))
+ if needToQuote {
+ out.write(`"`)
+ }
+ return out.err
+}
+
+// Unmarshal unmarshals a JSON object stream into a protocol
+// buffer. This function is lenient and will decode any options
+// permutations of the related Marshaller.
+func Unmarshal(r io.Reader, pb proto.Message) error {
+ inputValue := json.RawMessage{}
+ if err := json.NewDecoder(r).Decode(&inputValue); err != nil {
+ return err
+ }
+ return unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue)
+}
+
+// UnmarshalString will populate the fields of a protocol buffer based
+// on a JSON string. This function is lenient and will decode any options
+// permutations of the related Marshaller.
+func UnmarshalString(str string, pb proto.Message) error {
+ return Unmarshal(bytes.NewReader([]byte(str)), pb)
+}
+
+// unmarshalValue converts/copies a value into the target.
+func unmarshalValue(target reflect.Value, inputValue json.RawMessage) error {
+ targetType := target.Type()
+
+ // Allocate memory for pointer fields.
+ if targetType.Kind() == reflect.Ptr {
+ target.Set(reflect.New(targetType.Elem()))
+ return unmarshalValue(target.Elem(), inputValue)
+ }
+
+ // Handle nested messages.
+ if targetType.Kind() == reflect.Struct {
+ var jsonFields map[string]json.RawMessage
+ if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
+ return err
+ }
+
+ for i := 0; i < target.NumField(); i++ {
+ fieldName, _ := parseFieldOptions(target.Type().Field(i))
+
+ // Fields which should not be serialized will specify a json tag with '-'
+ if fieldName == "-" {
+ continue
+ }
+
+ if valueForField, ok := jsonFields[fieldName]; ok {
+ if err := unmarshalValue(target.Field(i), valueForField); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+ }
+
+ // Handle arrays (which aren't encoded bytes)
+ if targetType != byteArrayType && targetType.Kind() == reflect.Slice {
+ var slc []json.RawMessage
+ if err := json.Unmarshal(inputValue, &slc); err != nil {
+ return err
+ }
+ len := len(slc)
+ target.Set(reflect.MakeSlice(targetType, len, len))
+ for i := 0; i < len; i++ {
+ if err := unmarshalValue(target.Index(i), slc[i]); err != nil {
+ return err
+ }
+ }
+ return nil
+ }
+
+ // Handle maps (whose keys are always strings)
+ if targetType.Kind() == reflect.Map {
+ var mp map[string]json.RawMessage
+ if err := json.Unmarshal(inputValue, &mp); err != nil {
+ return err
+ }
+ target.Set(reflect.MakeMap(targetType))
+ for ks, raw := range mp {
+ // Unmarshal map key. The core json library already decoded the key into a
+ // string, so we handle that specially. Other types were quoted post-serialization.
+ var k reflect.Value
+ if targetType.Key().Kind() == reflect.String {
+ k = reflect.ValueOf(ks)
+ } else {
+ k = reflect.New(targetType.Key()).Elem()
+ if err := unmarshalValue(k, json.RawMessage(ks)); err != nil {
+ return err
+ }
+ }
+
+ // Unmarshal map value.
+ v := reflect.New(targetType.Elem()).Elem()
+ if err := unmarshalValue(v, raw); err != nil {
+ return err
+ }
+ target.SetMapIndex(k, v)
+ }
+ return nil
+ }
+
+ // 64-bit integers can be encoded as strings. In this case we drop
+ // the quotes and proceed as normal.
+ isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64
+ if isNum && strings.HasPrefix(string(inputValue), `"`) {
+ inputValue = inputValue[1 : len(inputValue)-1]
+ }
+
+ // Use the encoding/json for parsing other value types.
+ return json.Unmarshal(inputValue, target.Addr().Interface())
+}
+
+// hasUnmarshalJSON is a interface implemented by protocol buffer enums.
+type hasUnmarshalJSON interface {
+ UnmarshalJSON(data []byte) error
+}
+
+// parseFieldOptions returns the field name and if it should be omited.
+func parseFieldOptions(f reflect.StructField) (string, bool) {
+ name := f.Name
+ omitEmpty := false
+ tag := f.Tag.Get("json")
+ tagParts := strings.Split(tag, ",")
+ for i := range tagParts {
+ if tagParts[i] == "omitempty" || tagParts[i] == "" {
+ omitEmpty = true
+ } else {
+ name = tagParts[i]
+ }
+ }
+ return name, omitEmpty
+}
+
+// Writer wrapper inspired by https://blog.golang.org/errors-are-values
+type errWriter struct {
+ writer io.Writer
+ err error
+}
+
+func (w *errWriter) write(str string) {
+ if w.err != nil {
+ return
+ }
+ _, w.err = w.writer.Write([]byte(str))
+}
+
+// Map fields may have key types of non-float scalars, strings and enums.
+// The easiest way to sort them in some deterministic order is to use fmt.
+// If this turns out to be inefficient we can always consider other options,
+// such as doing a Schwartzian transform.
+type mapKeys []reflect.Value
+
+func (s mapKeys) Len() int { return len(s) }
+func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+func (s mapKeys) Less(i, j int) bool {
+ return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
+}
diff --git a/jsonpb/jsonpb_test.go b/jsonpb/jsonpb_test.go
new file mode 100644
index 0000000..fe8cd3f
--- /dev/null
+++ b/jsonpb/jsonpb_test.go
@@ -0,0 +1,367 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2015 The Go Authors. All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+package jsonpb
+
+import (
+ "testing"
+
+ pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
+ "github.com/golang/protobuf/proto"
+)
+
+var (
+ marshaller = Marshaller{}
+
+ marshallerAllOptions = Marshaller{
+ EnumsAsString: true,
+ Indent: " ",
+ }
+
+ simpleObject = &pb.Simple{
+ OInt32: proto.Int32(-32),
+ OInt64: proto.Int64(-6400000000),
+ OUint32: proto.Uint32(32),
+ OUint64: proto.Uint64(6400000000),
+ OSint32: proto.Int32(-13),
+ OSint64: proto.Int64(-2600000000),
+ OFloat: proto.Float32(3.14),
+ ODouble: proto.Float64(6.02214179e23),
+ OBool: proto.Bool(true),
+ OString: proto.String("hello \"there\""),
+ OBytes: []byte("beep boop"),
+ }
+
+ simpleObjectJSON = `{` +
+ `"o_bool":true,` +
+ `"o_int32":-32,` +
+ `"o_int64":"-6400000000",` +
+ `"o_uint32":32,` +
+ `"o_uint64":"6400000000",` +
+ `"o_sint32":-13,` +
+ `"o_sint64":"-2600000000",` +
+ `"o_float":3.14,` +
+ `"o_double":6.02214179e+23,` +
+ `"o_string":"hello \"there\"",` +
+ `"o_bytes":"YmVlcCBib29w"` +
+ `}`
+
+ simpleObjectPrettyJSON = `{
+ "o_bool": true,
+ "o_int32": -32,
+ "o_int64": "-6400000000",
+ "o_uint32": 32,
+ "o_uint64": "6400000000",
+ "o_sint32": -13,
+ "o_sint64": "-2600000000",
+ "o_float": 3.14,
+ "o_double": 6.02214179e+23,
+ "o_string": "hello \"there\"",
+ "o_bytes": "YmVlcCBib29w"
+}`
+
+ repeatsObject = &pb.Repeats{
+ RBool: []bool{true, false, true},
+ RInt32: []int32{-3, -4, -5},
+ RInt64: []int64{-123456789, -987654321},
+ RUint32: []uint32{1, 2, 3},
+ RUint64: []uint64{6789012345, 3456789012},
+ RSint32: []int32{-1, -2, -3},
+ RSint64: []int64{-6789012345, -3456789012},
+ RFloat: []float32{3.14, 6.28},
+ RDouble: []float64{299792458, 6.62606957e-34},
+ RString: []string{"happy", "days"},
+ RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
+ }
+
+ repeatsObjectJSON = `{` +
+ `"r_bool":[true,false,true],` +
+ `"r_int32":[-3,-4,-5],` +
+ `"r_int64":["-123456789","-987654321"],` +
+ `"r_uint32":[1,2,3],` +
+ `"r_uint64":["6789012345","3456789012"],` +
+ `"r_sint32":[-1,-2,-3],` +
+ `"r_sint64":["-6789012345","-3456789012"],` +
+ `"r_float":[3.14,6.28],` +
+ `"r_double":[2.99792458e+08,6.62606957e-34],` +
+ `"r_string":["happy","days"],` +
+ `"r_bytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
+ `}`
+
+ repeatsObjectPrettyJSON = `{
+ "r_bool": [
+ true,
+ false,
+ true
+ ],
+ "r_int32": [
+ -3,
+ -4,
+ -5
+ ],
+ "r_int64": [
+ "-123456789",
+ "-987654321"
+ ],
+ "r_uint32": [
+ 1,
+ 2,
+ 3
+ ],
+ "r_uint64": [
+ "6789012345",
+ "3456789012"
+ ],
+ "r_sint32": [
+ -1,
+ -2,
+ -3
+ ],
+ "r_sint64": [
+ "-6789012345",
+ "-3456789012"
+ ],
+ "r_float": [
+ 3.14,
+ 6.28
+ ],
+ "r_double": [
+ 2.99792458e+08,
+ 6.62606957e-34
+ ],
+ "r_string": [
+ "happy",
+ "days"
+ ],
+ "r_bytes": [
+ "c2tpdHRsZXM=",
+ "bSZtJ3M="
+ ]
+}`
+
+ innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
+ innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
+ innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
+ innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
+ complexObject = &pb.Widget{
+ Color: pb.Widget_GREEN.Enum(),
+ RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
+ Simple: innerSimple,
+ RSimple: []*pb.Simple{innerSimple, innerSimple2},
+ Repeats: innerRepeats,
+ RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
+ }
+
+ complexObjectJSON = `{"color":1,` +
+ `"r_color":[0,1,2],` +
+ `"simple":{"o_int32":-32},` +
+ `"r_simple":[{"o_int32":-32},{"o_int64":"25"}],` +
+ `"repeats":{"r_string":["roses","red"]},` +
+ `"r_repeats":[{"r_string":["roses","red"]},{"r_string":["violets","blue"]}]` +
+ `}`
+
+ complexObjectPrettyJSON = `{
+ "color": "GREEN",
+ "r_color": [
+ "RED",
+ "GREEN",
+ "BLUE"
+ ],
+ "simple": {
+ "o_int32": -32
+ },
+ "r_simple": [
+ {
+ "o_int32": -32
+ },
+ {
+ "o_int64": "25"
+ }
+ ],
+ "repeats": {
+ "r_string": [
+ "roses",
+ "red"
+ ]
+ },
+ "r_repeats": [
+ {
+ "r_string": [
+ "roses",
+ "red"
+ ]
+ },
+ {
+ "r_string": [
+ "violets",
+ "blue"
+ ]
+ }
+ ]
+}`
+
+ colorPrettyJSON = `{
+ "color": 2
+}`
+
+ colorListPrettyJSON = `{
+ "color": 1000,
+ "r_color": [
+ "RED"
+ ]
+}`
+
+ nummyPrettyJSON = `{
+ "nummy": {
+ "1": 2,
+ "3": 4
+ }
+}`
+
+ objjyPrettyJSON = `{
+ "objjy": {
+ "1": {
+ "dub": 1
+ }
+ }
+}`
+)
+
+var marshallingTests = []struct {
+ desc string
+ marshaller Marshaller
+ pb proto.Message
+ json string
+}{
+ {"simple flat object", marshaller, simpleObject, simpleObjectJSON},
+ {"simple pretty object", marshallerAllOptions, simpleObject, simpleObjectPrettyJSON},
+ {"repeated fields flat object", marshaller, repeatsObject, repeatsObjectJSON},
+ {"repeated fields pretty object", marshallerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
+ {"nested message/enum flat object", marshaller, complexObject, complexObjectJSON},
+ {"nested message/enum pretty object", marshallerAllOptions, complexObject, complexObjectPrettyJSON},
+ {"enum-string flat object", Marshaller{EnumsAsString: true},
+ &pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
+ {"enum-value pretty object", Marshaller{Indent: " "},
+ &pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
+ {"unknown enum value object", marshallerAllOptions,
+ &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
+ {"proto3 object with empty value", marshaller, &pb.Simple3{}, `{"dub":0}`},
+ {"map<int64, int32>", marshaller, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
+ {"map<int64, int32>", marshallerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
+ {"map<string, string>", marshaller,
+ &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
+ `{"strry":{"\"one\"":"two","three":"four"}}`},
+ {"map<int32, Object>", marshaller,
+ &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
+ {"map<int32, Object>", marshallerAllOptions,
+ &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, objjyPrettyJSON},
+ {"map<int64, string>", marshaller, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
+ `{"buggy":{"1234":"yup"}}`},
+ {"map<bool, bool>", marshaller, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
+ {"proto2 map<int64, string>", marshaller, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
+ `{"m_int64_str":{"213":"cat"}}`},
+ {"proto2 map<bool, Object>", marshaller,
+ &pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: &pb.Simple{OInt32: proto.Int32(1)}}},
+ `{"m_bool_simple":{"true":{"o_int32":1}}}`},
+}
+
+func TestMarshalling(t *testing.T) {
+ for _, tt := range marshallingTests {
+ json, err := tt.marshaller.MarshalToString(tt.pb)
+ if err != nil {
+ t.Errorf("%s: marshalling error: %v", tt.desc, err)
+ } else if tt.json != json {
+ t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
+ }
+ }
+}
+
+var unmarshallingTests = []struct {
+ desc string
+ json string
+ pb proto.Message
+}{
+ {"simple flat object", simpleObjectJSON, simpleObject},
+ {"simple pretty object", simpleObjectPrettyJSON, simpleObject},
+ {"repeated fields flat object", repeatsObjectJSON, repeatsObject},
+ {"repeated fields pretty object", repeatsObjectPrettyJSON, repeatsObject},
+ {"nested message/enum flat object", complexObjectJSON, complexObject},
+ {"nested message/enum pretty object", complexObjectPrettyJSON, complexObject},
+ {"enum-string object", `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
+ {"enum-value object", "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
+ {"unknown enum value object",
+ "{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
+ &pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
+ {"unquoted int64 object", `{"o_int64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
+ {"unquoted uint64 object", `{"o_uint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
+ {"map<int64, int32>", `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
+ {"map<string, string>", `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
+ {"map<int32, Object>", `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}},
+}
+
+func TestUnmarshalling(t *testing.T) {
+ for _, tt := range unmarshallingTests {
+ // Make a new instance of the type of our expected object.
+ p := proto.Clone(tt.pb)
+ p.Reset()
+
+ err := UnmarshalString(tt.json, p)
+ if err != nil {
+ t.Error(err)
+ continue
+ }
+
+ // For easier diffs, compare text strings of the protos.
+ exp := proto.MarshalTextString(tt.pb)
+ act := proto.MarshalTextString(p)
+ if string(exp) != string(act) {
+ t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
+ }
+ }
+}
+
+var unmarshallingShouldError = []struct {
+ desc string
+ in string
+}{
+ {"a value", "666"},
+ {"gibberish", "{adskja123;l23=-="},
+}
+
+func TestUnmarshallingBadInput(t *testing.T) {
+ for _, tt := range unmarshallingShouldError {
+ obj := &pb.Simple{}
+ err := UnmarshalString(tt.in, obj)
+ if err == nil {
+ t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
+ }
+ }
+}
diff --git a/jsonpb/jsonpb_test_proto/Makefile b/jsonpb/jsonpb_test_proto/Makefile
new file mode 100644
index 0000000..e53e6a6
--- /dev/null
+++ b/jsonpb/jsonpb_test_proto/Makefile
@@ -0,0 +1,33 @@
+# Go support for Protocol Buffers - Google's data interchange format
+#
+# Copyright 2015 The Go Authors. All rights reserved.
+# https://github.com/golang/protobuf
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+regenerate:
+ protoc --go_out=. *.proto
diff --git a/jsonpb/jsonpb_test_proto/more_test_objects.proto b/jsonpb/jsonpb_test_proto/more_test_objects.proto
new file mode 100644
index 0000000..6535791
--- /dev/null
+++ b/jsonpb/jsonpb_test_proto/more_test_objects.proto
@@ -0,0 +1,46 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2015 The Go Authors. All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package jsonpb;
+
+message Simple3 {
+ double dub = 1;
+}
+
+message Mappy {
+ map<int64, int32> nummy = 1;
+ map<string, string> strry = 2;
+ map<int32, Simple3> objjy = 3;
+ map<int64, string> buggy = 4;
+ map<bool, bool> booly = 5;
+}
diff --git a/jsonpb/jsonpb_test_proto/test_objects.proto b/jsonpb/jsonpb_test_proto/test_objects.proto
new file mode 100644
index 0000000..e48a3e8
--- /dev/null
+++ b/jsonpb/jsonpb_test_proto/test_objects.proto
@@ -0,0 +1,86 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2015 The Go Authors. All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto2";
+
+package jsonpb;
+
+// Test message for holding primitive types.
+message Simple {
+ optional bool o_bool = 1;
+ optional int32 o_int32 = 2;
+ optional int64 o_int64 = 3;
+ optional uint32 o_uint32 = 4;
+ optional uint64 o_uint64 = 5;
+ optional sint32 o_sint32 = 6;
+ optional sint64 o_sint64 = 7;
+ optional float o_float = 8;
+ optional double o_double = 9;
+ optional string o_string = 10;
+ optional bytes o_bytes = 11;
+}
+
+// Test message for holding repeated primitives.
+message Repeats {
+ repeated bool r_bool = 1;
+ repeated int32 r_int32 = 2;
+ repeated int64 r_int64 = 3;
+ repeated uint32 r_uint32 = 4;
+ repeated uint64 r_uint64 = 5;
+ repeated sint32 r_sint32 = 6;
+ repeated sint64 r_sint64 = 7;
+ repeated float r_float = 8;
+ repeated double r_double = 9;
+ repeated string r_string = 10;
+ repeated bytes r_bytes = 11;
+}
+
+// Test message for holding enums and nested messages.
+message Widget {
+ enum Color {
+ RED = 0;
+ GREEN = 1;
+ BLUE = 2;
+ };
+ optional Color color = 1;
+ repeated Color r_color = 2;
+
+ optional Simple simple = 10;
+ repeated Simple r_simple = 11;
+
+ optional Repeats repeats = 20;
+ repeated Repeats r_repeats = 21;
+}
+
+message Maps {
+ map<int64, string> m_int64_str = 1;
+ map<bool, Simple> m_bool_simple = 2;
+}