1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2015 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
34 It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.
36 This package produces a different output than the standard "encoding/json" package,
37 which does not operate correctly on protocol buffers.
54 "github.com/golang/protobuf/proto"
56 stpb "github.com/golang/protobuf/ptypes/struct"
59 const secondInNanos = int64(time.Second / time.Nanosecond)
61 // Marshaler is a configurable object for converting between
62 // protocol buffer objects and a JSON representation for them.
63 type Marshaler struct {
64 // Whether to render enum values as integers, as opposed to string values.
67 // Whether to render fields with zero values.
70 // A string to indent each level by. The presence of this field will
71 // also cause a space to appear between the field separator and
72 // value, and for newlines to be appear between fields and array
76 // Whether to use the original (.proto) name for fields.
79 // A custom URL resolver to use when marshaling Any messages to JSON.
80 // If unset, the default resolution strategy is to extract the
81 // fully-qualified type name from the type URL and pass that to
82 // proto.MessageType(string).
83 AnyResolver AnyResolver
86 // AnyResolver takes a type URL, present in an Any message, and resolves it into
87 // an instance of the associated message.
88 type AnyResolver interface {
89 Resolve(typeUrl string) (proto.Message, error)
92 func defaultResolveAny(typeUrl string) (proto.Message, error) {
93 // Only the part of typeUrl after the last slash is relevant.
95 if slash := strings.LastIndex(mname, "/"); slash >= 0 {
96 mname = mname[slash+1:]
98 mt := proto.MessageType(mname)
100 return nil, fmt.Errorf("unknown message type %q", mname)
102 return reflect.New(mt.Elem()).Interface().(proto.Message), nil
105 // JSONPBMarshaler is implemented by protobuf messages that customize the
106 // way they are marshaled to JSON. Messages that implement this should
107 // also implement JSONPBUnmarshaler so that the custom format can be
110 // The JSON marshaling must follow the proto to JSON specification:
111 // https://developers.google.com/protocol-buffers/docs/proto3#json
112 type JSONPBMarshaler interface {
113 MarshalJSONPB(*Marshaler) ([]byte, error)
116 // JSONPBUnmarshaler is implemented by protobuf messages that customize
117 // the way they are unmarshaled from JSON. Messages that implement this
118 // should also implement JSONPBMarshaler so that the custom format can be
121 // The JSON unmarshaling must follow the JSON to proto specification:
122 // https://developers.google.com/protocol-buffers/docs/proto3#json
123 type JSONPBUnmarshaler interface {
124 UnmarshalJSONPB(*Unmarshaler, []byte) error
127 // Marshal marshals a protocol buffer into JSON.
128 func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
129 v := reflect.ValueOf(pb)
130 if pb == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
131 return errors.New("Marshal called with nil")
133 // Check for unset required fields first.
134 if err := checkRequiredFields(pb); err != nil {
137 writer := &errWriter{writer: out}
138 return m.marshalObject(writer, pb, "", "")
141 // MarshalToString converts a protocol buffer object to JSON string.
142 func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
144 if err := m.Marshal(&buf, pb); err != nil {
147 return buf.String(), nil
150 type int32Slice []int32
152 var nonFinite = map[string]float64{
154 `"Infinity"`: math.Inf(1),
155 `"-Infinity"`: math.Inf(-1),
158 // For sorting extensions ids to ensure stable output.
159 func (s int32Slice) Len() int { return len(s) }
160 func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
161 func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
164 XXX_WellKnownType() string
167 // marshalObject writes a struct to the Writer.
168 func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
169 if jsm, ok := v.(JSONPBMarshaler); ok {
170 b, err := jsm.MarshalJSONPB(m)
175 // we are marshaling this object to an Any type
176 var js map[string]*json.RawMessage
177 if err = json.Unmarshal(b, &js); err != nil {
178 return fmt.Errorf("type %T produced invalid JSON: %v", v, err)
180 turl, err := json.Marshal(typeURL)
182 return fmt.Errorf("failed to marshal type URL %q to JSON: %v", typeURL, err)
184 js["@type"] = (*json.RawMessage)(&turl)
185 if b, err = json.Marshal(js); err != nil {
194 s := reflect.ValueOf(v).Elem()
196 // Handle well-known types.
197 if wkt, ok := v.(wkt); ok {
198 switch wkt.XXX_WellKnownType() {
199 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
200 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
201 // "Wrappers use the same representation in JSON
202 // as the wrapped primitive type, ..."
203 sprop := proto.GetProperties(s.Type())
204 return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
206 // Any is a bit more involved.
207 return m.marshalAny(out, v, indent)
209 // "Generated output always contains 0, 3, 6, or 9 fractional digits,
210 // depending on required precision."
211 s, ns := s.Field(0).Int(), s.Field(1).Int()
212 if ns <= -secondInNanos || ns >= secondInNanos {
213 return fmt.Errorf("ns out of range (%v, %v)", -secondInNanos, secondInNanos)
215 if (s > 0 && ns < 0) || (s < 0 && ns > 0) {
216 return errors.New("signs of seconds and nanos do not match")
221 x := fmt.Sprintf("%d.%09d", s, ns)
222 x = strings.TrimSuffix(x, "000")
223 x = strings.TrimSuffix(x, "000")
224 x = strings.TrimSuffix(x, ".000")
229 case "Struct", "ListValue":
230 // Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice.
231 // TODO: pass the correct Properties if needed.
232 return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
234 // "RFC 3339, where generated output will always be Z-normalized
235 // and uses 0, 3, 6 or 9 fractional digits."
236 s, ns := s.Field(0).Int(), s.Field(1).Int()
237 if ns < 0 || ns >= secondInNanos {
238 return fmt.Errorf("ns out of range [0, %v)", secondInNanos)
240 t := time.Unix(s, ns).UTC()
241 // time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
242 x := t.Format("2006-01-02T15:04:05.000000000")
243 x = strings.TrimSuffix(x, "000")
244 x = strings.TrimSuffix(x, "000")
245 x = strings.TrimSuffix(x, ".000")
251 // Value has a single oneof.
254 // "absence of any variant indicates an error"
255 return errors.New("nil Value")
257 // oneof -> *T -> T -> T.F
258 x := kind.Elem().Elem().Field(0)
259 // TODO: pass the correct Properties if needed.
260 return m.marshalValue(out, &proto.Properties{}, x, indent)
272 if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
278 for i := 0; i < s.NumField(); i++ {
280 valueField := s.Type().Field(i)
281 if strings.HasPrefix(valueField.Name, "XXX_") {
285 // IsNil will panic on most value kinds.
286 switch value.Kind() {
287 case reflect.Chan, reflect.Func, reflect.Interface:
294 switch value.Kind() {
299 case reflect.Int32, reflect.Int64:
300 if value.Int() == 0 {
303 case reflect.Uint32, reflect.Uint64:
304 if value.Uint() == 0 {
307 case reflect.Float32, reflect.Float64:
308 if value.Float() == 0 {
312 if value.Len() == 0 {
315 case reflect.Map, reflect.Ptr, reflect.Slice:
322 // Oneof fields need special handling.
323 if valueField.Tag.Get("protobuf_oneof") != "" {
324 // value is an interface containing &T{real_value}.
325 sv := value.Elem().Elem() // interface -> *T -> T
327 valueField = sv.Type().Field(0)
329 prop := jsonProperties(valueField, m.OrigName)
333 if err := m.marshalField(out, prop, value, indent); err != nil {
339 // Handle proto2 extensions.
340 if ep, ok := v.(proto.Message); ok {
341 extensions := proto.RegisteredExtensions(v)
342 // Sort extensions for stable output.
343 ids := make([]int32, 0, len(extensions))
344 for id, desc := range extensions {
345 if !proto.HasExtension(ep, desc) {
348 ids = append(ids, id)
350 sort.Sort(int32Slice(ids))
351 for _, id := range ids {
352 desc := extensions[id]
357 ext, extErr := proto.GetExtension(ep, desc)
361 value := reflect.ValueOf(ext)
362 var prop proto.Properties
364 prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
368 if err := m.marshalField(out, &prop, value, indent); err != nil {
384 func (m *Marshaler) writeSep(out *errWriter) {
392 func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
393 // "If the Any contains a value that has a special JSON mapping,
394 // it will be converted as follows: {"@type": xxx, "value": yyy}.
395 // Otherwise, the value will be converted into a JSON object,
396 // and the "@type" field will be inserted to indicate the actual data type."
397 v := reflect.ValueOf(any).Elem()
398 turl := v.Field(0).String()
399 val := v.Field(1).Bytes()
401 var msg proto.Message
403 if m.AnyResolver != nil {
404 msg, err = m.AnyResolver.Resolve(turl)
406 msg, err = defaultResolveAny(turl)
412 if err := proto.Unmarshal(val, msg); err != nil {
416 if _, ok := msg.(wkt); ok {
421 if err := m.marshalTypeURL(out, indent, turl); err != nil {
428 out.write(`"value": `)
430 out.write(`"value":`)
432 if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
443 return m.marshalObject(out, msg, indent, turl)
446 func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
451 out.write(`"@type":`)
455 b, err := json.Marshal(typeURL)
463 // marshalField writes field description and value to the Writer.
464 func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
470 out.write(prop.JSONName)
475 if err := m.marshalValue(out, prop, v, indent); err != nil {
481 // marshalValue writes the value to the Writer.
482 func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
484 v = reflect.Indirect(v)
486 // Handle nil pointer
487 if v.Kind() == reflect.Invalid {
492 // Handle repeated elements.
493 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
496 for i := 0; i < v.Len(); i++ {
497 sliceVal := v.Index(i)
505 if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
519 // Handle well-known types.
520 // Most are handled up in marshalObject (because 99% are messages).
521 if wkt, ok := v.Interface().(wkt); ok {
522 switch wkt.XXX_WellKnownType() {
529 // Handle enumerations.
530 if !m.EnumsAsInts && prop.Enum != "" {
531 // Unknown enum values will are stringified by the proto library as their
532 // value. Such values should _not_ be quoted or they will be interpreted
533 // as an enum string instead of their value.
534 enumStr := v.Interface().(fmt.Stringer).String()
536 if v.Kind() == reflect.Ptr {
537 valStr = strconv.Itoa(int(v.Elem().Int()))
539 valStr = strconv.Itoa(int(v.Int()))
541 isKnownEnum := enumStr != valStr
552 // Handle nested messages.
553 if v.Kind() == reflect.Struct {
554 return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent, "")
558 // Since Go randomizes map iteration, we sort keys for stable output.
559 if v.Kind() == reflect.Map {
562 sort.Sort(mapKeys(keys))
563 for i, k := range keys {
574 // TODO handle map key prop properly
575 b, err := json.Marshal(k.Interface())
581 // If the JSON is not a string value, encode it again to make it one.
582 if !strings.HasPrefix(s, `"`) {
583 b, err := json.Marshal(s)
597 if prop != nil && prop.MapValProp != nil {
598 vprop = prop.MapValProp
600 if err := m.marshalValue(out, vprop, v.MapIndex(k), indent+m.Indent); err != nil {
613 // Handle non-finite floats, e.g. NaN, Infinity and -Infinity.
614 if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
618 case math.IsInf(f, 1):
620 case math.IsInf(f, -1):
631 // Default handling defers to the encoding/json library.
632 b, err := json.Marshal(v.Interface())
636 needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
647 // Unmarshaler is a configurable object for converting from a JSON
648 // representation to a protocol buffer object.
649 type Unmarshaler struct {
650 // Whether to allow messages to contain unknown fields, as opposed to
651 // failing to unmarshal.
652 AllowUnknownFields bool
654 // A custom URL resolver to use when unmarshaling Any messages from JSON.
655 // If unset, the default resolution strategy is to extract the
656 // fully-qualified type name from the type URL and pass that to
657 // proto.MessageType(string).
658 AnyResolver AnyResolver
661 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
662 // This function is lenient and will decode any options permutations of the
663 // related Marshaler.
664 func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
665 inputValue := json.RawMessage{}
666 if err := dec.Decode(&inputValue); err != nil {
669 if err := u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil); err != nil {
672 return checkRequiredFields(pb)
675 // Unmarshal unmarshals a JSON object stream into a protocol
676 // buffer. This function is lenient and will decode any options
677 // permutations of the related Marshaler.
678 func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error {
679 dec := json.NewDecoder(r)
680 return u.UnmarshalNext(dec, pb)
683 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
684 // This function is lenient and will decode any options permutations of the
685 // related Marshaler.
686 func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
687 return new(Unmarshaler).UnmarshalNext(dec, pb)
690 // Unmarshal unmarshals a JSON object stream into a protocol
691 // buffer. This function is lenient and will decode any options
692 // permutations of the related Marshaler.
693 func Unmarshal(r io.Reader, pb proto.Message) error {
694 return new(Unmarshaler).Unmarshal(r, pb)
697 // UnmarshalString will populate the fields of a protocol buffer based
698 // on a JSON string. This function is lenient and will decode any options
699 // permutations of the related Marshaler.
700 func UnmarshalString(str string, pb proto.Message) error {
701 return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb)
704 // unmarshalValue converts/copies a value into the target.
706 func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
707 targetType := target.Type()
709 // Allocate memory for pointer fields.
710 if targetType.Kind() == reflect.Ptr {
711 // If input value is "null" and target is a pointer type, then the field should be treated as not set
712 // UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue.
713 _, isJSONPBUnmarshaler := target.Interface().(JSONPBUnmarshaler)
714 if string(inputValue) == "null" && targetType != reflect.TypeOf(&stpb.Value{}) && !isJSONPBUnmarshaler {
717 target.Set(reflect.New(targetType.Elem()))
719 return u.unmarshalValue(target.Elem(), inputValue, prop)
722 if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok {
723 return jsu.UnmarshalJSONPB(u, []byte(inputValue))
726 // Handle well-known types that are not pointers.
727 if w, ok := target.Addr().Interface().(wkt); ok {
728 switch w.XXX_WellKnownType() {
729 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
730 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
731 return u.unmarshalValue(target.Field(0), inputValue, prop)
733 // Use json.RawMessage pointer type instead of value to support pre-1.8 version.
734 // 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
735 // https://github.com/golang/go/issues/14493
736 var jsonFields map[string]*json.RawMessage
737 if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
741 val, ok := jsonFields["@type"]
742 if !ok || val == nil {
743 return errors.New("Any JSON doesn't have '@type'")
747 if err := json.Unmarshal([]byte(*val), &turl); err != nil {
748 return fmt.Errorf("can't unmarshal Any's '@type': %q", *val)
750 target.Field(0).SetString(turl)
754 if u.AnyResolver != nil {
755 m, err = u.AnyResolver.Resolve(turl)
757 m, err = defaultResolveAny(turl)
763 if _, ok := m.(wkt); ok {
764 val, ok := jsonFields["value"]
766 return errors.New("Any JSON doesn't have 'value'")
769 if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil {
770 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
773 delete(jsonFields, "@type")
774 nestedProto, err := json.Marshal(jsonFields)
776 return fmt.Errorf("can't generate JSON for Any's nested proto to be unmarshaled: %v", err)
779 if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), nestedProto, nil); err != nil {
780 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
784 b, err := proto.Marshal(m)
786 return fmt.Errorf("can't marshal proto %T into Any.Value: %v", m, err)
788 target.Field(1).SetBytes(b)
792 unq, err := unquote(string(inputValue))
797 d, err := time.ParseDuration(unq)
799 return fmt.Errorf("bad Duration: %v", err)
802 ns := d.Nanoseconds()
805 target.Field(0).SetInt(s)
806 target.Field(1).SetInt(ns)
809 unq, err := unquote(string(inputValue))
814 t, err := time.Parse(time.RFC3339Nano, unq)
816 return fmt.Errorf("bad Timestamp: %v", err)
819 target.Field(0).SetInt(t.Unix())
820 target.Field(1).SetInt(int64(t.Nanosecond()))
823 var m map[string]json.RawMessage
824 if err := json.Unmarshal(inputValue, &m); err != nil {
825 return fmt.Errorf("bad StructValue: %v", err)
828 target.Field(0).Set(reflect.ValueOf(map[string]*stpb.Value{}))
829 for k, jv := range m {
831 if err := u.unmarshalValue(reflect.ValueOf(pv).Elem(), jv, prop); err != nil {
832 return fmt.Errorf("bad value in StructValue for key %q: %v", k, err)
834 target.Field(0).SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(pv))
838 var s []json.RawMessage
839 if err := json.Unmarshal(inputValue, &s); err != nil {
840 return fmt.Errorf("bad ListValue: %v", err)
843 target.Field(0).Set(reflect.ValueOf(make([]*stpb.Value, len(s))))
844 for i, sv := range s {
845 if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil {
851 ivStr := string(inputValue)
853 target.Field(0).Set(reflect.ValueOf(&stpb.Value_NullValue{}))
854 } else if v, err := strconv.ParseFloat(ivStr, 0); err == nil {
855 target.Field(0).Set(reflect.ValueOf(&stpb.Value_NumberValue{v}))
856 } else if v, err := unquote(ivStr); err == nil {
857 target.Field(0).Set(reflect.ValueOf(&stpb.Value_StringValue{v}))
858 } else if v, err := strconv.ParseBool(ivStr); err == nil {
859 target.Field(0).Set(reflect.ValueOf(&stpb.Value_BoolValue{v}))
860 } else if err := json.Unmarshal(inputValue, &[]json.RawMessage{}); err == nil {
861 lv := &stpb.ListValue{}
862 target.Field(0).Set(reflect.ValueOf(&stpb.Value_ListValue{lv}))
863 return u.unmarshalValue(reflect.ValueOf(lv).Elem(), inputValue, prop)
864 } else if err := json.Unmarshal(inputValue, &map[string]json.RawMessage{}); err == nil {
866 target.Field(0).Set(reflect.ValueOf(&stpb.Value_StructValue{sv}))
867 return u.unmarshalValue(reflect.ValueOf(sv).Elem(), inputValue, prop)
869 return fmt.Errorf("unrecognized type for Value %q", ivStr)
875 // Handle enums, which have an underlying type of int32,
876 // and may appear as strings.
877 // The case of an enum appearing as a number is handled
878 // at the bottom of this function.
879 if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
880 vmap := proto.EnumValueMap(prop.Enum)
881 // Don't need to do unquoting; valid enum names
882 // are from a limited character set.
883 s := inputValue[1 : len(inputValue)-1]
884 n, ok := vmap[string(s)]
886 return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
888 if target.Kind() == reflect.Ptr { // proto2
889 target.Set(reflect.New(targetType.Elem()))
890 target = target.Elem()
892 if targetType.Kind() != reflect.Int32 {
893 return fmt.Errorf("invalid target %q for enum %s", targetType.Kind(), prop.Enum)
895 target.SetInt(int64(n))
899 // Handle nested messages.
900 if targetType.Kind() == reflect.Struct {
901 var jsonFields map[string]json.RawMessage
902 if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
906 consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
907 // Be liberal in what names we accept; both orig_name and camelName are okay.
908 fieldNames := acceptedJSONFieldNames(prop)
910 vOrig, okOrig := jsonFields[fieldNames.orig]
911 vCamel, okCamel := jsonFields[fieldNames.camel]
912 if !okOrig && !okCamel {
915 // If, for some reason, both are present in the data, favour the camelName.
916 var raw json.RawMessage
919 delete(jsonFields, fieldNames.orig)
923 delete(jsonFields, fieldNames.camel)
928 sprops := proto.GetProperties(targetType)
929 for i := 0; i < target.NumField(); i++ {
930 ft := target.Type().Field(i)
931 if strings.HasPrefix(ft.Name, "XXX_") {
935 valueForField, ok := consumeField(sprops.Prop[i])
940 if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
944 // Check for any oneof fields.
945 if len(jsonFields) > 0 {
946 for _, oop := range sprops.OneofTypes {
947 raw, ok := consumeField(oop.Prop)
951 nv := reflect.New(oop.Type.Elem())
952 target.Field(oop.Field).Set(nv)
953 if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
958 // Handle proto2 extensions.
959 if len(jsonFields) > 0 {
960 if ep, ok := target.Addr().Interface().(proto.Message); ok {
961 for _, ext := range proto.RegisteredExtensions(ep) {
962 name := fmt.Sprintf("[%s]", ext.Name)
963 raw, ok := jsonFields[name]
967 delete(jsonFields, name)
968 nv := reflect.New(reflect.TypeOf(ext.ExtensionType).Elem())
969 if err := u.unmarshalValue(nv.Elem(), raw, nil); err != nil {
972 if err := proto.SetExtension(ep, ext, nv.Interface()); err != nil {
978 if !u.AllowUnknownFields && len(jsonFields) > 0 {
979 // Pick any field to be the scapegoat.
981 for fname := range jsonFields {
985 return fmt.Errorf("unknown field %q in %v", f, targetType)
990 // Handle arrays (which aren't encoded bytes)
991 if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
992 var slc []json.RawMessage
993 if err := json.Unmarshal(inputValue, &slc); err != nil {
998 target.Set(reflect.MakeSlice(targetType, l, l))
999 for i := 0; i < l; i++ {
1000 if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
1008 // Handle maps (whose keys are always strings)
1009 if targetType.Kind() == reflect.Map {
1010 var mp map[string]json.RawMessage
1011 if err := json.Unmarshal(inputValue, &mp); err != nil {
1015 target.Set(reflect.MakeMap(targetType))
1016 for ks, raw := range mp {
1017 // Unmarshal map key. The core json library already decoded the key into a
1018 // string, so we handle that specially. Other types were quoted post-serialization.
1020 if targetType.Key().Kind() == reflect.String {
1021 k = reflect.ValueOf(ks)
1023 k = reflect.New(targetType.Key()).Elem()
1024 var kprop *proto.Properties
1025 if prop != nil && prop.MapKeyProp != nil {
1026 kprop = prop.MapKeyProp
1028 if err := u.unmarshalValue(k, json.RawMessage(ks), kprop); err != nil {
1033 // Unmarshal map value.
1034 v := reflect.New(targetType.Elem()).Elem()
1035 var vprop *proto.Properties
1036 if prop != nil && prop.MapValProp != nil {
1037 vprop = prop.MapValProp
1039 if err := u.unmarshalValue(v, raw, vprop); err != nil {
1042 target.SetMapIndex(k, v)
1048 // Non-finite numbers can be encoded as strings.
1049 isFloat := targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
1051 if num, ok := nonFinite[string(inputValue)]; ok {
1052 target.SetFloat(num)
1057 // integers & floats can be encoded as strings. In this case we drop
1058 // the quotes and proceed as normal.
1059 isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64 ||
1060 targetType.Kind() == reflect.Int32 || targetType.Kind() == reflect.Uint32 ||
1061 targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
1062 if isNum && strings.HasPrefix(string(inputValue), `"`) {
1063 inputValue = inputValue[1 : len(inputValue)-1]
1066 // Use the encoding/json for parsing other value types.
1067 return json.Unmarshal(inputValue, target.Addr().Interface())
1070 func unquote(s string) (string, error) {
1072 err := json.Unmarshal([]byte(s), &ret)
1076 // jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
1077 func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
1078 var prop proto.Properties
1079 prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
1080 if origName || prop.JSONName == "" {
1081 prop.JSONName = prop.OrigName
1086 type fieldNames struct {
1090 func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
1091 opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
1092 if prop.JSONName != "" {
1093 opts.camel = prop.JSONName
1098 // Writer wrapper inspired by https://blog.golang.org/errors-are-values
1099 type errWriter struct {
1104 func (w *errWriter) write(str string) {
1108 _, w.err = w.writer.Write([]byte(str))
1111 // Map fields may have key types of non-float scalars, strings and enums.
1112 // The easiest way to sort them in some deterministic order is to use fmt.
1113 // If this turns out to be inefficient we can always consider other options,
1114 // such as doing a Schwartzian transform.
1116 // Numeric keys are sorted in numeric order per
1117 // https://developers.google.com/protocol-buffers/docs/proto#maps.
1118 type mapKeys []reflect.Value
1120 func (s mapKeys) Len() int { return len(s) }
1121 func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
1122 func (s mapKeys) Less(i, j int) bool {
1123 if k := s[i].Kind(); k == s[j].Kind() {
1125 case reflect.String:
1126 return s[i].String() < s[j].String()
1127 case reflect.Int32, reflect.Int64:
1128 return s[i].Int() < s[j].Int()
1129 case reflect.Uint32, reflect.Uint64:
1130 return s[i].Uint() < s[j].Uint()
1133 return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
1136 // checkRequiredFields returns an error if any required field in the given proto message is not set.
1137 // This function is used by both Marshal and Unmarshal. While required fields only exist in a
1138 // proto2 message, a proto3 message can contain proto2 message(s).
1139 func checkRequiredFields(pb proto.Message) error {
1140 // Most well-known type messages do not contain required fields. The "Any" type may contain
1141 // a message that has required fields.
1143 // When an Any message is being marshaled, the code will invoked proto.Unmarshal on Any.Value
1144 // field in order to transform that into JSON, and that should have returned an error if a
1145 // required field is not set in the embedded message.
1147 // When an Any message is being unmarshaled, the code will have invoked proto.Marshal on the
1148 // embedded message to store the serialized message in Any.Value field, and that should have
1149 // returned an error if a required field is not set.
1150 if _, ok := pb.(wkt); ok {
1154 v := reflect.ValueOf(pb)
1155 // Skip message if it is not a struct pointer.
1156 if v.Kind() != reflect.Ptr {
1160 if v.Kind() != reflect.Struct {
1164 for i := 0; i < v.NumField(); i++ {
1166 sfield := v.Type().Field(i)
1168 if sfield.PkgPath != "" {
1169 // blank PkgPath means the field is exported; skip if not exported
1173 if strings.HasPrefix(sfield.Name, "XXX_") {
1177 // Oneof field is an interface implemented by wrapper structs containing the actual oneof
1178 // field, i.e. an interface containing &T{real_value}.
1179 if sfield.Tag.Get("protobuf_oneof") != "" {
1180 if field.Kind() != reflect.Interface {
1184 if v.Kind() != reflect.Ptr || v.IsNil() {
1188 if v.Kind() != reflect.Struct || v.NumField() < 1 {
1192 sfield = v.Type().Field(0)
1195 protoTag := sfield.Tag.Get("protobuf")
1199 var prop proto.Properties
1200 prop.Init(sfield.Type, sfield.Name, protoTag, &sfield)
1202 switch field.Kind() {
1207 // Check each map value.
1208 keys := field.MapKeys()
1209 for _, k := range keys {
1210 v := field.MapIndex(k)
1211 if err := checkRequiredFieldsInValue(v); err != nil {
1216 // Handle non-repeated type, e.g. bytes.
1218 if prop.Required && field.IsNil() {
1219 return fmt.Errorf("required field %q is not set", prop.Name)
1224 // Handle repeated type.
1228 // Check each slice item.
1229 for i := 0; i < field.Len(); i++ {
1231 if err := checkRequiredFieldsInValue(v); err != nil {
1238 return fmt.Errorf("required field %q is not set", prop.Name)
1242 if err := checkRequiredFieldsInValue(field); err != nil {
1248 // Handle proto2 extensions.
1249 for _, ext := range proto.RegisteredExtensions(pb) {
1250 if !proto.HasExtension(pb, ext) {
1253 ep, err := proto.GetExtension(pb, ext)
1257 err = checkRequiredFieldsInValue(reflect.ValueOf(ep))
1266 func checkRequiredFieldsInValue(v reflect.Value) error {
1267 if pm, ok := v.Interface().(proto.Message); ok {
1268 return checkRequiredFields(pm)