encode.go 32 KB

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