You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1199 lines
31 KiB

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