encode.go 33 KB

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