encode.go 31 KB

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