1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2015 The Go Authors.  All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
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
15// distribution.
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.
19//
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.
31
32/*
33Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
34It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.
35
36This package produces a different output than the standard "encoding/json" package,
37which does not operate correctly on protocol buffers.
38*/
39package jsonpb
40
41import (
42	"bytes"
43	"encoding/json"
44	"errors"
45	"fmt"
46	"io"
47	"math"
48	"reflect"
49	"sort"
50	"strconv"
51	"strings"
52	"time"
53
54	"github.com/golang/protobuf/proto"
55
56	stpb "github.com/golang/protobuf/ptypes/struct"
57)
58
59const secondInNanos = int64(time.Second / time.Nanosecond)
60const maxSecondsInDuration = 315576000000
61
62// Marshaler is a configurable object for converting between
63// protocol buffer objects and a JSON representation for them.
64type Marshaler struct {
65	// Whether to render enum values as integers, as opposed to string values.
66	EnumsAsInts bool
67
68	// Whether to render fields with zero values.
69	EmitDefaults bool
70
71	// A string to indent each level by. The presence of this field will
72	// also cause a space to appear between the field separator and
73	// value, and for newlines to be appear between fields and array
74	// elements.
75	Indent string
76
77	// Whether to use the original (.proto) name for fields.
78	OrigName bool
79
80	// A custom URL resolver to use when marshaling Any messages to JSON.
81	// If unset, the default resolution strategy is to extract the
82	// fully-qualified type name from the type URL and pass that to
83	// proto.MessageType(string).
84	AnyResolver AnyResolver
85}
86
87// AnyResolver takes a type URL, present in an Any message, and resolves it into
88// an instance of the associated message.
89type AnyResolver interface {
90	Resolve(typeUrl string) (proto.Message, error)
91}
92
93func defaultResolveAny(typeUrl string) (proto.Message, error) {
94	// Only the part of typeUrl after the last slash is relevant.
95	mname := typeUrl
96	if slash := strings.LastIndex(mname, "/"); slash >= 0 {
97		mname = mname[slash+1:]
98	}
99	mt := proto.MessageType(mname)
100	if mt == nil {
101		return nil, fmt.Errorf("unknown message type %q", mname)
102	}
103	return reflect.New(mt.Elem()).Interface().(proto.Message), nil
104}
105
106// JSONPBMarshaler is implemented by protobuf messages that customize the
107// way they are marshaled to JSON. Messages that implement this should
108// also implement JSONPBUnmarshaler so that the custom format can be
109// parsed.
110//
111// The JSON marshaling must follow the proto to JSON specification:
112//	https://developers.google.com/protocol-buffers/docs/proto3#json
113type JSONPBMarshaler interface {
114	MarshalJSONPB(*Marshaler) ([]byte, error)
115}
116
117// JSONPBUnmarshaler is implemented by protobuf messages that customize
118// the way they are unmarshaled from JSON. Messages that implement this
119// should also implement JSONPBMarshaler so that the custom format can be
120// produced.
121//
122// The JSON unmarshaling must follow the JSON to proto specification:
123//	https://developers.google.com/protocol-buffers/docs/proto3#json
124type JSONPBUnmarshaler interface {
125	UnmarshalJSONPB(*Unmarshaler, []byte) error
126}
127
128// Marshal marshals a protocol buffer into JSON.
129func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
130	v := reflect.ValueOf(pb)
131	if pb == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
132		return errors.New("Marshal called with nil")
133	}
134	// Check for unset required fields first.
135	if err := checkRequiredFields(pb); err != nil {
136		return err
137	}
138	writer := &errWriter{writer: out}
139	return m.marshalObject(writer, pb, "", "")
140}
141
142// MarshalToString converts a protocol buffer object to JSON string.
143func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
144	var buf bytes.Buffer
145	if err := m.Marshal(&buf, pb); err != nil {
146		return "", err
147	}
148	return buf.String(), nil
149}
150
151type int32Slice []int32
152
153var nonFinite = map[string]float64{
154	`"NaN"`:       math.NaN(),
155	`"Infinity"`:  math.Inf(1),
156	`"-Infinity"`: math.Inf(-1),
157}
158
159// For sorting extensions ids to ensure stable output.
160func (s int32Slice) Len() int           { return len(s) }
161func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
162func (s int32Slice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
163
164type wkt interface {
165	XXX_WellKnownType() string
166}
167
168// marshalObject writes a struct to the Writer.
169func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
170	if jsm, ok := v.(JSONPBMarshaler); ok {
171		b, err := jsm.MarshalJSONPB(m)
172		if err != nil {
173			return err
174		}
175		if typeURL != "" {
176			// we are marshaling this object to an Any type
177			var js map[string]*json.RawMessage
178			if err = json.Unmarshal(b, &js); err != nil {
179				return fmt.Errorf("type %T produced invalid JSON: %v", v, err)
180			}
181			turl, err := json.Marshal(typeURL)
182			if err != nil {
183				return fmt.Errorf("failed to marshal type URL %q to JSON: %v", typeURL, err)
184			}
185			js["@type"] = (*json.RawMessage)(&turl)
186			if m.Indent != "" {
187				b, err = json.MarshalIndent(js, indent, m.Indent)
188			} else {
189				b, err = json.Marshal(js)
190			}
191			if err != nil {
192				return err
193			}
194		}
195
196		out.write(string(b))
197		return out.err
198	}
199
200	s := reflect.ValueOf(v).Elem()
201
202	// Handle well-known types.
203	if wkt, ok := v.(wkt); ok {
204		switch wkt.XXX_WellKnownType() {
205		case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
206			"Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
207			// "Wrappers use the same representation in JSON
208			//  as the wrapped primitive type, ..."
209			sprop := proto.GetProperties(s.Type())
210			return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
211		case "Any":
212			// Any is a bit more involved.
213			return m.marshalAny(out, v, indent)
214		case "Duration":
215			s, ns := s.Field(0).Int(), s.Field(1).Int()
216			if s < -maxSecondsInDuration || s > maxSecondsInDuration {
217				return fmt.Errorf("seconds out of range %v", s)
218			}
219			if ns <= -secondInNanos || ns >= secondInNanos {
220				return fmt.Errorf("ns out of range (%v, %v)", -secondInNanos, secondInNanos)
221			}
222			if (s > 0 && ns < 0) || (s < 0 && ns > 0) {
223				return errors.New("signs of seconds and nanos do not match")
224			}
225			// Generated output always contains 0, 3, 6, or 9 fractional digits,
226			// depending on required precision, followed by the suffix "s".
227			f := "%d.%09d"
228			if ns < 0 {
229				ns = -ns
230				if s == 0 {
231					f = "-%d.%09d"
232				}
233			}
234			x := fmt.Sprintf(f, s, ns)
235			x = strings.TrimSuffix(x, "000")
236			x = strings.TrimSuffix(x, "000")
237			x = strings.TrimSuffix(x, ".000")
238			out.write(`"`)
239			out.write(x)
240			out.write(`s"`)
241			return out.err
242		case "Struct", "ListValue":
243			// Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice.
244			// TODO: pass the correct Properties if needed.
245			return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
246		case "Timestamp":
247			// "RFC 3339, where generated output will always be Z-normalized
248			//  and uses 0, 3, 6 or 9 fractional digits."
249			s, ns := s.Field(0).Int(), s.Field(1).Int()
250			if ns < 0 || ns >= secondInNanos {
251				return fmt.Errorf("ns out of range [0, %v)", secondInNanos)
252			}
253			t := time.Unix(s, ns).UTC()
254			// time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
255			x := t.Format("2006-01-02T15:04:05.000000000")
256			x = strings.TrimSuffix(x, "000")
257			x = strings.TrimSuffix(x, "000")
258			x = strings.TrimSuffix(x, ".000")
259			out.write(`"`)
260			out.write(x)
261			out.write(`Z"`)
262			return out.err
263		case "Value":
264			// Value has a single oneof.
265			kind := s.Field(0)
266			if kind.IsNil() {
267				// "absence of any variant indicates an error"
268				return errors.New("nil Value")
269			}
270			// oneof -> *T -> T -> T.F
271			x := kind.Elem().Elem().Field(0)
272			// TODO: pass the correct Properties if needed.
273			return m.marshalValue(out, &proto.Properties{}, x, indent)
274		}
275	}
276
277	out.write("{")
278	if m.Indent != "" {
279		out.write("\n")
280	}
281
282	firstField := true
283
284	if typeURL != "" {
285		if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
286			return err
287		}
288		firstField = false
289	}
290
291	for i := 0; i < s.NumField(); i++ {
292		value := s.Field(i)
293		valueField := s.Type().Field(i)
294		if strings.HasPrefix(valueField.Name, "XXX_") {
295			continue
296		}
297
298		// IsNil will panic on most value kinds.
299		switch value.Kind() {
300		case reflect.Chan, reflect.Func, reflect.Interface:
301			if value.IsNil() {
302				continue
303			}
304		}
305
306		if !m.EmitDefaults {
307			switch value.Kind() {
308			case reflect.Bool:
309				if !value.Bool() {
310					continue
311				}
312			case reflect.Int32, reflect.Int64:
313				if value.Int() == 0 {
314					continue
315				}
316			case reflect.Uint32, reflect.Uint64:
317				if value.Uint() == 0 {
318					continue
319				}
320			case reflect.Float32, reflect.Float64:
321				if value.Float() == 0 {
322					continue
323				}
324			case reflect.String:
325				if value.Len() == 0 {
326					continue
327				}
328			case reflect.Map, reflect.Ptr, reflect.Slice:
329				if value.IsNil() {
330					continue
331				}
332			}
333		}
334
335		// Oneof fields need special handling.
336		if valueField.Tag.Get("protobuf_oneof") != "" {
337			// value is an interface containing &T{real_value}.
338			sv := value.Elem().Elem() // interface -> *T -> T
339			value = sv.Field(0)
340			valueField = sv.Type().Field(0)
341		}
342		prop := jsonProperties(valueField, m.OrigName)
343		if !firstField {
344			m.writeSep(out)
345		}
346		if err := m.marshalField(out, prop, value, indent); err != nil {
347			return err
348		}
349		firstField = false
350	}
351
352	// Handle proto2 extensions.
353	if ep, ok := v.(proto.Message); ok {
354		extensions := proto.RegisteredExtensions(v)
355		// Sort extensions for stable output.
356		ids := make([]int32, 0, len(extensions))
357		for id, desc := range extensions {
358			if !proto.HasExtension(ep, desc) {
359				continue
360			}
361			ids = append(ids, id)
362		}
363		sort.Sort(int32Slice(ids))
364		for _, id := range ids {
365			desc := extensions[id]
366			if desc == nil {
367				// unknown extension
368				continue
369			}
370			ext, extErr := proto.GetExtension(ep, desc)
371			if extErr != nil {
372				return extErr
373			}
374			value := reflect.ValueOf(ext)
375			var prop proto.Properties
376			prop.Parse(desc.Tag)
377			prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
378			if !firstField {
379				m.writeSep(out)
380			}
381			if err := m.marshalField(out, &prop, value, indent); err != nil {
382				return err
383			}
384			firstField = false
385		}
386
387	}
388
389	if m.Indent != "" {
390		out.write("\n")
391		out.write(indent)
392	}
393	out.write("}")
394	return out.err
395}
396
397func (m *Marshaler) writeSep(out *errWriter) {
398	if m.Indent != "" {
399		out.write(",\n")
400	} else {
401		out.write(",")
402	}
403}
404
405func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
406	// "If the Any contains a value that has a special JSON mapping,
407	//  it will be converted as follows: {"@type": xxx, "value": yyy}.
408	//  Otherwise, the value will be converted into a JSON object,
409	//  and the "@type" field will be inserted to indicate the actual data type."
410	v := reflect.ValueOf(any).Elem()
411	turl := v.Field(0).String()
412	val := v.Field(1).Bytes()
413
414	var msg proto.Message
415	var err error
416	if m.AnyResolver != nil {
417		msg, err = m.AnyResolver.Resolve(turl)
418	} else {
419		msg, err = defaultResolveAny(turl)
420	}
421	if err != nil {
422		return err
423	}
424
425	if err := proto.Unmarshal(val, msg); err != nil {
426		return err
427	}
428
429	if _, ok := msg.(wkt); ok {
430		out.write("{")
431		if m.Indent != "" {
432			out.write("\n")
433		}
434		if err := m.marshalTypeURL(out, indent, turl); err != nil {
435			return err
436		}
437		m.writeSep(out)
438		if m.Indent != "" {
439			out.write(indent)
440			out.write(m.Indent)
441			out.write(`"value": `)
442		} else {
443			out.write(`"value":`)
444		}
445		if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
446			return err
447		}
448		if m.Indent != "" {
449			out.write("\n")
450			out.write(indent)
451		}
452		out.write("}")
453		return out.err
454	}
455
456	return m.marshalObject(out, msg, indent, turl)
457}
458
459func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
460	if m.Indent != "" {
461		out.write(indent)
462		out.write(m.Indent)
463	}
464	out.write(`"@type":`)
465	if m.Indent != "" {
466		out.write(" ")
467	}
468	b, err := json.Marshal(typeURL)
469	if err != nil {
470		return err
471	}
472	out.write(string(b))
473	return out.err
474}
475
476// marshalField writes field description and value to the Writer.
477func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
478	if m.Indent != "" {
479		out.write(indent)
480		out.write(m.Indent)
481	}
482	out.write(`"`)
483	out.write(prop.JSONName)
484	out.write(`":`)
485	if m.Indent != "" {
486		out.write(" ")
487	}
488	if err := m.marshalValue(out, prop, v, indent); err != nil {
489		return err
490	}
491	return nil
492}
493
494// marshalValue writes the value to the Writer.
495func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
496	var err error
497	v = reflect.Indirect(v)
498
499	// Handle nil pointer
500	if v.Kind() == reflect.Invalid {
501		out.write("null")
502		return out.err
503	}
504
505	// Handle repeated elements.
506	if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
507		out.write("[")
508		comma := ""
509		for i := 0; i < v.Len(); i++ {
510			sliceVal := v.Index(i)
511			out.write(comma)
512			if m.Indent != "" {
513				out.write("\n")
514				out.write(indent)
515				out.write(m.Indent)
516				out.write(m.Indent)
517			}
518			if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
519				return err
520			}
521			comma = ","
522		}
523		if m.Indent != "" {
524			out.write("\n")
525			out.write(indent)
526			out.write(m.Indent)
527		}
528		out.write("]")
529		return out.err
530	}
531
532	// Handle well-known types.
533	// Most are handled up in marshalObject (because 99% are messages).
534	if wkt, ok := v.Interface().(wkt); ok {
535		switch wkt.XXX_WellKnownType() {
536		case "NullValue":
537			out.write("null")
538			return out.err
539		}
540	}
541
542	// Handle enumerations.
543	if !m.EnumsAsInts && prop.Enum != "" {
544		// Unknown enum values will are stringified by the proto library as their
545		// value. Such values should _not_ be quoted or they will be interpreted
546		// as an enum string instead of their value.
547		enumStr := v.Interface().(fmt.Stringer).String()
548		var valStr string
549		if v.Kind() == reflect.Ptr {
550			valStr = strconv.Itoa(int(v.Elem().Int()))
551		} else {
552			valStr = strconv.Itoa(int(v.Int()))
553		}
554		isKnownEnum := enumStr != valStr
555		if isKnownEnum {
556			out.write(`"`)
557		}
558		out.write(enumStr)
559		if isKnownEnum {
560			out.write(`"`)
561		}
562		return out.err
563	}
564
565	// Handle nested messages.
566	if v.Kind() == reflect.Struct {
567		return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent, "")
568	}
569
570	// Handle maps.
571	// Since Go randomizes map iteration, we sort keys for stable output.
572	if v.Kind() == reflect.Map {
573		out.write(`{`)
574		keys := v.MapKeys()
575		sort.Sort(mapKeys(keys))
576		for i, k := range keys {
577			if i > 0 {
578				out.write(`,`)
579			}
580			if m.Indent != "" {
581				out.write("\n")
582				out.write(indent)
583				out.write(m.Indent)
584				out.write(m.Indent)
585			}
586
587			// TODO handle map key prop properly
588			b, err := json.Marshal(k.Interface())
589			if err != nil {
590				return err
591			}
592			s := string(b)
593
594			// If the JSON is not a string value, encode it again to make it one.
595			if !strings.HasPrefix(s, `"`) {
596				b, err := json.Marshal(s)
597				if err != nil {
598					return err
599				}
600				s = string(b)
601			}
602
603			out.write(s)
604			out.write(`:`)
605			if m.Indent != "" {
606				out.write(` `)
607			}
608
609			vprop := prop
610			if prop != nil && prop.MapValProp != nil {
611				vprop = prop.MapValProp
612			}
613			if err := m.marshalValue(out, vprop, v.MapIndex(k), indent+m.Indent); err != nil {
614				return err
615			}
616		}
617		if m.Indent != "" {
618			out.write("\n")
619			out.write(indent)
620			out.write(m.Indent)
621		}
622		out.write(`}`)
623		return out.err
624	}
625
626	// Handle non-finite floats, e.g. NaN, Infinity and -Infinity.
627	if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
628		f := v.Float()
629		var sval string
630		switch {
631		case math.IsInf(f, 1):
632			sval = `"Infinity"`
633		case math.IsInf(f, -1):
634			sval = `"-Infinity"`
635		case math.IsNaN(f):
636			sval = `"NaN"`
637		}
638		if sval != "" {
639			out.write(sval)
640			return out.err
641		}
642	}
643
644	// Default handling defers to the encoding/json library.
645	b, err := json.Marshal(v.Interface())
646	if err != nil {
647		return err
648	}
649	needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
650	if needToQuote {
651		out.write(`"`)
652	}
653	out.write(string(b))
654	if needToQuote {
655		out.write(`"`)
656	}
657	return out.err
658}
659
660// Unmarshaler is a configurable object for converting from a JSON
661// representation to a protocol buffer object.
662type Unmarshaler struct {
663	// Whether to allow messages to contain unknown fields, as opposed to
664	// failing to unmarshal.
665	AllowUnknownFields bool
666
667	// A custom URL resolver to use when unmarshaling Any messages from JSON.
668	// If unset, the default resolution strategy is to extract the
669	// fully-qualified type name from the type URL and pass that to
670	// proto.MessageType(string).
671	AnyResolver AnyResolver
672}
673
674// UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
675// This function is lenient and will decode any options permutations of the
676// related Marshaler.
677func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
678	inputValue := json.RawMessage{}
679	if err := dec.Decode(&inputValue); err != nil {
680		return err
681	}
682	if err := u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil); err != nil {
683		return err
684	}
685	return checkRequiredFields(pb)
686}
687
688// Unmarshal unmarshals a JSON object stream into a protocol
689// buffer. This function is lenient and will decode any options
690// permutations of the related Marshaler.
691func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error {
692	dec := json.NewDecoder(r)
693	return u.UnmarshalNext(dec, pb)
694}
695
696// UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
697// This function is lenient and will decode any options permutations of the
698// related Marshaler.
699func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
700	return new(Unmarshaler).UnmarshalNext(dec, pb)
701}
702
703// Unmarshal unmarshals a JSON object stream into a protocol
704// buffer. This function is lenient and will decode any options
705// permutations of the related Marshaler.
706func Unmarshal(r io.Reader, pb proto.Message) error {
707	return new(Unmarshaler).Unmarshal(r, pb)
708}
709
710// UnmarshalString will populate the fields of a protocol buffer based
711// on a JSON string. This function is lenient and will decode any options
712// permutations of the related Marshaler.
713func UnmarshalString(str string, pb proto.Message) error {
714	return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb)
715}
716
717// unmarshalValue converts/copies a value into the target.
718// prop may be nil.
719func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
720	targetType := target.Type()
721
722	// Allocate memory for pointer fields.
723	if targetType.Kind() == reflect.Ptr {
724		// If input value is "null" and target is a pointer type, then the field should be treated as not set
725		// UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue.
726		_, isJSONPBUnmarshaler := target.Interface().(JSONPBUnmarshaler)
727		if string(inputValue) == "null" && targetType != reflect.TypeOf(&stpb.Value{}) && !isJSONPBUnmarshaler {
728			return nil
729		}
730		target.Set(reflect.New(targetType.Elem()))
731
732		return u.unmarshalValue(target.Elem(), inputValue, prop)
733	}
734
735	if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok {
736		return jsu.UnmarshalJSONPB(u, []byte(inputValue))
737	}
738
739	// Handle well-known types that are not pointers.
740	if w, ok := target.Addr().Interface().(wkt); ok {
741		switch w.XXX_WellKnownType() {
742		case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
743			"Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
744			return u.unmarshalValue(target.Field(0), inputValue, prop)
745		case "Any":
746			// Use json.RawMessage pointer type instead of value to support pre-1.8 version.
747			// 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
748			// https://github.com/golang/go/issues/14493
749			var jsonFields map[string]*json.RawMessage
750			if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
751				return err
752			}
753
754			val, ok := jsonFields["@type"]
755			if !ok || val == nil {
756				return errors.New("Any JSON doesn't have '@type'")
757			}
758
759			var turl string
760			if err := json.Unmarshal([]byte(*val), &turl); err != nil {
761				return fmt.Errorf("can't unmarshal Any's '@type': %q", *val)
762			}
763			target.Field(0).SetString(turl)
764
765			var m proto.Message
766			var err error
767			if u.AnyResolver != nil {
768				m, err = u.AnyResolver.Resolve(turl)
769			} else {
770				m, err = defaultResolveAny(turl)
771			}
772			if err != nil {
773				return err
774			}
775
776			if _, ok := m.(wkt); ok {
777				val, ok := jsonFields["value"]
778				if !ok {
779					return errors.New("Any JSON doesn't have 'value'")
780				}
781
782				if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil {
783					return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
784				}
785			} else {
786				delete(jsonFields, "@type")
787				nestedProto, err := json.Marshal(jsonFields)
788				if err != nil {
789					return fmt.Errorf("can't generate JSON for Any's nested proto to be unmarshaled: %v", err)
790				}
791
792				if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), nestedProto, nil); err != nil {
793					return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
794				}
795			}
796
797			b, err := proto.Marshal(m)
798			if err != nil {
799				return fmt.Errorf("can't marshal proto %T into Any.Value: %v", m, err)
800			}
801			target.Field(1).SetBytes(b)
802
803			return nil
804		case "Duration":
805			unq, err := unquote(string(inputValue))
806			if err != nil {
807				return err
808			}
809
810			d, err := time.ParseDuration(unq)
811			if err != nil {
812				return fmt.Errorf("bad Duration: %v", err)
813			}
814
815			ns := d.Nanoseconds()
816			s := ns / 1e9
817			ns %= 1e9
818			target.Field(0).SetInt(s)
819			target.Field(1).SetInt(ns)
820			return nil
821		case "Timestamp":
822			unq, err := unquote(string(inputValue))
823			if err != nil {
824				return err
825			}
826
827			t, err := time.Parse(time.RFC3339Nano, unq)
828			if err != nil {
829				return fmt.Errorf("bad Timestamp: %v", err)
830			}
831
832			target.Field(0).SetInt(t.Unix())
833			target.Field(1).SetInt(int64(t.Nanosecond()))
834			return nil
835		case "Struct":
836			var m map[string]json.RawMessage
837			if err := json.Unmarshal(inputValue, &m); err != nil {
838				return fmt.Errorf("bad StructValue: %v", err)
839			}
840
841			target.Field(0).Set(reflect.ValueOf(map[string]*stpb.Value{}))
842			for k, jv := range m {
843				pv := &stpb.Value{}
844				if err := u.unmarshalValue(reflect.ValueOf(pv).Elem(), jv, prop); err != nil {
845					return fmt.Errorf("bad value in StructValue for key %q: %v", k, err)
846				}
847				target.Field(0).SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(pv))
848			}
849			return nil
850		case "ListValue":
851			var s []json.RawMessage
852			if err := json.Unmarshal(inputValue, &s); err != nil {
853				return fmt.Errorf("bad ListValue: %v", err)
854			}
855
856			target.Field(0).Set(reflect.ValueOf(make([]*stpb.Value, len(s))))
857			for i, sv := range s {
858				if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil {
859					return err
860				}
861			}
862			return nil
863		case "Value":
864			ivStr := string(inputValue)
865			if ivStr == "null" {
866				target.Field(0).Set(reflect.ValueOf(&stpb.Value_NullValue{}))
867			} else if v, err := strconv.ParseFloat(ivStr, 0); err == nil {
868				target.Field(0).Set(reflect.ValueOf(&stpb.Value_NumberValue{v}))
869			} else if v, err := unquote(ivStr); err == nil {
870				target.Field(0).Set(reflect.ValueOf(&stpb.Value_StringValue{v}))
871			} else if v, err := strconv.ParseBool(ivStr); err == nil {
872				target.Field(0).Set(reflect.ValueOf(&stpb.Value_BoolValue{v}))
873			} else if err := json.Unmarshal(inputValue, &[]json.RawMessage{}); err == nil {
874				lv := &stpb.ListValue{}
875				target.Field(0).Set(reflect.ValueOf(&stpb.Value_ListValue{lv}))
876				return u.unmarshalValue(reflect.ValueOf(lv).Elem(), inputValue, prop)
877			} else if err := json.Unmarshal(inputValue, &map[string]json.RawMessage{}); err == nil {
878				sv := &stpb.Struct{}
879				target.Field(0).Set(reflect.ValueOf(&stpb.Value_StructValue{sv}))
880				return u.unmarshalValue(reflect.ValueOf(sv).Elem(), inputValue, prop)
881			} else {
882				return fmt.Errorf("unrecognized type for Value %q", ivStr)
883			}
884			return nil
885		}
886	}
887
888	// Handle enums, which have an underlying type of int32,
889	// and may appear as strings.
890	// The case of an enum appearing as a number is handled
891	// at the bottom of this function.
892	if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
893		vmap := proto.EnumValueMap(prop.Enum)
894		// Don't need to do unquoting; valid enum names
895		// are from a limited character set.
896		s := inputValue[1 : len(inputValue)-1]
897		n, ok := vmap[string(s)]
898		if !ok {
899			return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
900		}
901		if target.Kind() == reflect.Ptr { // proto2
902			target.Set(reflect.New(targetType.Elem()))
903			target = target.Elem()
904		}
905		if targetType.Kind() != reflect.Int32 {
906			return fmt.Errorf("invalid target %q for enum %s", targetType.Kind(), prop.Enum)
907		}
908		target.SetInt(int64(n))
909		return nil
910	}
911
912	// Handle nested messages.
913	if targetType.Kind() == reflect.Struct {
914		var jsonFields map[string]json.RawMessage
915		if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
916			return err
917		}
918
919		consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
920			// Be liberal in what names we accept; both orig_name and camelName are okay.
921			fieldNames := acceptedJSONFieldNames(prop)
922
923			vOrig, okOrig := jsonFields[fieldNames.orig]
924			vCamel, okCamel := jsonFields[fieldNames.camel]
925			if !okOrig && !okCamel {
926				return nil, false
927			}
928			// If, for some reason, both are present in the data, favour the camelName.
929			var raw json.RawMessage
930			if okOrig {
931				raw = vOrig
932				delete(jsonFields, fieldNames.orig)
933			}
934			if okCamel {
935				raw = vCamel
936				delete(jsonFields, fieldNames.camel)
937			}
938			return raw, true
939		}
940
941		sprops := proto.GetProperties(targetType)
942		for i := 0; i < target.NumField(); i++ {
943			ft := target.Type().Field(i)
944			if strings.HasPrefix(ft.Name, "XXX_") {
945				continue
946			}
947
948			valueForField, ok := consumeField(sprops.Prop[i])
949			if !ok {
950				continue
951			}
952
953			if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
954				return err
955			}
956		}
957		// Check for any oneof fields.
958		if len(jsonFields) > 0 {
959			for _, oop := range sprops.OneofTypes {
960				raw, ok := consumeField(oop.Prop)
961				if !ok {
962					continue
963				}
964				nv := reflect.New(oop.Type.Elem())
965				target.Field(oop.Field).Set(nv)
966				if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
967					return err
968				}
969			}
970		}
971		// Handle proto2 extensions.
972		if len(jsonFields) > 0 {
973			if ep, ok := target.Addr().Interface().(proto.Message); ok {
974				for _, ext := range proto.RegisteredExtensions(ep) {
975					name := fmt.Sprintf("[%s]", ext.Name)
976					raw, ok := jsonFields[name]
977					if !ok {
978						continue
979					}
980					delete(jsonFields, name)
981					nv := reflect.New(reflect.TypeOf(ext.ExtensionType).Elem())
982					if err := u.unmarshalValue(nv.Elem(), raw, nil); err != nil {
983						return err
984					}
985					if err := proto.SetExtension(ep, ext, nv.Interface()); err != nil {
986						return err
987					}
988				}
989			}
990		}
991		if !u.AllowUnknownFields && len(jsonFields) > 0 {
992			// Pick any field to be the scapegoat.
993			var f string
994			for fname := range jsonFields {
995				f = fname
996				break
997			}
998			return fmt.Errorf("unknown field %q in %v", f, targetType)
999		}
1000		return nil
1001	}
1002
1003	// Handle arrays (which aren't encoded bytes)
1004	if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
1005		var slc []json.RawMessage
1006		if err := json.Unmarshal(inputValue, &slc); err != nil {
1007			return err
1008		}
1009		if slc != nil {
1010			l := len(slc)
1011			target.Set(reflect.MakeSlice(targetType, l, l))
1012			for i := 0; i < l; i++ {
1013				if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
1014					return err
1015				}
1016			}
1017		}
1018		return nil
1019	}
1020
1021	// Handle maps (whose keys are always strings)
1022	if targetType.Kind() == reflect.Map {
1023		var mp map[string]json.RawMessage
1024		if err := json.Unmarshal(inputValue, &mp); err != nil {
1025			return err
1026		}
1027		if mp != nil {
1028			target.Set(reflect.MakeMap(targetType))
1029			for ks, raw := range mp {
1030				// Unmarshal map key. The core json library already decoded the key into a
1031				// string, so we handle that specially. Other types were quoted post-serialization.
1032				var k reflect.Value
1033				if targetType.Key().Kind() == reflect.String {
1034					k = reflect.ValueOf(ks)
1035				} else {
1036					k = reflect.New(targetType.Key()).Elem()
1037					var kprop *proto.Properties
1038					if prop != nil && prop.MapKeyProp != nil {
1039						kprop = prop.MapKeyProp
1040					}
1041					if err := u.unmarshalValue(k, json.RawMessage(ks), kprop); err != nil {
1042						return err
1043					}
1044				}
1045
1046				// Unmarshal map value.
1047				v := reflect.New(targetType.Elem()).Elem()
1048				var vprop *proto.Properties
1049				if prop != nil && prop.MapValProp != nil {
1050					vprop = prop.MapValProp
1051				}
1052				if err := u.unmarshalValue(v, raw, vprop); err != nil {
1053					return err
1054				}
1055				target.SetMapIndex(k, v)
1056			}
1057		}
1058		return nil
1059	}
1060
1061	// Non-finite numbers can be encoded as strings.
1062	isFloat := targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
1063	if isFloat {
1064		if num, ok := nonFinite[string(inputValue)]; ok {
1065			target.SetFloat(num)
1066			return nil
1067		}
1068	}
1069
1070	// integers & floats can be encoded as strings. In this case we drop
1071	// the quotes and proceed as normal.
1072	isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64 ||
1073		targetType.Kind() == reflect.Int32 || targetType.Kind() == reflect.Uint32 ||
1074		targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
1075	if isNum && strings.HasPrefix(string(inputValue), `"`) {
1076		inputValue = inputValue[1 : len(inputValue)-1]
1077	}
1078
1079	// Use the encoding/json for parsing other value types.
1080	return json.Unmarshal(inputValue, target.Addr().Interface())
1081}
1082
1083func unquote(s string) (string, error) {
1084	var ret string
1085	err := json.Unmarshal([]byte(s), &ret)
1086	return ret, err
1087}
1088
1089// jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
1090func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
1091	var prop proto.Properties
1092	prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
1093	if origName || prop.JSONName == "" {
1094		prop.JSONName = prop.OrigName
1095	}
1096	return &prop
1097}
1098
1099type fieldNames struct {
1100	orig, camel string
1101}
1102
1103func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
1104	opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
1105	if prop.JSONName != "" {
1106		opts.camel = prop.JSONName
1107	}
1108	return opts
1109}
1110
1111// Writer wrapper inspired by https://blog.golang.org/errors-are-values
1112type errWriter struct {
1113	writer io.Writer
1114	err    error
1115}
1116
1117func (w *errWriter) write(str string) {
1118	if w.err != nil {
1119		return
1120	}
1121	_, w.err = w.writer.Write([]byte(str))
1122}
1123
1124// Map fields may have key types of non-float scalars, strings and enums.
1125// The easiest way to sort them in some deterministic order is to use fmt.
1126// If this turns out to be inefficient we can always consider other options,
1127// such as doing a Schwartzian transform.
1128//
1129// Numeric keys are sorted in numeric order per
1130// https://developers.google.com/protocol-buffers/docs/proto#maps.
1131type mapKeys []reflect.Value
1132
1133func (s mapKeys) Len() int      { return len(s) }
1134func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
1135func (s mapKeys) Less(i, j int) bool {
1136	if k := s[i].Kind(); k == s[j].Kind() {
1137		switch k {
1138		case reflect.String:
1139			return s[i].String() < s[j].String()
1140		case reflect.Int32, reflect.Int64:
1141			return s[i].Int() < s[j].Int()
1142		case reflect.Uint32, reflect.Uint64:
1143			return s[i].Uint() < s[j].Uint()
1144		}
1145	}
1146	return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
1147}
1148
1149// checkRequiredFields returns an error if any required field in the given proto message is not set.
1150// This function is used by both Marshal and Unmarshal.  While required fields only exist in a
1151// proto2 message, a proto3 message can contain proto2 message(s).
1152func checkRequiredFields(pb proto.Message) error {
1153	// Most well-known type messages do not contain required fields.  The "Any" type may contain
1154	// a message that has required fields.
1155	//
1156	// When an Any message is being marshaled, the code will invoked proto.Unmarshal on Any.Value
1157	// field in order to transform that into JSON, and that should have returned an error if a
1158	// required field is not set in the embedded message.
1159	//
1160	// When an Any message is being unmarshaled, the code will have invoked proto.Marshal on the
1161	// embedded message to store the serialized message in Any.Value field, and that should have
1162	// returned an error if a required field is not set.
1163	if _, ok := pb.(wkt); ok {
1164		return nil
1165	}
1166
1167	v := reflect.ValueOf(pb)
1168	// Skip message if it is not a struct pointer.
1169	if v.Kind() != reflect.Ptr {
1170		return nil
1171	}
1172	v = v.Elem()
1173	if v.Kind() != reflect.Struct {
1174		return nil
1175	}
1176
1177	for i := 0; i < v.NumField(); i++ {
1178		field := v.Field(i)
1179		sfield := v.Type().Field(i)
1180
1181		if sfield.PkgPath != "" {
1182			// blank PkgPath means the field is exported; skip if not exported
1183			continue
1184		}
1185
1186		if strings.HasPrefix(sfield.Name, "XXX_") {
1187			continue
1188		}
1189
1190		// Oneof field is an interface implemented by wrapper structs containing the actual oneof
1191		// field, i.e. an interface containing &T{real_value}.
1192		if sfield.Tag.Get("protobuf_oneof") != "" {
1193			if field.Kind() != reflect.Interface {
1194				continue
1195			}
1196			v := field.Elem()
1197			if v.Kind() != reflect.Ptr || v.IsNil() {
1198				continue
1199			}
1200			v = v.Elem()
1201			if v.Kind() != reflect.Struct || v.NumField() < 1 {
1202				continue
1203			}
1204			field = v.Field(0)
1205			sfield = v.Type().Field(0)
1206		}
1207
1208		protoTag := sfield.Tag.Get("protobuf")
1209		if protoTag == "" {
1210			continue
1211		}
1212		var prop proto.Properties
1213		prop.Init(sfield.Type, sfield.Name, protoTag, &sfield)
1214
1215		switch field.Kind() {
1216		case reflect.Map:
1217			if field.IsNil() {
1218				continue
1219			}
1220			// Check each map value.
1221			keys := field.MapKeys()
1222			for _, k := range keys {
1223				v := field.MapIndex(k)
1224				if err := checkRequiredFieldsInValue(v); err != nil {
1225					return err
1226				}
1227			}
1228		case reflect.Slice:
1229			// Handle non-repeated type, e.g. bytes.
1230			if !prop.Repeated {
1231				if prop.Required && field.IsNil() {
1232					return fmt.Errorf("required field %q is not set", prop.Name)
1233				}
1234				continue
1235			}
1236
1237			// Handle repeated type.
1238			if field.IsNil() {
1239				continue
1240			}
1241			// Check each slice item.
1242			for i := 0; i < field.Len(); i++ {
1243				v := field.Index(i)
1244				if err := checkRequiredFieldsInValue(v); err != nil {
1245					return err
1246				}
1247			}
1248		case reflect.Ptr:
1249			if field.IsNil() {
1250				if prop.Required {
1251					return fmt.Errorf("required field %q is not set", prop.Name)
1252				}
1253				continue
1254			}
1255			if err := checkRequiredFieldsInValue(field); err != nil {
1256				return err
1257			}
1258		}
1259	}
1260
1261	// Handle proto2 extensions.
1262	for _, ext := range proto.RegisteredExtensions(pb) {
1263		if !proto.HasExtension(pb, ext) {
1264			continue
1265		}
1266		ep, err := proto.GetExtension(pb, ext)
1267		if err != nil {
1268			return err
1269		}
1270		err = checkRequiredFieldsInValue(reflect.ValueOf(ep))
1271		if err != nil {
1272			return err
1273		}
1274	}
1275
1276	return nil
1277}
1278
1279func checkRequiredFieldsInValue(v reflect.Value) error {
1280	if pm, ok := v.Interface().(proto.Message); ok {
1281		return checkRequiredFields(pm)
1282	}
1283	return nil
1284}
1285