...
Run Format

Source file src/pkg/encoding/json/encode.go

     1	// Copyright 2010 The Go Authors.  All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package json implements encoding and decoding of JSON objects as defined in
     6	// RFC 4627. The mapping between JSON objects and Go values is described
     7	// in the documentation for the Marshal and Unmarshal functions.
     8	//
     9	// See "JSON and Go" for an introduction to this package:
    10	// http://golang.org/doc/articles/json_and_go.html
    11	package json
    12	
    13	import (
    14		"bytes"
    15		"encoding"
    16		"encoding/base64"
    17		"math"
    18		"reflect"
    19		"runtime"
    20		"sort"
    21		"strconv"
    22		"strings"
    23		"sync"
    24		"unicode"
    25		"unicode/utf8"
    26	)
    27	
    28	// Marshal returns the JSON encoding of v.
    29	//
    30	// Marshal traverses the value v recursively.
    31	// If an encountered value implements the Marshaler interface
    32	// and is not a nil pointer, Marshal calls its MarshalJSON method
    33	// to produce JSON.  The nil pointer exception is not strictly necessary
    34	// but mimics a similar, necessary exception in the behavior of
    35	// UnmarshalJSON.
    36	//
    37	// Otherwise, Marshal uses the following type-dependent default encodings:
    38	//
    39	// Boolean values encode as JSON booleans.
    40	//
    41	// Floating point, integer, and Number values encode as JSON numbers.
    42	//
    43	// String values encode as JSON strings. InvalidUTF8Error will be returned
    44	// if an invalid UTF-8 sequence is encountered.
    45	// The angle brackets "<" and ">" are escaped to "\u003c" and "\u003e"
    46	// to keep some browsers from misinterpreting JSON output as HTML.
    47	// Ampersand "&" is also escaped to "\u0026" for the same reason.
    48	//
    49	// Array and slice values encode as JSON arrays, except that
    50	// []byte encodes as a base64-encoded string, and a nil slice
    51	// encodes as the null JSON object.
    52	//
    53	// Struct values encode as JSON objects. Each exported struct field
    54	// becomes a member of the object unless
    55	//   - the field's tag is "-", or
    56	//   - the field is empty and its tag specifies the "omitempty" option.
    57	// The empty values are false, 0, any
    58	// nil pointer or interface value, and any array, slice, map, or string of
    59	// length zero. The object's default key string is the struct field name
    60	// but can be specified in the struct field's tag value. The "json" key in
    61	// the struct field's tag value is the key name, followed by an optional comma
    62	// and options. Examples:
    63	//
    64	//   // Field is ignored by this package.
    65	//   Field int `json:"-"`
    66	//
    67	//   // Field appears in JSON as key "myName".
    68	//   Field int `json:"myName"`
    69	//
    70	//   // Field appears in JSON as key "myName" and
    71	//   // the field is omitted from the object if its value is empty,
    72	//   // as defined above.
    73	//   Field int `json:"myName,omitempty"`
    74	//
    75	//   // Field appears in JSON as key "Field" (the default), but
    76	//   // the field is skipped if empty.
    77	//   // Note the leading comma.
    78	//   Field int `json:",omitempty"`
    79	//
    80	// The "string" option signals that a field is stored as JSON inside a
    81	// JSON-encoded string. It applies only to fields of string, floating point,
    82	// or integer types. This extra level of encoding is sometimes used when
    83	// communicating with JavaScript programs:
    84	//
    85	//    Int64String int64 `json:",string"`
    86	//
    87	// The key name will be used if it's a non-empty string consisting of
    88	// only Unicode letters, digits, dollar signs, percent signs, hyphens,
    89	// underscores and slashes.
    90	//
    91	// Anonymous struct fields are usually marshaled as if their inner exported fields
    92	// were fields in the outer struct, subject to the usual Go visibility rules amended
    93	// as described in the next paragraph.
    94	// An anonymous struct field with a name given in its JSON tag is treated as
    95	// having that name, rather than being anonymous.
    96	//
    97	// The Go visibility rules for struct fields are amended for JSON when
    98	// deciding which field to marshal or unmarshal. If there are
    99	// multiple fields at the same level, and that level is the least
   100	// nested (and would therefore be the nesting level selected by the
   101	// usual Go rules), the following extra rules apply:
   102	//
   103	// 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
   104	// even if there are multiple untagged fields that would otherwise conflict.
   105	// 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
   106	// 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
   107	//
   108	// Handling of anonymous struct fields is new in Go 1.1.
   109	// Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
   110	// an anonymous struct field in both current and earlier versions, give the field
   111	// a JSON tag of "-".
   112	//
   113	// Map values encode as JSON objects.
   114	// The map's key type must be string; the object keys are used directly
   115	// as map keys.
   116	//
   117	// Pointer values encode as the value pointed to.
   118	// A nil pointer encodes as the null JSON object.
   119	//
   120	// Interface values encode as the value contained in the interface.
   121	// A nil interface value encodes as the null JSON object.
   122	//
   123	// Channel, complex, and function values cannot be encoded in JSON.
   124	// Attempting to encode such a value causes Marshal to return
   125	// an UnsupportedTypeError.
   126	//
   127	// JSON cannot represent cyclic data structures and Marshal does not
   128	// handle them.  Passing cyclic structures to Marshal will result in
   129	// an infinite recursion.
   130	//
   131	func Marshal(v interface{}) ([]byte, error) {
   132		e := &encodeState{}
   133		err := e.marshal(v)
   134		if err != nil {
   135			return nil, err
   136		}
   137		return e.Bytes(), nil
   138	}
   139	
   140	// MarshalIndent is like Marshal but applies Indent to format the output.
   141	func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
   142		b, err := Marshal(v)
   143		if err != nil {
   144			return nil, err
   145		}
   146		var buf bytes.Buffer
   147		err = Indent(&buf, b, prefix, indent)
   148		if err != nil {
   149			return nil, err
   150		}
   151		return buf.Bytes(), nil
   152	}
   153	
   154	// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
   155	// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
   156	// so that the JSON will be safe to embed inside HTML <script> tags.
   157	// For historical reasons, web browsers don't honor standard HTML
   158	// escaping within <script> tags, so an alternative JSON encoding must
   159	// be used.
   160	func HTMLEscape(dst *bytes.Buffer, src []byte) {
   161		// The characters can only appear in string literals,
   162		// so just scan the string one byte at a time.
   163		start := 0
   164		for i, c := range src {
   165			if c == '<' || c == '>' || c == '&' {
   166				if start < i {
   167					dst.Write(src[start:i])
   168				}
   169				dst.WriteString(`\u00`)
   170				dst.WriteByte(hex[c>>4])
   171				dst.WriteByte(hex[c&0xF])
   172				start = i + 1
   173			}
   174			// Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
   175			if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
   176				if start < i {
   177					dst.Write(src[start:i])
   178				}
   179				dst.WriteString(`\u202`)
   180				dst.WriteByte(hex[src[i+2]&0xF])
   181				start = i + 3
   182			}
   183		}
   184		if start < len(src) {
   185			dst.Write(src[start:])
   186		}
   187	}
   188	
   189	// Marshaler is the interface implemented by objects that
   190	// can marshal themselves into valid JSON.
   191	type Marshaler interface {
   192		MarshalJSON() ([]byte, error)
   193	}
   194	
   195	// An UnsupportedTypeError is returned by Marshal when attempting
   196	// to encode an unsupported value type.
   197	type UnsupportedTypeError struct {
   198		Type reflect.Type
   199	}
   200	
   201	func (e *UnsupportedTypeError) Error() string {
   202		return "json: unsupported type: " + e.Type.String()
   203	}
   204	
   205	type UnsupportedValueError struct {
   206		Value reflect.Value
   207		Str   string
   208	}
   209	
   210	func (e *UnsupportedValueError) Error() string {
   211		return "json: unsupported value: " + e.Str
   212	}
   213	
   214	// Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
   215	// attempting to encode a string value with invalid UTF-8 sequences.
   216	// As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
   217	// replacing invalid bytes with the Unicode replacement rune U+FFFD.
   218	// This error is no longer generated but is kept for backwards compatibility
   219	// with programs that might mention it.
   220	type InvalidUTF8Error struct {
   221		S string // the whole string value that caused the error
   222	}
   223	
   224	func (e *InvalidUTF8Error) Error() string {
   225		return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
   226	}
   227	
   228	type MarshalerError struct {
   229		Type reflect.Type
   230		Err  error
   231	}
   232	
   233	func (e *MarshalerError) Error() string {
   234		return "json: error calling MarshalJSON for type " + e.Type.String() + ": " + e.Err.Error()
   235	}
   236	
   237	var hex = "0123456789abcdef"
   238	
   239	// An encodeState encodes JSON into a bytes.Buffer.
   240	type encodeState struct {
   241		bytes.Buffer // accumulated output
   242		scratch      [64]byte
   243	}
   244	
   245	var encodeStatePool sync.Pool
   246	
   247	func newEncodeState() *encodeState {
   248		if v := encodeStatePool.Get(); v != nil {
   249			e := v.(*encodeState)
   250			e.Reset()
   251			return e
   252		}
   253		return new(encodeState)
   254	}
   255	
   256	func (e *encodeState) marshal(v interface{}) (err error) {
   257		defer func() {
   258			if r := recover(); r != nil {
   259				if _, ok := r.(runtime.Error); ok {
   260					panic(r)
   261				}
   262				if s, ok := r.(string); ok {
   263					panic(s)
   264				}
   265				err = r.(error)
   266			}
   267		}()
   268		e.reflectValue(reflect.ValueOf(v))
   269		return nil
   270	}
   271	
   272	func (e *encodeState) error(err error) {
   273		panic(err)
   274	}
   275	
   276	var byteSliceType = reflect.TypeOf([]byte(nil))
   277	
   278	func isEmptyValue(v reflect.Value) bool {
   279		switch v.Kind() {
   280		case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
   281			return v.Len() == 0
   282		case reflect.Bool:
   283			return !v.Bool()
   284		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   285			return v.Int() == 0
   286		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   287			return v.Uint() == 0
   288		case reflect.Float32, reflect.Float64:
   289			return v.Float() == 0
   290		case reflect.Interface, reflect.Ptr:
   291			return v.IsNil()
   292		}
   293		return false
   294	}
   295	
   296	func (e *encodeState) reflectValue(v reflect.Value) {
   297		valueEncoder(v)(e, v, false)
   298	}
   299	
   300	type encoderFunc func(e *encodeState, v reflect.Value, quoted bool)
   301	
   302	var encoderCache struct {
   303		sync.RWMutex
   304		m map[reflect.Type]encoderFunc
   305	}
   306	
   307	func valueEncoder(v reflect.Value) encoderFunc {
   308		if !v.IsValid() {
   309			return invalidValueEncoder
   310		}
   311		return typeEncoder(v.Type())
   312	}
   313	
   314	func typeEncoder(t reflect.Type) encoderFunc {
   315		encoderCache.RLock()
   316		f := encoderCache.m[t]
   317		encoderCache.RUnlock()
   318		if f != nil {
   319			return f
   320		}
   321	
   322		// To deal with recursive types, populate the map with an
   323		// indirect func before we build it. This type waits on the
   324		// real func (f) to be ready and then calls it.  This indirect
   325		// func is only used for recursive types.
   326		encoderCache.Lock()
   327		if encoderCache.m == nil {
   328			encoderCache.m = make(map[reflect.Type]encoderFunc)
   329		}
   330		var wg sync.WaitGroup
   331		wg.Add(1)
   332		encoderCache.m[t] = func(e *encodeState, v reflect.Value, quoted bool) {
   333			wg.Wait()
   334			f(e, v, quoted)
   335		}
   336		encoderCache.Unlock()
   337	
   338		// Compute fields without lock.
   339		// Might duplicate effort but won't hold other computations back.
   340		f = newTypeEncoder(t, true)
   341		wg.Done()
   342		encoderCache.Lock()
   343		encoderCache.m[t] = f
   344		encoderCache.Unlock()
   345		return f
   346	}
   347	
   348	var (
   349		marshalerType     = reflect.TypeOf(new(Marshaler)).Elem()
   350		textMarshalerType = reflect.TypeOf(new(encoding.TextMarshaler)).Elem()
   351	)
   352	
   353	// newTypeEncoder constructs an encoderFunc for a type.
   354	// The returned encoder only checks CanAddr when allowAddr is true.
   355	func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
   356		if t.Implements(marshalerType) {
   357			return marshalerEncoder
   358		}
   359		if t.Kind() != reflect.Ptr && allowAddr {
   360			if reflect.PtrTo(t).Implements(marshalerType) {
   361				return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
   362			}
   363		}
   364	
   365		if t.Implements(textMarshalerType) {
   366			return textMarshalerEncoder
   367		}
   368		if t.Kind() != reflect.Ptr && allowAddr {
   369			if reflect.PtrTo(t).Implements(textMarshalerType) {
   370				return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
   371			}
   372		}
   373	
   374		switch t.Kind() {
   375		case reflect.Bool:
   376			return boolEncoder
   377		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   378			return intEncoder
   379		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   380			return uintEncoder
   381		case reflect.Float32:
   382			return float32Encoder
   383		case reflect.Float64:
   384			return float64Encoder
   385		case reflect.String:
   386			return stringEncoder
   387		case reflect.Interface:
   388			return interfaceEncoder
   389		case reflect.Struct:
   390			return newStructEncoder(t)
   391		case reflect.Map:
   392			return newMapEncoder(t)
   393		case reflect.Slice:
   394			return newSliceEncoder(t)
   395		case reflect.Array:
   396			return newArrayEncoder(t)
   397		case reflect.Ptr:
   398			return newPtrEncoder(t)
   399		default:
   400			return unsupportedTypeEncoder
   401		}
   402	}
   403	
   404	func invalidValueEncoder(e *encodeState, v reflect.Value, quoted bool) {
   405		e.WriteString("null")
   406	}
   407	
   408	func marshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   409		if v.Kind() == reflect.Ptr && v.IsNil() {
   410			e.WriteString("null")
   411			return
   412		}
   413		m := v.Interface().(Marshaler)
   414		b, err := m.MarshalJSON()
   415		if err == nil {
   416			// copy JSON into buffer, checking validity.
   417			err = compact(&e.Buffer, b, true)
   418		}
   419		if err != nil {
   420			e.error(&MarshalerError{v.Type(), err})
   421		}
   422	}
   423	
   424	func addrMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   425		va := v.Addr()
   426		if va.IsNil() {
   427			e.WriteString("null")
   428			return
   429		}
   430		m := va.Interface().(Marshaler)
   431		b, err := m.MarshalJSON()
   432		if err == nil {
   433			// copy JSON into buffer, checking validity.
   434			err = compact(&e.Buffer, b, true)
   435		}
   436		if err != nil {
   437			e.error(&MarshalerError{v.Type(), err})
   438		}
   439	}
   440	
   441	func textMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   442		if v.Kind() == reflect.Ptr && v.IsNil() {
   443			e.WriteString("null")
   444			return
   445		}
   446		m := v.Interface().(encoding.TextMarshaler)
   447		b, err := m.MarshalText()
   448		if err == nil {
   449			_, err = e.stringBytes(b)
   450		}
   451		if err != nil {
   452			e.error(&MarshalerError{v.Type(), err})
   453		}
   454	}
   455	
   456	func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, quoted bool) {
   457		va := v.Addr()
   458		if va.IsNil() {
   459			e.WriteString("null")
   460			return
   461		}
   462		m := va.Interface().(encoding.TextMarshaler)
   463		b, err := m.MarshalText()
   464		if err == nil {
   465			_, err = e.stringBytes(b)
   466		}
   467		if err != nil {
   468			e.error(&MarshalerError{v.Type(), err})
   469		}
   470	}
   471	
   472	func boolEncoder(e *encodeState, v reflect.Value, quoted bool) {
   473		if quoted {
   474			e.WriteByte('"')
   475		}
   476		if v.Bool() {
   477			e.WriteString("true")
   478		} else {
   479			e.WriteString("false")
   480		}
   481		if quoted {
   482			e.WriteByte('"')
   483		}
   484	}
   485	
   486	func intEncoder(e *encodeState, v reflect.Value, quoted bool) {
   487		b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
   488		if quoted {
   489			e.WriteByte('"')
   490		}
   491		e.Write(b)
   492		if quoted {
   493			e.WriteByte('"')
   494		}
   495	}
   496	
   497	func uintEncoder(e *encodeState, v reflect.Value, quoted bool) {
   498		b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
   499		if quoted {
   500			e.WriteByte('"')
   501		}
   502		e.Write(b)
   503		if quoted {
   504			e.WriteByte('"')
   505		}
   506	}
   507	
   508	type floatEncoder int // number of bits
   509	
   510	func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   511		f := v.Float()
   512		if math.IsInf(f, 0) || math.IsNaN(f) {
   513			e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
   514		}
   515		b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
   516		if quoted {
   517			e.WriteByte('"')
   518		}
   519		e.Write(b)
   520		if quoted {
   521			e.WriteByte('"')
   522		}
   523	}
   524	
   525	var (
   526		float32Encoder = (floatEncoder(32)).encode
   527		float64Encoder = (floatEncoder(64)).encode
   528	)
   529	
   530	func stringEncoder(e *encodeState, v reflect.Value, quoted bool) {
   531		if v.Type() == numberType {
   532			numStr := v.String()
   533			if numStr == "" {
   534				numStr = "0" // Number's zero-val
   535			}
   536			e.WriteString(numStr)
   537			return
   538		}
   539		if quoted {
   540			sb, err := Marshal(v.String())
   541			if err != nil {
   542				e.error(err)
   543			}
   544			e.string(string(sb))
   545		} else {
   546			e.string(v.String())
   547		}
   548	}
   549	
   550	func interfaceEncoder(e *encodeState, v reflect.Value, quoted bool) {
   551		if v.IsNil() {
   552			e.WriteString("null")
   553			return
   554		}
   555		e.reflectValue(v.Elem())
   556	}
   557	
   558	func unsupportedTypeEncoder(e *encodeState, v reflect.Value, quoted bool) {
   559		e.error(&UnsupportedTypeError{v.Type()})
   560	}
   561	
   562	type structEncoder struct {
   563		fields    []field
   564		fieldEncs []encoderFunc
   565	}
   566	
   567	func (se *structEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   568		e.WriteByte('{')
   569		first := true
   570		for i, f := range se.fields {
   571			fv := fieldByIndex(v, f.index)
   572			if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) {
   573				continue
   574			}
   575			if first {
   576				first = false
   577			} else {
   578				e.WriteByte(',')
   579			}
   580			e.string(f.name)
   581			e.WriteByte(':')
   582			se.fieldEncs[i](e, fv, f.quoted)
   583		}
   584		e.WriteByte('}')
   585	}
   586	
   587	func newStructEncoder(t reflect.Type) encoderFunc {
   588		fields := cachedTypeFields(t)
   589		se := &structEncoder{
   590			fields:    fields,
   591			fieldEncs: make([]encoderFunc, len(fields)),
   592		}
   593		for i, f := range fields {
   594			se.fieldEncs[i] = typeEncoder(typeByIndex(t, f.index))
   595		}
   596		return se.encode
   597	}
   598	
   599	type mapEncoder struct {
   600		elemEnc encoderFunc
   601	}
   602	
   603	func (me *mapEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   604		if v.IsNil() {
   605			e.WriteString("null")
   606			return
   607		}
   608		e.WriteByte('{')
   609		var sv stringValues = v.MapKeys()
   610		sort.Sort(sv)
   611		for i, k := range sv {
   612			if i > 0 {
   613				e.WriteByte(',')
   614			}
   615			e.string(k.String())
   616			e.WriteByte(':')
   617			me.elemEnc(e, v.MapIndex(k), false)
   618		}
   619		e.WriteByte('}')
   620	}
   621	
   622	func newMapEncoder(t reflect.Type) encoderFunc {
   623		if t.Key().Kind() != reflect.String {
   624			return unsupportedTypeEncoder
   625		}
   626		me := &mapEncoder{typeEncoder(t.Elem())}
   627		return me.encode
   628	}
   629	
   630	func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) {
   631		if v.IsNil() {
   632			e.WriteString("null")
   633			return
   634		}
   635		s := v.Bytes()
   636		e.WriteByte('"')
   637		if len(s) < 1024 {
   638			// for small buffers, using Encode directly is much faster.
   639			dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
   640			base64.StdEncoding.Encode(dst, s)
   641			e.Write(dst)
   642		} else {
   643			// for large buffers, avoid unnecessary extra temporary
   644			// buffer space.
   645			enc := base64.NewEncoder(base64.StdEncoding, e)
   646			enc.Write(s)
   647			enc.Close()
   648		}
   649		e.WriteByte('"')
   650	}
   651	
   652	// sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
   653	type sliceEncoder struct {
   654		arrayEnc encoderFunc
   655	}
   656	
   657	func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   658		if v.IsNil() {
   659			e.WriteString("null")
   660			return
   661		}
   662		se.arrayEnc(e, v, false)
   663	}
   664	
   665	func newSliceEncoder(t reflect.Type) encoderFunc {
   666		// Byte slices get special treatment; arrays don't.
   667		if t.Elem().Kind() == reflect.Uint8 {
   668			return encodeByteSlice
   669		}
   670		enc := &sliceEncoder{newArrayEncoder(t)}
   671		return enc.encode
   672	}
   673	
   674	type arrayEncoder struct {
   675		elemEnc encoderFunc
   676	}
   677	
   678	func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   679		e.WriteByte('[')
   680		n := v.Len()
   681		for i := 0; i < n; i++ {
   682			if i > 0 {
   683				e.WriteByte(',')
   684			}
   685			ae.elemEnc(e, v.Index(i), false)
   686		}
   687		e.WriteByte(']')
   688	}
   689	
   690	func newArrayEncoder(t reflect.Type) encoderFunc {
   691		enc := &arrayEncoder{typeEncoder(t.Elem())}
   692		return enc.encode
   693	}
   694	
   695	type ptrEncoder struct {
   696		elemEnc encoderFunc
   697	}
   698	
   699	func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, _ bool) {
   700		if v.IsNil() {
   701			e.WriteString("null")
   702			return
   703		}
   704		pe.elemEnc(e, v.Elem(), false)
   705	}
   706	
   707	func newPtrEncoder(t reflect.Type) encoderFunc {
   708		enc := &ptrEncoder{typeEncoder(t.Elem())}
   709		return enc.encode
   710	}
   711	
   712	type condAddrEncoder struct {
   713		canAddrEnc, elseEnc encoderFunc
   714	}
   715	
   716	func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
   717		if v.CanAddr() {
   718			ce.canAddrEnc(e, v, quoted)
   719		} else {
   720			ce.elseEnc(e, v, quoted)
   721		}
   722	}
   723	
   724	// newCondAddrEncoder returns an encoder that checks whether its value
   725	// CanAddr and delegates to canAddrEnc if so, else to elseEnc.
   726	func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
   727		enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
   728		return enc.encode
   729	}
   730	
   731	func isValidTag(s string) bool {
   732		if s == "" {
   733			return false
   734		}
   735		for _, c := range s {
   736			switch {
   737			case strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", c):
   738				// Backslash and quote chars are reserved, but
   739				// otherwise any punctuation chars are allowed
   740				// in a tag name.
   741			default:
   742				if !unicode.IsLetter(c) && !unicode.IsDigit(c) {
   743					return false
   744				}
   745			}
   746		}
   747		return true
   748	}
   749	
   750	func fieldByIndex(v reflect.Value, index []int) reflect.Value {
   751		for _, i := range index {
   752			if v.Kind() == reflect.Ptr {
   753				if v.IsNil() {
   754					return reflect.Value{}
   755				}
   756				v = v.Elem()
   757			}
   758			v = v.Field(i)
   759		}
   760		return v
   761	}
   762	
   763	func typeByIndex(t reflect.Type, index []int) reflect.Type {
   764		for _, i := range index {
   765			if t.Kind() == reflect.Ptr {
   766				t = t.Elem()
   767			}
   768			t = t.Field(i).Type
   769		}
   770		return t
   771	}
   772	
   773	// stringValues is a slice of reflect.Value holding *reflect.StringValue.
   774	// It implements the methods to sort by string.
   775	type stringValues []reflect.Value
   776	
   777	func (sv stringValues) Len() int           { return len(sv) }
   778	func (sv stringValues) Swap(i, j int)      { sv[i], sv[j] = sv[j], sv[i] }
   779	func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
   780	func (sv stringValues) get(i int) string   { return sv[i].String() }
   781	
   782	// NOTE: keep in sync with stringBytes below.
   783	func (e *encodeState) string(s string) (int, error) {
   784		len0 := e.Len()
   785		e.WriteByte('"')
   786		start := 0
   787		for i := 0; i < len(s); {
   788			if b := s[i]; b < utf8.RuneSelf {
   789				if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   790					i++
   791					continue
   792				}
   793				if start < i {
   794					e.WriteString(s[start:i])
   795				}
   796				switch b {
   797				case '\\', '"':
   798					e.WriteByte('\\')
   799					e.WriteByte(b)
   800				case '\n':
   801					e.WriteByte('\\')
   802					e.WriteByte('n')
   803				case '\r':
   804					e.WriteByte('\\')
   805					e.WriteByte('r')
   806				default:
   807					// This encodes bytes < 0x20 except for \n and \r,
   808					// as well as <, > and &. The latter are escaped because they
   809					// can lead to security holes when user-controlled strings
   810					// are rendered into JSON and served to some browsers.
   811					e.WriteString(`\u00`)
   812					e.WriteByte(hex[b>>4])
   813					e.WriteByte(hex[b&0xF])
   814				}
   815				i++
   816				start = i
   817				continue
   818			}
   819			c, size := utf8.DecodeRuneInString(s[i:])
   820			if c == utf8.RuneError && size == 1 {
   821				if start < i {
   822					e.WriteString(s[start:i])
   823				}
   824				e.WriteString(`\ufffd`)
   825				i += size
   826				start = i
   827				continue
   828			}
   829			// U+2028 is LINE SEPARATOR.
   830			// U+2029 is PARAGRAPH SEPARATOR.
   831			// They are both technically valid characters in JSON strings,
   832			// but don't work in JSONP, which has to be evaluated as JavaScript,
   833			// and can lead to security holes there. It is valid JSON to
   834			// escape them, so we do so unconditionally.
   835			// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
   836			if c == '\u2028' || c == '\u2029' {
   837				if start < i {
   838					e.WriteString(s[start:i])
   839				}
   840				e.WriteString(`\u202`)
   841				e.WriteByte(hex[c&0xF])
   842				i += size
   843				start = i
   844				continue
   845			}
   846			i += size
   847		}
   848		if start < len(s) {
   849			e.WriteString(s[start:])
   850		}
   851		e.WriteByte('"')
   852		return e.Len() - len0, nil
   853	}
   854	
   855	// NOTE: keep in sync with string above.
   856	func (e *encodeState) stringBytes(s []byte) (int, error) {
   857		len0 := e.Len()
   858		e.WriteByte('"')
   859		start := 0
   860		for i := 0; i < len(s); {
   861			if b := s[i]; b < utf8.RuneSelf {
   862				if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   863					i++
   864					continue
   865				}
   866				if start < i {
   867					e.Write(s[start:i])
   868				}
   869				switch b {
   870				case '\\', '"':
   871					e.WriteByte('\\')
   872					e.WriteByte(b)
   873				case '\n':
   874					e.WriteByte('\\')
   875					e.WriteByte('n')
   876				case '\r':
   877					e.WriteByte('\\')
   878					e.WriteByte('r')
   879				default:
   880					// This encodes bytes < 0x20 except for \n and \r,
   881					// as well as < and >. The latter are escaped because they
   882					// can lead to security holes when user-controlled strings
   883					// are rendered into JSON and served to some browsers.
   884					e.WriteString(`\u00`)
   885					e.WriteByte(hex[b>>4])
   886					e.WriteByte(hex[b&0xF])
   887				}
   888				i++
   889				start = i
   890				continue
   891			}
   892			c, size := utf8.DecodeRune(s[i:])
   893			if c == utf8.RuneError && size == 1 {
   894				if start < i {
   895					e.Write(s[start:i])
   896				}
   897				e.WriteString(`\ufffd`)
   898				i += size
   899				start = i
   900				continue
   901			}
   902			// U+2028 is LINE SEPARATOR.
   903			// U+2029 is PARAGRAPH SEPARATOR.
   904			// They are both technically valid characters in JSON strings,
   905			// but don't work in JSONP, which has to be evaluated as JavaScript,
   906			// and can lead to security holes there. It is valid JSON to
   907			// escape them, so we do so unconditionally.
   908			// See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
   909			if c == '\u2028' || c == '\u2029' {
   910				if start < i {
   911					e.Write(s[start:i])
   912				}
   913				e.WriteString(`\u202`)
   914				e.WriteByte(hex[c&0xF])
   915				i += size
   916				start = i
   917				continue
   918			}
   919			i += size
   920		}
   921		if start < len(s) {
   922			e.Write(s[start:])
   923		}
   924		e.WriteByte('"')
   925		return e.Len() - len0, nil
   926	}
   927	
   928	// A field represents a single field found in a struct.
   929	type field struct {
   930		name      string
   931		nameBytes []byte                 // []byte(name)
   932		equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
   933	
   934		tag       bool
   935		index     []int
   936		typ       reflect.Type
   937		omitEmpty bool
   938		quoted    bool
   939	}
   940	
   941	func fillField(f field) field {
   942		f.nameBytes = []byte(f.name)
   943		f.equalFold = foldFunc(f.nameBytes)
   944		return f
   945	}
   946	
   947	// byName sorts field by name, breaking ties with depth,
   948	// then breaking ties with "name came from json tag", then
   949	// breaking ties with index sequence.
   950	type byName []field
   951	
   952	func (x byName) Len() int { return len(x) }
   953	
   954	func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   955	
   956	func (x byName) Less(i, j int) bool {
   957		if x[i].name != x[j].name {
   958			return x[i].name < x[j].name
   959		}
   960		if len(x[i].index) != len(x[j].index) {
   961			return len(x[i].index) < len(x[j].index)
   962		}
   963		if x[i].tag != x[j].tag {
   964			return x[i].tag
   965		}
   966		return byIndex(x).Less(i, j)
   967	}
   968	
   969	// byIndex sorts field by index sequence.
   970	type byIndex []field
   971	
   972	func (x byIndex) Len() int { return len(x) }
   973	
   974	func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
   975	
   976	func (x byIndex) Less(i, j int) bool {
   977		for k, xik := range x[i].index {
   978			if k >= len(x[j].index) {
   979				return false
   980			}
   981			if xik != x[j].index[k] {
   982				return xik < x[j].index[k]
   983			}
   984		}
   985		return len(x[i].index) < len(x[j].index)
   986	}
   987	
   988	// typeFields returns a list of fields that JSON should recognize for the given type.
   989	// The algorithm is breadth-first search over the set of structs to include - the top struct
   990	// and then any reachable anonymous structs.
   991	func typeFields(t reflect.Type) []field {
   992		// Anonymous fields to explore at the current level and the next.
   993		current := []field{}
   994		next := []field{{typ: t}}
   995	
   996		// Count of queued names for current level and the next.
   997		count := map[reflect.Type]int{}
   998		nextCount := map[reflect.Type]int{}
   999	
  1000		// Types already visited at an earlier level.
  1001		visited := map[reflect.Type]bool{}
  1002	
  1003		// Fields found.
  1004		var fields []field
  1005	
  1006		for len(next) > 0 {
  1007			current, next = next, current[:0]
  1008			count, nextCount = nextCount, map[reflect.Type]int{}
  1009	
  1010			for _, f := range current {
  1011				if visited[f.typ] {
  1012					continue
  1013				}
  1014				visited[f.typ] = true
  1015	
  1016				// Scan f.typ for fields to include.
  1017				for i := 0; i < f.typ.NumField(); i++ {
  1018					sf := f.typ.Field(i)
  1019					if sf.PkgPath != "" { // unexported
  1020						continue
  1021					}
  1022					tag := sf.Tag.Get("json")
  1023					if tag == "-" {
  1024						continue
  1025					}
  1026					name, opts := parseTag(tag)
  1027					if !isValidTag(name) {
  1028						name = ""
  1029					}
  1030					index := make([]int, len(f.index)+1)
  1031					copy(index, f.index)
  1032					index[len(f.index)] = i
  1033	
  1034					ft := sf.Type
  1035					if ft.Name() == "" && ft.Kind() == reflect.Ptr {
  1036						// Follow pointer.
  1037						ft = ft.Elem()
  1038					}
  1039	
  1040					// Record found field and index sequence.
  1041					if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
  1042						tagged := name != ""
  1043						if name == "" {
  1044							name = sf.Name
  1045						}
  1046						fields = append(fields, fillField(field{
  1047							name:      name,
  1048							tag:       tagged,
  1049							index:     index,
  1050							typ:       ft,
  1051							omitEmpty: opts.Contains("omitempty"),
  1052							quoted:    opts.Contains("string"),
  1053						}))
  1054						if count[f.typ] > 1 {
  1055							// If there were multiple instances, add a second,
  1056							// so that the annihilation code will see a duplicate.
  1057							// It only cares about the distinction between 1 or 2,
  1058							// so don't bother generating any more copies.
  1059							fields = append(fields, fields[len(fields)-1])
  1060						}
  1061						continue
  1062					}
  1063	
  1064					// Record new anonymous struct to explore in next round.
  1065					nextCount[ft]++
  1066					if nextCount[ft] == 1 {
  1067						next = append(next, fillField(field{name: ft.Name(), index: index, typ: ft}))
  1068					}
  1069				}
  1070			}
  1071		}
  1072	
  1073		sort.Sort(byName(fields))
  1074	
  1075		// Delete all fields that are hidden by the Go rules for embedded fields,
  1076		// except that fields with JSON tags are promoted.
  1077	
  1078		// The fields are sorted in primary order of name, secondary order
  1079		// of field index length. Loop over names; for each name, delete
  1080		// hidden fields by choosing the one dominant field that survives.
  1081		out := fields[:0]
  1082		for advance, i := 0, 0; i < len(fields); i += advance {
  1083			// One iteration per name.
  1084			// Find the sequence of fields with the name of this first field.
  1085			fi := fields[i]
  1086			name := fi.name
  1087			for advance = 1; i+advance < len(fields); advance++ {
  1088				fj := fields[i+advance]
  1089				if fj.name != name {
  1090					break
  1091				}
  1092			}
  1093			if advance == 1 { // Only one field with this name
  1094				out = append(out, fi)
  1095				continue
  1096			}
  1097			dominant, ok := dominantField(fields[i : i+advance])
  1098			if ok {
  1099				out = append(out, dominant)
  1100			}
  1101		}
  1102	
  1103		fields = out
  1104		sort.Sort(byIndex(fields))
  1105	
  1106		return fields
  1107	}
  1108	
  1109	// dominantField looks through the fields, all of which are known to
  1110	// have the same name, to find the single field that dominates the
  1111	// others using Go's embedding rules, modified by the presence of
  1112	// JSON tags. If there are multiple top-level fields, the boolean
  1113	// will be false: This condition is an error in Go and we skip all
  1114	// the fields.
  1115	func dominantField(fields []field) (field, bool) {
  1116		// The fields are sorted in increasing index-length order. The winner
  1117		// must therefore be one with the shortest index length. Drop all
  1118		// longer entries, which is easy: just truncate the slice.
  1119		length := len(fields[0].index)
  1120		tagged := -1 // Index of first tagged field.
  1121		for i, f := range fields {
  1122			if len(f.index) > length {
  1123				fields = fields[:i]
  1124				break
  1125			}
  1126			if f.tag {
  1127				if tagged >= 0 {
  1128					// Multiple tagged fields at the same level: conflict.
  1129					// Return no field.
  1130					return field{}, false
  1131				}
  1132				tagged = i
  1133			}
  1134		}
  1135		if tagged >= 0 {
  1136			return fields[tagged], true
  1137		}
  1138		// All remaining fields have the same length. If there's more than one,
  1139		// we have a conflict (two fields named "X" at the same level) and we
  1140		// return no field.
  1141		if len(fields) > 1 {
  1142			return field{}, false
  1143		}
  1144		return fields[0], true
  1145	}
  1146	
  1147	var fieldCache struct {
  1148		sync.RWMutex
  1149		m map[reflect.Type][]field
  1150	}
  1151	
  1152	// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
  1153	func cachedTypeFields(t reflect.Type) []field {
  1154		fieldCache.RLock()
  1155		f := fieldCache.m[t]
  1156		fieldCache.RUnlock()
  1157		if f != nil {
  1158			return f
  1159		}
  1160	
  1161		// Compute fields without lock.
  1162		// Might duplicate effort but won't hold other computations back.
  1163		f = typeFields(t)
  1164		if f == nil {
  1165			f = []field{}
  1166		}
  1167	
  1168		fieldCache.Lock()
  1169		if fieldCache.m == nil {
  1170			fieldCache.m = map[reflect.Type][]field{}
  1171		}
  1172		fieldCache.m[t] = f
  1173		fieldCache.Unlock()
  1174		return f
  1175	}
  1176	

View as plain text