encode.go 30 KB

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