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 // Marshaler is a configurable object for converting between
60 // protocol buffer objects and a JSON representation for them.
61 type Marshaler struct {
62 // Whether to render enum values as integers, as opposed to string values.
65 // Whether to render fields with zero values.
68 // A string to indent each level by. The presence of this field will
69 // also cause a space to appear between the field separator and
70 // value, and for newlines to be appear between fields and array
74 // Whether to use the original (.proto) name for fields.
77 // A custom URL resolver to use when marshaling Any messages to JSON.
78 // If unset, the default resolution strategy is to extract the
79 // fully-qualified type name from the type URL and pass that to
80 // proto.MessageType(string).
81 AnyResolver AnyResolver
84 // AnyResolver takes a type URL, present in an Any message, and resolves it into
85 // an instance of the associated message.
86 type AnyResolver interface {
87 Resolve(typeUrl string) (proto.Message, error)
90 func defaultResolveAny(typeUrl string) (proto.Message, error) {
91 // Only the part of typeUrl after the last slash is relevant.
93 if slash := strings.LastIndex(mname, "/"); slash >= 0 {
94 mname = mname[slash+1:]
96 mt := proto.MessageType(mname)
98 return nil, fmt.Errorf("unknown message type %q", mname)
100 return reflect.New(mt.Elem()).Interface().(proto.Message), nil
103 // JSONPBMarshaler is implemented by protobuf messages that customize the
104 // way they are marshaled to JSON. Messages that implement this should
105 // also implement JSONPBUnmarshaler so that the custom format can be
107 type JSONPBMarshaler interface {
108 MarshalJSONPB(*Marshaler) ([]byte, error)
111 // JSONPBUnmarshaler is implemented by protobuf messages that customize
112 // the way they are unmarshaled from JSON. Messages that implement this
113 // should also implement JSONPBMarshaler so that the custom format can be
115 type JSONPBUnmarshaler interface {
116 UnmarshalJSONPB(*Unmarshaler, []byte) error
119 // Marshal marshals a protocol buffer into JSON.
120 func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
121 writer := &errWriter{writer: out}
122 return m.marshalObject(writer, pb, "", "")
125 // MarshalToString converts a protocol buffer object to JSON string.
126 func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
128 if err := m.Marshal(&buf, pb); err != nil {
131 return buf.String(), nil
134 type int32Slice []int32
136 var nonFinite = map[string]float64{
138 `"Infinity"`: math.Inf(1),
139 `"-Infinity"`: math.Inf(-1),
142 // For sorting extensions ids to ensure stable output.
143 func (s int32Slice) Len() int { return len(s) }
144 func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
145 func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
148 XXX_WellKnownType() string
151 // marshalObject writes a struct to the Writer.
152 func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
153 if jsm, ok := v.(JSONPBMarshaler); ok {
154 b, err := jsm.MarshalJSONPB(m)
159 // we are marshaling this object to an Any type
160 var js map[string]*json.RawMessage
161 if err = json.Unmarshal(b, &js); err != nil {
162 return fmt.Errorf("type %T produced invalid JSON: %v", v, err)
164 turl, err := json.Marshal(typeURL)
166 return fmt.Errorf("failed to marshal type URL %q to JSON: %v", typeURL, err)
168 js["@type"] = (*json.RawMessage)(&turl)
169 if b, err = json.Marshal(js); err != nil {
178 s := reflect.ValueOf(v).Elem()
180 // Handle well-known types.
181 if wkt, ok := v.(wkt); ok {
182 switch wkt.XXX_WellKnownType() {
183 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
184 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
185 // "Wrappers use the same representation in JSON
186 // as the wrapped primitive type, ..."
187 sprop := proto.GetProperties(s.Type())
188 return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
190 // Any is a bit more involved.
191 return m.marshalAny(out, v, indent)
193 // "Generated output always contains 3, 6, or 9 fractional digits,
194 // depending on required precision."
195 s, ns := s.Field(0).Int(), s.Field(1).Int()
196 d := time.Duration(s)*time.Second + time.Duration(ns)*time.Nanosecond
197 x := fmt.Sprintf("%.9f", d.Seconds())
198 x = strings.TrimSuffix(x, "000")
199 x = strings.TrimSuffix(x, "000")
204 case "Struct", "ListValue":
205 // Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice.
206 // TODO: pass the correct Properties if needed.
207 return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
209 // "RFC 3339, where generated output will always be Z-normalized
210 // and uses 3, 6 or 9 fractional digits."
211 s, ns := s.Field(0).Int(), s.Field(1).Int()
212 t := time.Unix(s, ns).UTC()
213 // time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
214 x := t.Format("2006-01-02T15:04:05.000000000")
215 x = strings.TrimSuffix(x, "000")
216 x = strings.TrimSuffix(x, "000")
222 // Value has a single oneof.
225 // "absence of any variant indicates an error"
226 return errors.New("nil Value")
228 // oneof -> *T -> T -> T.F
229 x := kind.Elem().Elem().Field(0)
230 // TODO: pass the correct Properties if needed.
231 return m.marshalValue(out, &proto.Properties{}, x, indent)
243 if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
249 for i := 0; i < s.NumField(); i++ {
251 valueField := s.Type().Field(i)
252 if strings.HasPrefix(valueField.Name, "XXX_") {
256 // IsNil will panic on most value kinds.
257 switch value.Kind() {
258 case reflect.Chan, reflect.Func, reflect.Interface:
265 switch value.Kind() {
270 case reflect.Int32, reflect.Int64:
271 if value.Int() == 0 {
274 case reflect.Uint32, reflect.Uint64:
275 if value.Uint() == 0 {
278 case reflect.Float32, reflect.Float64:
279 if value.Float() == 0 {
283 if value.Len() == 0 {
286 case reflect.Map, reflect.Ptr, reflect.Slice:
293 // Oneof fields need special handling.
294 if valueField.Tag.Get("protobuf_oneof") != "" {
295 // value is an interface containing &T{real_value}.
296 sv := value.Elem().Elem() // interface -> *T -> T
298 valueField = sv.Type().Field(0)
300 prop := jsonProperties(valueField, m.OrigName)
304 if err := m.marshalField(out, prop, value, indent); err != nil {
310 // Handle proto2 extensions.
311 if ep, ok := v.(proto.Message); ok {
312 extensions := proto.RegisteredExtensions(v)
313 // Sort extensions for stable output.
314 ids := make([]int32, 0, len(extensions))
315 for id, desc := range extensions {
316 if !proto.HasExtension(ep, desc) {
319 ids = append(ids, id)
321 sort.Sort(int32Slice(ids))
322 for _, id := range ids {
323 desc := extensions[id]
328 ext, extErr := proto.GetExtension(ep, desc)
332 value := reflect.ValueOf(ext)
333 var prop proto.Properties
335 prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
339 if err := m.marshalField(out, &prop, value, indent); err != nil {
355 func (m *Marshaler) writeSep(out *errWriter) {
363 func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
364 // "If the Any contains a value that has a special JSON mapping,
365 // it will be converted as follows: {"@type": xxx, "value": yyy}.
366 // Otherwise, the value will be converted into a JSON object,
367 // and the "@type" field will be inserted to indicate the actual data type."
368 v := reflect.ValueOf(any).Elem()
369 turl := v.Field(0).String()
370 val := v.Field(1).Bytes()
372 var msg proto.Message
374 if m.AnyResolver != nil {
375 msg, err = m.AnyResolver.Resolve(turl)
377 msg, err = defaultResolveAny(turl)
383 if err := proto.Unmarshal(val, msg); err != nil {
387 if _, ok := msg.(wkt); ok {
392 if err := m.marshalTypeURL(out, indent, turl); err != nil {
399 out.write(`"value": `)
401 out.write(`"value":`)
403 if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
414 return m.marshalObject(out, msg, indent, turl)
417 func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
422 out.write(`"@type":`)
426 b, err := json.Marshal(typeURL)
434 // marshalField writes field description and value to the Writer.
435 func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
441 out.write(prop.JSONName)
446 if err := m.marshalValue(out, prop, v, indent); err != nil {
452 // marshalValue writes the value to the Writer.
453 func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
455 v = reflect.Indirect(v)
457 // Handle nil pointer
458 if v.Kind() == reflect.Invalid {
463 // Handle repeated elements.
464 if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
467 for i := 0; i < v.Len(); i++ {
468 sliceVal := v.Index(i)
476 if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
490 // Handle well-known types.
491 // Most are handled up in marshalObject (because 99% are messages).
492 if wkt, ok := v.Interface().(wkt); ok {
493 switch wkt.XXX_WellKnownType() {
500 // Handle enumerations.
501 if !m.EnumsAsInts && prop.Enum != "" {
502 // Unknown enum values will are stringified by the proto library as their
503 // value. Such values should _not_ be quoted or they will be interpreted
504 // as an enum string instead of their value.
505 enumStr := v.Interface().(fmt.Stringer).String()
507 if v.Kind() == reflect.Ptr {
508 valStr = strconv.Itoa(int(v.Elem().Int()))
510 valStr = strconv.Itoa(int(v.Int()))
512 isKnownEnum := enumStr != valStr
523 // Handle nested messages.
524 if v.Kind() == reflect.Struct {
525 return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent, "")
529 // Since Go randomizes map iteration, we sort keys for stable output.
530 if v.Kind() == reflect.Map {
533 sort.Sort(mapKeys(keys))
534 for i, k := range keys {
545 b, err := json.Marshal(k.Interface())
551 // If the JSON is not a string value, encode it again to make it one.
552 if !strings.HasPrefix(s, `"`) {
553 b, err := json.Marshal(s)
566 if err := m.marshalValue(out, prop, v.MapIndex(k), indent+m.Indent); err != nil {
579 // Handle non-finite floats, e.g. NaN, Infinity and -Infinity.
580 if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
584 case math.IsInf(f, 1):
586 case math.IsInf(f, -1):
597 // Default handling defers to the encoding/json library.
598 b, err := json.Marshal(v.Interface())
602 needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
613 // Unmarshaler is a configurable object for converting from a JSON
614 // representation to a protocol buffer object.
615 type Unmarshaler struct {
616 // Whether to allow messages to contain unknown fields, as opposed to
617 // failing to unmarshal.
618 AllowUnknownFields bool
620 // A custom URL resolver to use when unmarshaling Any messages from JSON.
621 // If unset, the default resolution strategy is to extract the
622 // fully-qualified type name from the type URL and pass that to
623 // proto.MessageType(string).
624 AnyResolver AnyResolver
627 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
628 // This function is lenient and will decode any options permutations of the
629 // related Marshaler.
630 func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
631 inputValue := json.RawMessage{}
632 if err := dec.Decode(&inputValue); err != nil {
635 return u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil)
638 // Unmarshal unmarshals a JSON object stream into a protocol
639 // buffer. This function is lenient and will decode any options
640 // permutations of the related Marshaler.
641 func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error {
642 dec := json.NewDecoder(r)
643 return u.UnmarshalNext(dec, pb)
646 // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
647 // This function is lenient and will decode any options permutations of the
648 // related Marshaler.
649 func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
650 return new(Unmarshaler).UnmarshalNext(dec, pb)
653 // Unmarshal unmarshals a JSON object stream into a protocol
654 // buffer. This function is lenient and will decode any options
655 // permutations of the related Marshaler.
656 func Unmarshal(r io.Reader, pb proto.Message) error {
657 return new(Unmarshaler).Unmarshal(r, pb)
660 // UnmarshalString will populate the fields of a protocol buffer based
661 // on a JSON string. This function is lenient and will decode any options
662 // permutations of the related Marshaler.
663 func UnmarshalString(str string, pb proto.Message) error {
664 return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb)
667 // unmarshalValue converts/copies a value into the target.
669 func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
670 targetType := target.Type()
672 // Allocate memory for pointer fields.
673 if targetType.Kind() == reflect.Ptr {
674 // If input value is "null" and target is a pointer type, then the field should be treated as not set
675 // UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue.
676 _, isJSONPBUnmarshaler := target.Interface().(JSONPBUnmarshaler)
677 if string(inputValue) == "null" && targetType != reflect.TypeOf(&stpb.Value{}) && !isJSONPBUnmarshaler {
680 target.Set(reflect.New(targetType.Elem()))
682 return u.unmarshalValue(target.Elem(), inputValue, prop)
685 if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok {
686 return jsu.UnmarshalJSONPB(u, []byte(inputValue))
689 // Handle well-known types that are not pointers.
690 if w, ok := target.Addr().Interface().(wkt); ok {
691 switch w.XXX_WellKnownType() {
692 case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
693 "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
694 return u.unmarshalValue(target.Field(0), inputValue, prop)
696 // Use json.RawMessage pointer type instead of value to support pre-1.8 version.
697 // 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
698 // https://github.com/golang/go/issues/14493
699 var jsonFields map[string]*json.RawMessage
700 if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
704 val, ok := jsonFields["@type"]
705 if !ok || val == nil {
706 return errors.New("Any JSON doesn't have '@type'")
710 if err := json.Unmarshal([]byte(*val), &turl); err != nil {
711 return fmt.Errorf("can't unmarshal Any's '@type': %q", *val)
713 target.Field(0).SetString(turl)
717 if u.AnyResolver != nil {
718 m, err = u.AnyResolver.Resolve(turl)
720 m, err = defaultResolveAny(turl)
726 if _, ok := m.(wkt); ok {
727 val, ok := jsonFields["value"]
729 return errors.New("Any JSON doesn't have 'value'")
732 if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil {
733 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
736 delete(jsonFields, "@type")
737 nestedProto, err := json.Marshal(jsonFields)
739 return fmt.Errorf("can't generate JSON for Any's nested proto to be unmarshaled: %v", err)
742 if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), nestedProto, nil); err != nil {
743 return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
747 b, err := proto.Marshal(m)
749 return fmt.Errorf("can't marshal proto %T into Any.Value: %v", m, err)
751 target.Field(1).SetBytes(b)
755 unq, err := strconv.Unquote(string(inputValue))
760 d, err := time.ParseDuration(unq)
762 return fmt.Errorf("bad Duration: %v", err)
765 ns := d.Nanoseconds()
768 target.Field(0).SetInt(s)
769 target.Field(1).SetInt(ns)
772 unq, err := strconv.Unquote(string(inputValue))
777 t, err := time.Parse(time.RFC3339Nano, unq)
779 return fmt.Errorf("bad Timestamp: %v", err)
782 target.Field(0).SetInt(t.Unix())
783 target.Field(1).SetInt(int64(t.Nanosecond()))
786 var m map[string]json.RawMessage
787 if err := json.Unmarshal(inputValue, &m); err != nil {
788 return fmt.Errorf("bad StructValue: %v", err)
791 target.Field(0).Set(reflect.ValueOf(map[string]*stpb.Value{}))
792 for k, jv := range m {
794 if err := u.unmarshalValue(reflect.ValueOf(pv).Elem(), jv, prop); err != nil {
795 return fmt.Errorf("bad value in StructValue for key %q: %v", k, err)
797 target.Field(0).SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(pv))
801 var s []json.RawMessage
802 if err := json.Unmarshal(inputValue, &s); err != nil {
803 return fmt.Errorf("bad ListValue: %v", err)
806 target.Field(0).Set(reflect.ValueOf(make([]*stpb.Value, len(s), len(s))))
807 for i, sv := range s {
808 if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil {
814 ivStr := string(inputValue)
816 target.Field(0).Set(reflect.ValueOf(&stpb.Value_NullValue{}))
817 } else if v, err := strconv.ParseFloat(ivStr, 0); err == nil {
818 target.Field(0).Set(reflect.ValueOf(&stpb.Value_NumberValue{v}))
819 } else if v, err := strconv.Unquote(ivStr); err == nil {
820 target.Field(0).Set(reflect.ValueOf(&stpb.Value_StringValue{v}))
821 } else if v, err := strconv.ParseBool(ivStr); err == nil {
822 target.Field(0).Set(reflect.ValueOf(&stpb.Value_BoolValue{v}))
823 } else if err := json.Unmarshal(inputValue, &[]json.RawMessage{}); err == nil {
824 lv := &stpb.ListValue{}
825 target.Field(0).Set(reflect.ValueOf(&stpb.Value_ListValue{lv}))
826 return u.unmarshalValue(reflect.ValueOf(lv).Elem(), inputValue, prop)
827 } else if err := json.Unmarshal(inputValue, &map[string]json.RawMessage{}); err == nil {
829 target.Field(0).Set(reflect.ValueOf(&stpb.Value_StructValue{sv}))
830 return u.unmarshalValue(reflect.ValueOf(sv).Elem(), inputValue, prop)
832 return fmt.Errorf("unrecognized type for Value %q", ivStr)
838 // Handle enums, which have an underlying type of int32,
839 // and may appear as strings.
840 // The case of an enum appearing as a number is handled
841 // at the bottom of this function.
842 if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
843 vmap := proto.EnumValueMap(prop.Enum)
844 // Don't need to do unquoting; valid enum names
845 // are from a limited character set.
846 s := inputValue[1 : len(inputValue)-1]
847 n, ok := vmap[string(s)]
849 return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
851 if target.Kind() == reflect.Ptr { // proto2
852 target.Set(reflect.New(targetType.Elem()))
853 target = target.Elem()
855 target.SetInt(int64(n))
859 // Handle nested messages.
860 if targetType.Kind() == reflect.Struct {
861 var jsonFields map[string]json.RawMessage
862 if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
866 consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
867 // Be liberal in what names we accept; both orig_name and camelName are okay.
868 fieldNames := acceptedJSONFieldNames(prop)
870 vOrig, okOrig := jsonFields[fieldNames.orig]
871 vCamel, okCamel := jsonFields[fieldNames.camel]
872 if !okOrig && !okCamel {
875 // If, for some reason, both are present in the data, favour the camelName.
876 var raw json.RawMessage
879 delete(jsonFields, fieldNames.orig)
883 delete(jsonFields, fieldNames.camel)
888 sprops := proto.GetProperties(targetType)
889 for i := 0; i < target.NumField(); i++ {
890 ft := target.Type().Field(i)
891 if strings.HasPrefix(ft.Name, "XXX_") {
895 valueForField, ok := consumeField(sprops.Prop[i])
900 if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
904 // Check for any oneof fields.
905 if len(jsonFields) > 0 {
906 for _, oop := range sprops.OneofTypes {
907 raw, ok := consumeField(oop.Prop)
911 nv := reflect.New(oop.Type.Elem())
912 target.Field(oop.Field).Set(nv)
913 if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
918 // Handle proto2 extensions.
919 if len(jsonFields) > 0 {
920 if ep, ok := target.Addr().Interface().(proto.Message); ok {
921 for _, ext := range proto.RegisteredExtensions(ep) {
922 name := fmt.Sprintf("[%s]", ext.Name)
923 raw, ok := jsonFields[name]
927 delete(jsonFields, name)
928 nv := reflect.New(reflect.TypeOf(ext.ExtensionType).Elem())
929 if err := u.unmarshalValue(nv.Elem(), raw, nil); err != nil {
932 if err := proto.SetExtension(ep, ext, nv.Interface()); err != nil {
938 if !u.AllowUnknownFields && len(jsonFields) > 0 {
939 // Pick any field to be the scapegoat.
941 for fname := range jsonFields {
945 return fmt.Errorf("unknown field %q in %v", f, targetType)
950 // Handle arrays (which aren't encoded bytes)
951 if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
952 var slc []json.RawMessage
953 if err := json.Unmarshal(inputValue, &slc); err != nil {
958 target.Set(reflect.MakeSlice(targetType, l, l))
959 for i := 0; i < l; i++ {
960 if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
968 // Handle maps (whose keys are always strings)
969 if targetType.Kind() == reflect.Map {
970 var mp map[string]json.RawMessage
971 if err := json.Unmarshal(inputValue, &mp); err != nil {
975 target.Set(reflect.MakeMap(targetType))
976 var keyprop, valprop *proto.Properties
978 // These could still be nil if the protobuf metadata is broken somehow.
979 // TODO: This won't work because the fields are unexported.
980 // We should probably just reparse them.
981 //keyprop, valprop = prop.mkeyprop, prop.mvalprop
983 for ks, raw := range mp {
984 // Unmarshal map key. The core json library already decoded the key into a
985 // string, so we handle that specially. Other types were quoted post-serialization.
987 if targetType.Key().Kind() == reflect.String {
988 k = reflect.ValueOf(ks)
990 k = reflect.New(targetType.Key()).Elem()
991 if err := u.unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
996 // Unmarshal map value.
997 v := reflect.New(targetType.Elem()).Elem()
998 if err := u.unmarshalValue(v, raw, valprop); err != nil {
1001 target.SetMapIndex(k, v)
1007 // 64-bit integers can be encoded as strings. In this case we drop
1008 // the quotes and proceed as normal.
1009 isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64
1010 if isNum && strings.HasPrefix(string(inputValue), `"`) {
1011 inputValue = inputValue[1 : len(inputValue)-1]
1014 // Non-finite numbers can be encoded as strings.
1015 isFloat := targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
1017 if num, ok := nonFinite[string(inputValue)]; ok {
1018 target.SetFloat(num)
1023 // Use the encoding/json for parsing other value types.
1024 return json.Unmarshal(inputValue, target.Addr().Interface())
1027 // jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
1028 func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
1029 var prop proto.Properties
1030 prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
1031 if origName || prop.JSONName == "" {
1032 prop.JSONName = prop.OrigName
1037 type fieldNames struct {
1041 func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
1042 opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
1043 if prop.JSONName != "" {
1044 opts.camel = prop.JSONName
1049 // Writer wrapper inspired by https://blog.golang.org/errors-are-values
1050 type errWriter struct {
1055 func (w *errWriter) write(str string) {
1059 _, w.err = w.writer.Write([]byte(str))
1062 // Map fields may have key types of non-float scalars, strings and enums.
1063 // The easiest way to sort them in some deterministic order is to use fmt.
1064 // If this turns out to be inefficient we can always consider other options,
1065 // such as doing a Schwartzian transform.
1067 // Numeric keys are sorted in numeric order per
1068 // https://developers.google.com/protocol-buffers/docs/proto#maps.
1069 type mapKeys []reflect.Value
1071 func (s mapKeys) Len() int { return len(s) }
1072 func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
1073 func (s mapKeys) Less(i, j int) bool {
1074 if k := s[i].Kind(); k == s[j].Kind() {
1076 case reflect.Int32, reflect.Int64:
1077 return s[i].Int() < s[j].Int()
1078 case reflect.Uint32, reflect.Uint64:
1079 return s[i].Uint() < s[j].Uint()
1082 return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())