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