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