encode.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 7159. 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. "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. If no MarshalJSON method is present but the
  31. // value implements encoding.TextMarshaler instead, Marshal calls
  32. // its MarshalText method and encodes the result as a JSON string.
  33. // The nil pointer exception is not strictly necessary
  34. // but mimics a similar, necessary exception in the behavior of
  35. // UnmarshalJSON.
  36. //
  37. // Otherwise, Marshal uses the following type-dependent default encodings:
  38. //
  39. // Boolean values encode as JSON booleans.
  40. //
  41. // Floating point, integer, and Number values encode as JSON numbers.
  42. //
  43. // String values encode as JSON strings coerced to valid UTF-8,
  44. // replacing invalid bytes with the Unicode replacement rune.
  45. // So that the JSON will be safe to embed inside HTML <script> tags,
  46. // the string is encoded using HTMLEscape,
  47. // which replaces "<", ">", "&", U+2028, and U+2029 are escaped
  48. // to "\u003c","\u003e", "\u0026", "\u2028", and "\u2029".
  49. // This replacement can be disabled when using an Encoder,
  50. // by calling SetEscapeHTML(false).
  51. //
  52. // Array and slice values encode as JSON arrays, except that
  53. // []byte encodes as a base64-encoded string, and a nil slice
  54. // encodes as the null JSON value.
  55. //
  56. // Struct values encode as JSON objects.
  57. // Each exported struct field becomes a member of the object, using the
  58. // field name as the object key, unless the field is omitted for one of the
  59. // reasons given below.
  60. //
  61. // The encoding of each struct field can be customized by the format string
  62. // stored under the "json" key in the struct field's tag.
  63. // The format string gives the name of the field, possibly followed by a
  64. // comma-separated list of options. The name may be empty in order to
  65. // specify options without overriding the default field name.
  66. //
  67. // The "omitempty" option specifies that the field should be omitted
  68. // from the encoding if the field has an empty value, defined as
  69. // false, 0, a nil pointer, a nil interface value, and any empty array,
  70. // slice, map, or string.
  71. //
  72. // As a special case, if the field tag is "-", the field is always omitted.
  73. // Note that a field with name "-" can still be generated using the tag "-,".
  74. //
  75. // Examples of struct field tags and their meanings:
  76. //
  77. // // Field appears in JSON as key "myName".
  78. // Field int `json:"myName"`
  79. //
  80. // // Field appears in JSON as key "myName" and
  81. // // the field is omitted from the object if its value is empty,
  82. // // as defined above.
  83. // Field int `json:"myName,omitempty"`
  84. //
  85. // // Field appears in JSON as key "Field" (the default), but
  86. // // the field is skipped if empty.
  87. // // Note the leading comma.
  88. // Field int `json:",omitempty"`
  89. //
  90. // // Field is ignored by this package.
  91. // Field int `json:"-"`
  92. //
  93. // // Field appears in JSON as key "-".
  94. // Field int `json:"-,"`
  95. //
  96. // The "string" option signals that a field is stored as JSON inside a
  97. // JSON-encoded string. It applies only to fields of string, floating point,
  98. // integer, or boolean types. This extra level of encoding is sometimes used
  99. // when communicating with JavaScript programs:
  100. //
  101. // Int64String int64 `json:",string"`
  102. //
  103. // The key name will be used if it's a non-empty string consisting of
  104. // only Unicode letters, digits, and ASCII punctuation except quotation
  105. // marks, backslash, and comma.
  106. //
  107. // Anonymous struct fields are usually marshaled as if their inner exported fields
  108. // were fields in the outer struct, subject to the usual Go visibility rules amended
  109. // as described in the next paragraph.
  110. // An anonymous struct field with a name given in its JSON tag is treated as
  111. // having that name, rather than being anonymous.
  112. // An anonymous struct field of interface type is treated the same as having
  113. // that type as its name, rather than being anonymous.
  114. //
  115. // The Go visibility rules for struct fields are amended for JSON when
  116. // deciding which field to marshal or unmarshal. If there are
  117. // multiple fields at the same level, and that level is the least
  118. // nested (and would therefore be the nesting level selected by the
  119. // usual Go rules), the following extra rules apply:
  120. //
  121. // 1) Of those fields, if any are JSON-tagged, only tagged fields are considered,
  122. // even if there are multiple untagged fields that would otherwise conflict.
  123. //
  124. // 2) If there is exactly one field (tagged or not according to the first rule), that is selected.
  125. //
  126. // 3) Otherwise there are multiple fields, and all are ignored; no error occurs.
  127. //
  128. // Handling of anonymous struct fields is new in Go 1.1.
  129. // Prior to Go 1.1, anonymous struct fields were ignored. To force ignoring of
  130. // an anonymous struct field in both current and earlier versions, give the field
  131. // a JSON tag of "-".
  132. //
  133. // Map values encode as JSON objects. The map's key type must either be a
  134. // string, an integer type, or implement encoding.TextMarshaler. The map keys
  135. // are sorted and used as JSON object keys by applying the following rules,
  136. // subject to the UTF-8 coercion described for string values above:
  137. // - keys of any string type are used directly
  138. // - encoding.TextMarshalers are marshaled
  139. // - integer keys are converted to strings
  140. //
  141. // Pointer values encode as the value pointed to.
  142. // A nil pointer encodes as the null JSON value.
  143. //
  144. // Interface values encode as the value contained in the interface.
  145. // A nil interface value encodes as the null JSON value.
  146. //
  147. // Channel, complex, and function values cannot be encoded in JSON.
  148. // Attempting to encode such a value causes Marshal to return
  149. // an UnsupportedTypeError.
  150. //
  151. // JSON cannot represent cyclic data structures and Marshal does not
  152. // handle them. Passing cyclic structures to Marshal will result in
  153. // an error.
  154. func Marshal(v interface{}) ([]byte, error) {
  155. e := newEncodeState()
  156. err := e.marshal(v, encOpts{escapeHTML: true})
  157. if err != nil {
  158. return nil, err
  159. }
  160. buf := append([]byte(nil), e.Bytes()...)
  161. encodeStatePool.Put(e)
  162. return buf, nil
  163. }
  164. // MarshalIndent is like Marshal but applies Indent to format the output.
  165. // Each JSON element in the output will begin on a new line beginning with prefix
  166. // followed by one or more copies of indent according to the indentation nesting.
  167. func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
  168. b, err := Marshal(v)
  169. if err != nil {
  170. return nil, err
  171. }
  172. var buf bytes.Buffer
  173. err = Indent(&buf, b, prefix, indent)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return buf.Bytes(), nil
  178. }
  179. // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
  180. // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
  181. // so that the JSON will be safe to embed inside HTML <script> tags.
  182. // For historical reasons, web browsers don't honor standard HTML
  183. // escaping within <script> tags, so an alternative JSON encoding must
  184. // be used.
  185. func HTMLEscape(dst *bytes.Buffer, src []byte) {
  186. // The characters can only appear in string literals,
  187. // so just scan the string one byte at a time.
  188. start := 0
  189. for i, c := range src {
  190. if c == '<' || c == '>' || c == '&' {
  191. if start < i {
  192. dst.Write(src[start:i])
  193. }
  194. dst.WriteString(`\u00`)
  195. dst.WriteByte(hex[c>>4])
  196. dst.WriteByte(hex[c&0xF])
  197. start = i + 1
  198. }
  199. // Convert U+2028 and U+2029 (E2 80 A8 and E2 80 A9).
  200. if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
  201. if start < i {
  202. dst.Write(src[start:i])
  203. }
  204. dst.WriteString(`\u202`)
  205. dst.WriteByte(hex[src[i+2]&0xF])
  206. start = i + 3
  207. }
  208. }
  209. if start < len(src) {
  210. dst.Write(src[start:])
  211. }
  212. }
  213. // Marshaler is the interface implemented by types that
  214. // can marshal themselves into valid JSON.
  215. type Marshaler interface {
  216. MarshalJSON() ([]byte, error)
  217. }
  218. // An UnsupportedTypeError is returned by Marshal when attempting
  219. // to encode an unsupported value type.
  220. type UnsupportedTypeError struct {
  221. Type reflect.Type
  222. }
  223. func (e *UnsupportedTypeError) Error() string {
  224. return "json: unsupported type: " + e.Type.String()
  225. }
  226. // An UnsupportedValueError is returned by Marshal when attempting
  227. // to encode an unsupported value.
  228. type UnsupportedValueError struct {
  229. Value reflect.Value
  230. Str string
  231. }
  232. func (e *UnsupportedValueError) Error() string {
  233. return "json: unsupported value: " + e.Str
  234. }
  235. // Before Go 1.2, an InvalidUTF8Error was returned by Marshal when
  236. // attempting to encode a string value with invalid UTF-8 sequences.
  237. // As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by
  238. // replacing invalid bytes with the Unicode replacement rune U+FFFD.
  239. //
  240. // Deprecated: No longer used; kept for compatibility.
  241. type InvalidUTF8Error struct {
  242. S string // the whole string value that caused the error
  243. }
  244. func (e *InvalidUTF8Error) Error() string {
  245. return "json: invalid UTF-8 in string: " + strconv.Quote(e.S)
  246. }
  247. // A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.
  248. type MarshalerError struct {
  249. Type reflect.Type
  250. Err error
  251. sourceFunc string
  252. }
  253. func (e *MarshalerError) Error() string {
  254. srcFunc := e.sourceFunc
  255. if srcFunc == "" {
  256. srcFunc = "MarshalJSON"
  257. }
  258. return "json: error calling " + srcFunc +
  259. " for type " + e.Type.String() +
  260. ": " + e.Err.Error()
  261. }
  262. // Unwrap returns the underlying error.
  263. func (e *MarshalerError) Unwrap() error { return e.Err }
  264. var hex = "0123456789abcdef"
  265. // An encodeState encodes JSON into a bytes.Buffer.
  266. type encodeState struct {
  267. bytes.Buffer // accumulated output
  268. scratch [64]byte
  269. // Keep track of what pointers we've seen in the current recursive call
  270. // path, to avoid cycles that could lead to a stack overflow. Only do
  271. // the relatively expensive map operations if ptrLevel is larger than
  272. // startDetectingCyclesAfter, so that we skip the work if we're within a
  273. // reasonable amount of nested pointers deep.
  274. ptrLevel uint
  275. ptrSeen map[interface{}]struct{}
  276. // discriminatorEncodeTypeName is set to true when the type name should
  277. // be encoded along with a map or struct value. The flag is flipped back
  278. // to false as soon as the type name is encoded to prevent impacting
  279. // subsequent values.
  280. discriminatorEncodeTypeName bool
  281. }
  282. const startDetectingCyclesAfter = 1000
  283. var encodeStatePool sync.Pool
  284. func newEncodeState() *encodeState {
  285. if v := encodeStatePool.Get(); v != nil {
  286. e := v.(*encodeState)
  287. e.Reset()
  288. if len(e.ptrSeen) > 0 {
  289. panic("ptrEncoder.encode should have emptied ptrSeen via defers")
  290. }
  291. e.ptrLevel = 0
  292. e.discriminatorEncodeTypeName = false
  293. return e
  294. }
  295. return &encodeState{ptrSeen: make(map[interface{}]struct{})}
  296. }
  297. // jsonError is an error wrapper type for internal use only.
  298. // Panics with errors are wrapped in jsonError so that the top-level recover
  299. // can distinguish intentional panics from this package.
  300. type jsonError struct{ error }
  301. var interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
  302. func (e *encodeState) marshal(v interface{}, opts encOpts) (err error) {
  303. defer func() {
  304. if r := recover(); r != nil {
  305. if je, ok := r.(jsonError); ok {
  306. err = je.error
  307. } else {
  308. panic(r)
  309. }
  310. }
  311. }()
  312. val := reflect.ValueOf(v)
  313. if val.IsValid() && opts.isDiscriminatorSet() && opts.discriminatorEncodeMode.root() {
  314. val = val.Convert(interfaceType)
  315. }
  316. e.reflectValue(val, opts)
  317. return nil
  318. }
  319. // error aborts the encoding by panicking with err wrapped in jsonError.
  320. func (e *encodeState) error(err error) {
  321. panic(jsonError{err})
  322. }
  323. func isEmptyValue(v reflect.Value) bool {
  324. switch v.Kind() {
  325. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  326. return v.Len() == 0
  327. case reflect.Bool:
  328. return !v.Bool()
  329. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  330. return v.Int() == 0
  331. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  332. return v.Uint() == 0
  333. case reflect.Float32, reflect.Float64:
  334. return v.Float() == 0
  335. case reflect.Interface, reflect.Ptr:
  336. return v.IsNil()
  337. }
  338. return false
  339. }
  340. func (e *encodeState) reflectValue(v reflect.Value, opts encOpts) {
  341. valueEncoder(v)(e, v, opts)
  342. }
  343. type encOpts struct {
  344. // quoted causes primitive fields to be encoded inside JSON strings.
  345. quoted bool
  346. // escapeHTML causes '<', '>', and '&' to be escaped in JSON strings.
  347. escapeHTML bool
  348. // see Encoder.SetDiscriminator
  349. discriminatorTypeFieldName string
  350. // see Encoder.SetDiscriminator
  351. discriminatorValueFieldName string
  352. // see Encoder.SetDiscriminator
  353. discriminatorValueFn TypeToDiscriminatorFunc
  354. // see Encoder.SetDiscriminator
  355. discriminatorEncodeMode DiscriminatorEncodeMode
  356. }
  357. type encoderFunc func(e *encodeState, v reflect.Value, opts encOpts)
  358. var encoderCache sync.Map // map[reflect.Type]encoderFunc
  359. func valueEncoder(v reflect.Value) encoderFunc {
  360. if !v.IsValid() {
  361. return invalidValueEncoder
  362. }
  363. return typeEncoder(v.Type())
  364. }
  365. func typeEncoder(t reflect.Type) encoderFunc {
  366. if fi, ok := encoderCache.Load(t); ok {
  367. return fi.(encoderFunc)
  368. }
  369. // To deal with recursive types, populate the map with an
  370. // indirect func before we build it. This type waits on the
  371. // real func (f) to be ready and then calls it. This indirect
  372. // func is only used for recursive types.
  373. var (
  374. wg sync.WaitGroup
  375. f encoderFunc
  376. )
  377. wg.Add(1)
  378. fi, loaded := encoderCache.LoadOrStore(t, encoderFunc(func(e *encodeState, v reflect.Value, opts encOpts) {
  379. wg.Wait()
  380. f(e, v, opts)
  381. }))
  382. if loaded {
  383. return fi.(encoderFunc)
  384. }
  385. // Compute the real encoder and replace the indirect func with it.
  386. f = newTypeEncoder(t, true)
  387. wg.Done()
  388. encoderCache.Store(t, f)
  389. return f
  390. }
  391. var (
  392. marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
  393. textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
  394. )
  395. // newTypeEncoder constructs an encoderFunc for a type.
  396. // The returned encoder only checks CanAddr when allowAddr is true.
  397. func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
  398. // If we have a non-pointer value whose type implements
  399. // Marshaler with a value receiver, then we're better off taking
  400. // the address of the value - otherwise we end up with an
  401. // allocation as we cast the value to an interface.
  402. if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(marshalerType) {
  403. return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
  404. }
  405. if t.Implements(marshalerType) {
  406. return marshalerEncoder
  407. }
  408. if t.Kind() != reflect.Ptr && allowAddr && reflect.PtrTo(t).Implements(textMarshalerType) {
  409. return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
  410. }
  411. if t.Implements(textMarshalerType) {
  412. return textMarshalerEncoder
  413. }
  414. switch t.Kind() {
  415. case reflect.Bool:
  416. return boolEncoder
  417. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  418. return intEncoder
  419. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  420. return uintEncoder
  421. case reflect.Float32:
  422. return float32Encoder
  423. case reflect.Float64:
  424. return float64Encoder
  425. case reflect.String:
  426. return stringEncoder
  427. case reflect.Interface:
  428. return interfaceEncoder
  429. case reflect.Struct:
  430. return newStructEncoder(t)
  431. case reflect.Map:
  432. return newMapEncoder(t)
  433. case reflect.Slice:
  434. return newSliceEncoder(t)
  435. case reflect.Array:
  436. return newArrayEncoder(t)
  437. case reflect.Ptr:
  438. return newPtrEncoder(t)
  439. default:
  440. return unsupportedTypeEncoder
  441. }
  442. }
  443. func invalidValueEncoder(e *encodeState, v reflect.Value, _ encOpts) {
  444. e.WriteString("null")
  445. }
  446. func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  447. if v.Kind() == reflect.Ptr && v.IsNil() {
  448. e.WriteString("null")
  449. return
  450. }
  451. m, ok := v.Interface().(Marshaler)
  452. if !ok {
  453. e.WriteString("null")
  454. return
  455. }
  456. b, err := m.MarshalJSON()
  457. if err == nil {
  458. // copy JSON into buffer, checking validity.
  459. err = compact(&e.Buffer, b, opts.escapeHTML)
  460. }
  461. if err != nil {
  462. e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
  463. }
  464. }
  465. func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  466. va := v.Addr()
  467. if va.IsNil() {
  468. e.WriteString("null")
  469. return
  470. }
  471. m := va.Interface().(Marshaler)
  472. b, err := m.MarshalJSON()
  473. if err == nil {
  474. // copy JSON into buffer, checking validity.
  475. err = compact(&e.Buffer, b, opts.escapeHTML)
  476. }
  477. if err != nil {
  478. e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
  479. }
  480. }
  481. func textMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  482. if v.Kind() == reflect.Ptr && v.IsNil() {
  483. e.WriteString("null")
  484. return
  485. }
  486. m, ok := v.Interface().(encoding.TextMarshaler)
  487. if !ok {
  488. e.WriteString("null")
  489. return
  490. }
  491. b, err := m.MarshalText()
  492. if err != nil {
  493. e.error(&MarshalerError{v.Type(), err, "MarshalText"})
  494. }
  495. e.stringBytes(b, opts.escapeHTML)
  496. }
  497. func addrTextMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  498. va := v.Addr()
  499. if va.IsNil() {
  500. e.WriteString("null")
  501. return
  502. }
  503. m := va.Interface().(encoding.TextMarshaler)
  504. b, err := m.MarshalText()
  505. if err != nil {
  506. e.error(&MarshalerError{v.Type(), err, "MarshalText"})
  507. }
  508. e.stringBytes(b, opts.escapeHTML)
  509. }
  510. func boolEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  511. if opts.quoted {
  512. e.WriteByte('"')
  513. }
  514. if v.Bool() {
  515. e.WriteString("true")
  516. } else {
  517. e.WriteString("false")
  518. }
  519. if opts.quoted {
  520. e.WriteByte('"')
  521. }
  522. }
  523. func intEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  524. b := strconv.AppendInt(e.scratch[:0], v.Int(), 10)
  525. if opts.quoted {
  526. e.WriteByte('"')
  527. }
  528. e.Write(b)
  529. if opts.quoted {
  530. e.WriteByte('"')
  531. }
  532. }
  533. func uintEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  534. b := strconv.AppendUint(e.scratch[:0], v.Uint(), 10)
  535. if opts.quoted {
  536. e.WriteByte('"')
  537. }
  538. e.Write(b)
  539. if opts.quoted {
  540. e.WriteByte('"')
  541. }
  542. }
  543. type floatEncoder int // number of bits
  544. func (bits floatEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  545. f := v.Float()
  546. if math.IsInf(f, 0) || math.IsNaN(f) {
  547. e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
  548. }
  549. // Convert as if by ES6 number to string conversion.
  550. // This matches most other JSON generators.
  551. // See golang.org/issue/6384 and golang.org/issue/14135.
  552. // Like fmt %g, but the exponent cutoffs are different
  553. // and exponents themselves are not padded to two digits.
  554. b := e.scratch[:0]
  555. abs := math.Abs(f)
  556. fmt := byte('f')
  557. // Note: Must use float32 comparisons for underlying float32 value to get precise cutoffs right.
  558. if abs != 0 {
  559. if bits == 64 && (abs < 1e-6 || abs >= 1e21) || bits == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
  560. fmt = 'e'
  561. }
  562. }
  563. b = strconv.AppendFloat(b, f, fmt, -1, int(bits))
  564. if fmt == 'e' {
  565. // clean up e-09 to e-9
  566. n := len(b)
  567. if n >= 4 && b[n-4] == 'e' && b[n-3] == '-' && b[n-2] == '0' {
  568. b[n-2] = b[n-1]
  569. b = b[:n-1]
  570. }
  571. }
  572. if opts.quoted {
  573. e.WriteByte('"')
  574. }
  575. e.Write(b)
  576. if opts.quoted {
  577. e.WriteByte('"')
  578. }
  579. }
  580. var (
  581. float32Encoder = (floatEncoder(32)).encode
  582. float64Encoder = (floatEncoder(64)).encode
  583. )
  584. func stringEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  585. if v.Type() == numberType {
  586. numStr := v.String()
  587. // In Go1.5 the empty string encodes to "0", while this is not a valid number literal
  588. // we keep compatibility so check validity after this.
  589. if numStr == "" {
  590. numStr = "0" // Number's zero-val
  591. }
  592. if !isValidNumber(numStr) {
  593. e.error(fmt.Errorf("json: invalid number literal %q", numStr))
  594. }
  595. if opts.quoted {
  596. e.WriteByte('"')
  597. }
  598. e.WriteString(numStr)
  599. if opts.quoted {
  600. e.WriteByte('"')
  601. }
  602. return
  603. }
  604. if opts.quoted {
  605. e2 := newEncodeState()
  606. // Since we encode the string twice, we only need to escape HTML
  607. // the first time.
  608. e2.string(v.String(), opts.escapeHTML)
  609. e.stringBytes(e2.Bytes(), false)
  610. encodeStatePool.Put(e2)
  611. } else {
  612. e.string(v.String(), opts.escapeHTML)
  613. }
  614. }
  615. // isValidNumber reports whether s is a valid JSON number literal.
  616. func isValidNumber(s string) bool {
  617. // This function implements the JSON numbers grammar.
  618. // See https://tools.ietf.org/html/rfc7159#section-6
  619. // and https://www.json.org/img/number.png
  620. if s == "" {
  621. return false
  622. }
  623. // Optional -
  624. if s[0] == '-' {
  625. s = s[1:]
  626. if s == "" {
  627. return false
  628. }
  629. }
  630. // Digits
  631. switch {
  632. default:
  633. return false
  634. case s[0] == '0':
  635. s = s[1:]
  636. case '1' <= s[0] && s[0] <= '9':
  637. s = s[1:]
  638. for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
  639. s = s[1:]
  640. }
  641. }
  642. // . followed by 1 or more digits.
  643. if len(s) >= 2 && s[0] == '.' && '0' <= s[1] && s[1] <= '9' {
  644. s = s[2:]
  645. for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
  646. s = s[1:]
  647. }
  648. }
  649. // e or E followed by an optional - or + and
  650. // 1 or more digits.
  651. if len(s) >= 2 && (s[0] == 'e' || s[0] == 'E') {
  652. s = s[1:]
  653. if s[0] == '+' || s[0] == '-' {
  654. s = s[1:]
  655. if s == "" {
  656. return false
  657. }
  658. }
  659. for len(s) > 0 && '0' <= s[0] && s[0] <= '9' {
  660. s = s[1:]
  661. }
  662. }
  663. // Make sure we are at the end.
  664. return s == ""
  665. }
  666. func interfaceEncoder(e *encodeState, v reflect.Value, opts encOpts) {
  667. if v.IsNil() {
  668. e.WriteString("null")
  669. return
  670. }
  671. if opts.isDiscriminatorSet() {
  672. discriminatorInterfaceEncode(e, v, opts)
  673. return
  674. }
  675. e.reflectValue(v.Elem(), opts)
  676. }
  677. func unsupportedTypeEncoder(e *encodeState, v reflect.Value, _ encOpts) {
  678. e.error(&UnsupportedTypeError{v.Type()})
  679. }
  680. type structEncoder struct {
  681. fields structFields
  682. }
  683. type structFields struct {
  684. list []field
  685. nameIndex map[string]int
  686. }
  687. func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  688. next := byte('{')
  689. if opts.isDiscriminatorSet() {
  690. next = discriminatorStructEncode(e, v, opts)
  691. }
  692. FieldLoop:
  693. for i := range se.fields.list {
  694. f := &se.fields.list[i]
  695. // Find the nested struct field by following f.index.
  696. fv := v
  697. for _, i := range f.index {
  698. if fv.Kind() == reflect.Ptr {
  699. if fv.IsNil() {
  700. continue FieldLoop
  701. }
  702. fv = fv.Elem()
  703. }
  704. fv = fv.Field(i)
  705. }
  706. if f.omitEmpty && isEmptyValue(fv) {
  707. continue
  708. }
  709. e.WriteByte(next)
  710. next = ','
  711. if opts.escapeHTML {
  712. e.WriteString(f.nameEscHTML)
  713. } else {
  714. e.WriteString(f.nameNonEsc)
  715. }
  716. opts.quoted = f.quoted
  717. f.encoder(e, fv, opts)
  718. }
  719. if next == '{' {
  720. e.WriteString("{}")
  721. } else {
  722. e.WriteByte('}')
  723. }
  724. }
  725. func newStructEncoder(t reflect.Type) encoderFunc {
  726. se := structEncoder{fields: cachedTypeFields(t)}
  727. return se.encode
  728. }
  729. type mapEncoder struct {
  730. elemEnc encoderFunc
  731. }
  732. func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  733. if v.IsNil() {
  734. e.WriteString("null")
  735. return
  736. }
  737. if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
  738. // We're a large number of nested ptrEncoder.encode calls deep;
  739. // start checking if we've run into a pointer cycle.
  740. ptr := v.Pointer()
  741. if _, ok := e.ptrSeen[ptr]; ok {
  742. e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
  743. }
  744. e.ptrSeen[ptr] = struct{}{}
  745. defer delete(e.ptrSeen, ptr)
  746. }
  747. e.WriteByte('{')
  748. if opts.isDiscriminatorSet() {
  749. discriminatorMapEncode(e, v, opts)
  750. }
  751. // Extract and sort the keys.
  752. sv := make([]reflectWithString, v.Len())
  753. mi := v.MapRange()
  754. for i := 0; mi.Next(); i++ {
  755. sv[i].k = mi.Key()
  756. sv[i].v = mi.Value()
  757. if err := sv[i].resolve(); err != nil {
  758. e.error(fmt.Errorf("json: encoding error for type %q: %q", v.Type().String(), err.Error()))
  759. }
  760. }
  761. sort.Slice(sv, func(i, j int) bool { return sv[i].ks < sv[j].ks })
  762. for i, kv := range sv {
  763. if i > 0 {
  764. e.WriteByte(',')
  765. }
  766. e.string(kv.ks, opts.escapeHTML)
  767. e.WriteByte(':')
  768. me.elemEnc(e, kv.v, opts)
  769. }
  770. e.WriteByte('}')
  771. e.ptrLevel--
  772. }
  773. func newMapEncoder(t reflect.Type) encoderFunc {
  774. switch t.Key().Kind() {
  775. case reflect.String,
  776. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  777. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  778. default:
  779. if !t.Key().Implements(textMarshalerType) {
  780. return unsupportedTypeEncoder
  781. }
  782. }
  783. me := mapEncoder{typeEncoder(t.Elem())}
  784. return me.encode
  785. }
  786. func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) {
  787. if v.IsNil() {
  788. e.WriteString("null")
  789. return
  790. }
  791. s := v.Bytes()
  792. e.WriteByte('"')
  793. encodedLen := base64.StdEncoding.EncodedLen(len(s))
  794. if encodedLen <= len(e.scratch) {
  795. // If the encoded bytes fit in e.scratch, avoid an extra
  796. // allocation and use the cheaper Encoding.Encode.
  797. dst := e.scratch[:encodedLen]
  798. base64.StdEncoding.Encode(dst, s)
  799. e.Write(dst)
  800. } else if encodedLen <= 1024 {
  801. // The encoded bytes are short enough to allocate for, and
  802. // Encoding.Encode is still cheaper.
  803. dst := make([]byte, encodedLen)
  804. base64.StdEncoding.Encode(dst, s)
  805. e.Write(dst)
  806. } else {
  807. // The encoded bytes are too long to cheaply allocate, and
  808. // Encoding.Encode is no longer noticeably cheaper.
  809. enc := base64.NewEncoder(base64.StdEncoding, e)
  810. enc.Write(s)
  811. enc.Close()
  812. }
  813. e.WriteByte('"')
  814. }
  815. // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
  816. type sliceEncoder struct {
  817. arrayEnc encoderFunc
  818. }
  819. func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  820. if v.IsNil() {
  821. e.WriteString("null")
  822. return
  823. }
  824. if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
  825. // We're a large number of nested ptrEncoder.encode calls deep;
  826. // start checking if we've run into a pointer cycle.
  827. // Here we use a struct to memorize the pointer to the first element of the slice
  828. // and its length.
  829. ptr := struct {
  830. ptr uintptr
  831. len int
  832. }{v.Pointer(), v.Len()}
  833. if _, ok := e.ptrSeen[ptr]; ok {
  834. e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
  835. }
  836. e.ptrSeen[ptr] = struct{}{}
  837. defer delete(e.ptrSeen, ptr)
  838. }
  839. se.arrayEnc(e, v, opts)
  840. e.ptrLevel--
  841. }
  842. func newSliceEncoder(t reflect.Type) encoderFunc {
  843. // Byte slices get special treatment; arrays don't.
  844. if t.Elem().Kind() == reflect.Uint8 {
  845. p := reflect.PtrTo(t.Elem())
  846. if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
  847. return encodeByteSlice
  848. }
  849. }
  850. enc := sliceEncoder{newArrayEncoder(t)}
  851. return enc.encode
  852. }
  853. type arrayEncoder struct {
  854. elemEnc encoderFunc
  855. }
  856. func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  857. e.WriteByte('[')
  858. n := v.Len()
  859. for i := 0; i < n; i++ {
  860. if i > 0 {
  861. e.WriteByte(',')
  862. }
  863. ae.elemEnc(e, v.Index(i), opts)
  864. }
  865. e.WriteByte(']')
  866. }
  867. func newArrayEncoder(t reflect.Type) encoderFunc {
  868. enc := arrayEncoder{typeEncoder(t.Elem())}
  869. return enc.encode
  870. }
  871. type ptrEncoder struct {
  872. elemEnc encoderFunc
  873. }
  874. func (pe ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  875. if v.IsNil() {
  876. e.WriteString("null")
  877. return
  878. }
  879. if e.ptrLevel++; e.ptrLevel > startDetectingCyclesAfter {
  880. // We're a large number of nested ptrEncoder.encode calls deep;
  881. // start checking if we've run into a pointer cycle.
  882. ptr := v.Interface()
  883. if _, ok := e.ptrSeen[ptr]; ok {
  884. e.error(&UnsupportedValueError{v, fmt.Sprintf("encountered a cycle via %s", v.Type())})
  885. }
  886. e.ptrSeen[ptr] = struct{}{}
  887. defer delete(e.ptrSeen, ptr)
  888. }
  889. pe.elemEnc(e, v.Elem(), opts)
  890. e.ptrLevel--
  891. }
  892. func newPtrEncoder(t reflect.Type) encoderFunc {
  893. enc := ptrEncoder{typeEncoder(t.Elem())}
  894. return enc.encode
  895. }
  896. type condAddrEncoder struct {
  897. canAddrEnc, elseEnc encoderFunc
  898. }
  899. func (ce condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
  900. if v.CanAddr() {
  901. ce.canAddrEnc(e, v, opts)
  902. } else {
  903. ce.elseEnc(e, v, opts)
  904. }
  905. }
  906. // newCondAddrEncoder returns an encoder that checks whether its value
  907. // CanAddr and delegates to canAddrEnc if so, else to elseEnc.
  908. func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
  909. enc := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
  910. return enc.encode
  911. }
  912. func isValidTag(s string) bool {
  913. if s == "" {
  914. return false
  915. }
  916. for _, c := range s {
  917. switch {
  918. case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", c):
  919. // Backslash and quote chars are reserved, but
  920. // otherwise any punctuation chars are allowed
  921. // in a tag name.
  922. case !unicode.IsLetter(c) && !unicode.IsDigit(c):
  923. return false
  924. }
  925. }
  926. return true
  927. }
  928. func typeByIndex(t reflect.Type, index []int) reflect.Type {
  929. for _, i := range index {
  930. if t.Kind() == reflect.Ptr {
  931. t = t.Elem()
  932. }
  933. t = t.Field(i).Type
  934. }
  935. return t
  936. }
  937. type reflectWithString struct {
  938. k reflect.Value
  939. v reflect.Value
  940. ks string
  941. }
  942. func (w *reflectWithString) resolve() error {
  943. if w.k.Kind() == reflect.String {
  944. w.ks = w.k.String()
  945. return nil
  946. }
  947. if tm, ok := w.k.Interface().(encoding.TextMarshaler); ok {
  948. if w.k.Kind() == reflect.Ptr && w.k.IsNil() {
  949. return nil
  950. }
  951. buf, err := tm.MarshalText()
  952. w.ks = string(buf)
  953. return err
  954. }
  955. switch w.k.Kind() {
  956. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  957. w.ks = strconv.FormatInt(w.k.Int(), 10)
  958. return nil
  959. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  960. w.ks = strconv.FormatUint(w.k.Uint(), 10)
  961. return nil
  962. }
  963. panic("unexpected map key type")
  964. }
  965. // NOTE: keep in sync with stringBytes below.
  966. func (e *encodeState) string(s string, escapeHTML bool) {
  967. e.WriteByte('"')
  968. start := 0
  969. for i := 0; i < len(s); {
  970. if b := s[i]; b < utf8.RuneSelf {
  971. if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
  972. i++
  973. continue
  974. }
  975. if start < i {
  976. e.WriteString(s[start:i])
  977. }
  978. e.WriteByte('\\')
  979. switch b {
  980. case '\\', '"':
  981. e.WriteByte(b)
  982. case '\n':
  983. e.WriteByte('n')
  984. case '\r':
  985. e.WriteByte('r')
  986. case '\t':
  987. e.WriteByte('t')
  988. default:
  989. // This encodes bytes < 0x20 except for \t, \n and \r.
  990. // If escapeHTML is set, it also escapes <, >, and &
  991. // because they can lead to security holes when
  992. // user-controlled strings are rendered into JSON
  993. // and served to some browsers.
  994. e.WriteString(`u00`)
  995. e.WriteByte(hex[b>>4])
  996. e.WriteByte(hex[b&0xF])
  997. }
  998. i++
  999. start = i
  1000. continue
  1001. }
  1002. c, size := utf8.DecodeRuneInString(s[i:])
  1003. if c == utf8.RuneError && size == 1 {
  1004. if start < i {
  1005. e.WriteString(s[start:i])
  1006. }
  1007. e.WriteString(`\ufffd`)
  1008. i += size
  1009. start = i
  1010. continue
  1011. }
  1012. // U+2028 is LINE SEPARATOR.
  1013. // U+2029 is PARAGRAPH SEPARATOR.
  1014. // They are both technically valid characters in JSON strings,
  1015. // but don't work in JSONP, which has to be evaluated as JavaScript,
  1016. // and can lead to security holes there. It is valid JSON to
  1017. // escape them, so we do so unconditionally.
  1018. // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
  1019. if c == '\u2028' || c == '\u2029' {
  1020. if start < i {
  1021. e.WriteString(s[start:i])
  1022. }
  1023. e.WriteString(`\u202`)
  1024. e.WriteByte(hex[c&0xF])
  1025. i += size
  1026. start = i
  1027. continue
  1028. }
  1029. i += size
  1030. }
  1031. if start < len(s) {
  1032. e.WriteString(s[start:])
  1033. }
  1034. e.WriteByte('"')
  1035. }
  1036. // NOTE: keep in sync with string above.
  1037. func (e *encodeState) stringBytes(s []byte, escapeHTML bool) {
  1038. e.WriteByte('"')
  1039. start := 0
  1040. for i := 0; i < len(s); {
  1041. if b := s[i]; b < utf8.RuneSelf {
  1042. if htmlSafeSet[b] || (!escapeHTML && safeSet[b]) {
  1043. i++
  1044. continue
  1045. }
  1046. if start < i {
  1047. e.Write(s[start:i])
  1048. }
  1049. e.WriteByte('\\')
  1050. switch b {
  1051. case '\\', '"':
  1052. e.WriteByte(b)
  1053. case '\n':
  1054. e.WriteByte('n')
  1055. case '\r':
  1056. e.WriteByte('r')
  1057. case '\t':
  1058. e.WriteByte('t')
  1059. default:
  1060. // This encodes bytes < 0x20 except for \t, \n and \r.
  1061. // If escapeHTML is set, it also escapes <, >, and &
  1062. // because they can lead to security holes when
  1063. // user-controlled strings are rendered into JSON
  1064. // and served to some browsers.
  1065. e.WriteString(`u00`)
  1066. e.WriteByte(hex[b>>4])
  1067. e.WriteByte(hex[b&0xF])
  1068. }
  1069. i++
  1070. start = i
  1071. continue
  1072. }
  1073. c, size := utf8.DecodeRune(s[i:])
  1074. if c == utf8.RuneError && size == 1 {
  1075. if start < i {
  1076. e.Write(s[start:i])
  1077. }
  1078. e.WriteString(`\ufffd`)
  1079. i += size
  1080. start = i
  1081. continue
  1082. }
  1083. // U+2028 is LINE SEPARATOR.
  1084. // U+2029 is PARAGRAPH SEPARATOR.
  1085. // They are both technically valid characters in JSON strings,
  1086. // but don't work in JSONP, which has to be evaluated as JavaScript,
  1087. // and can lead to security holes there. It is valid JSON to
  1088. // escape them, so we do so unconditionally.
  1089. // See http://timelessrepo.com/json-isnt-a-javascript-subset for discussion.
  1090. if c == '\u2028' || c == '\u2029' {
  1091. if start < i {
  1092. e.Write(s[start:i])
  1093. }
  1094. e.WriteString(`\u202`)
  1095. e.WriteByte(hex[c&0xF])
  1096. i += size
  1097. start = i
  1098. continue
  1099. }
  1100. i += size
  1101. }
  1102. if start < len(s) {
  1103. e.Write(s[start:])
  1104. }
  1105. e.WriteByte('"')
  1106. }
  1107. // A field represents a single field found in a struct.
  1108. type field struct {
  1109. name string
  1110. nameBytes []byte // []byte(name)
  1111. equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent
  1112. nameNonEsc string // `"` + name + `":`
  1113. nameEscHTML string // `"` + HTMLEscape(name) + `":`
  1114. tag bool
  1115. index []int
  1116. typ reflect.Type
  1117. omitEmpty bool
  1118. quoted bool
  1119. encoder encoderFunc
  1120. }
  1121. // byIndex sorts field by index sequence.
  1122. type byIndex []field
  1123. func (x byIndex) Len() int { return len(x) }
  1124. func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  1125. func (x byIndex) Less(i, j int) bool {
  1126. for k, xik := range x[i].index {
  1127. if k >= len(x[j].index) {
  1128. return false
  1129. }
  1130. if xik != x[j].index[k] {
  1131. return xik < x[j].index[k]
  1132. }
  1133. }
  1134. return len(x[i].index) < len(x[j].index)
  1135. }
  1136. // typeFields returns a list of fields that JSON should recognize for the given type.
  1137. // The algorithm is breadth-first search over the set of structs to include - the top struct
  1138. // and then any reachable anonymous structs.
  1139. func typeFields(t reflect.Type) structFields {
  1140. // Anonymous fields to explore at the current level and the next.
  1141. current := []field{}
  1142. next := []field{{typ: t}}
  1143. // Count of queued names for current level and the next.
  1144. var count, nextCount map[reflect.Type]int
  1145. // Types already visited at an earlier level.
  1146. visited := map[reflect.Type]bool{}
  1147. // Fields found.
  1148. var fields []field
  1149. // Buffer to run HTMLEscape on field names.
  1150. var nameEscBuf bytes.Buffer
  1151. for len(next) > 0 {
  1152. current, next = next, current[:0]
  1153. count, nextCount = nextCount, map[reflect.Type]int{}
  1154. for _, f := range current {
  1155. if visited[f.typ] {
  1156. continue
  1157. }
  1158. visited[f.typ] = true
  1159. // Scan f.typ for fields to include.
  1160. for i := 0; i < f.typ.NumField(); i++ {
  1161. sf := f.typ.Field(i)
  1162. if sf.Anonymous {
  1163. t := sf.Type
  1164. if t.Kind() == reflect.Ptr {
  1165. t = t.Elem()
  1166. }
  1167. if !sf.IsExported() && t.Kind() != reflect.Struct {
  1168. // Ignore embedded fields of unexported non-struct types.
  1169. continue
  1170. }
  1171. // Do not ignore embedded fields of unexported struct types
  1172. // since they may have exported fields.
  1173. } else if !sf.IsExported() {
  1174. // Ignore unexported non-embedded fields.
  1175. continue
  1176. }
  1177. tag := sf.Tag.Get("json")
  1178. if tag == "-" {
  1179. continue
  1180. }
  1181. name, opts := parseTag(tag)
  1182. if !isValidTag(name) {
  1183. name = ""
  1184. }
  1185. index := make([]int, len(f.index)+1)
  1186. copy(index, f.index)
  1187. index[len(f.index)] = i
  1188. ft := sf.Type
  1189. if ft.Name() == "" && ft.Kind() == reflect.Ptr {
  1190. // Follow pointer.
  1191. ft = ft.Elem()
  1192. }
  1193. // Only strings, floats, integers, and booleans can be quoted.
  1194. quoted := false
  1195. if opts.Contains("string") {
  1196. switch ft.Kind() {
  1197. case reflect.Bool,
  1198. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  1199. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
  1200. reflect.Float32, reflect.Float64,
  1201. reflect.String:
  1202. quoted = true
  1203. }
  1204. }
  1205. // Record found field and index sequence.
  1206. if name != "" || !sf.Anonymous || ft.Kind() != reflect.Struct {
  1207. tagged := name != ""
  1208. if name == "" {
  1209. name = sf.Name
  1210. }
  1211. field := field{
  1212. name: name,
  1213. tag: tagged,
  1214. index: index,
  1215. typ: ft,
  1216. omitEmpty: opts.Contains("omitempty"),
  1217. quoted: quoted,
  1218. }
  1219. field.nameBytes = []byte(field.name)
  1220. field.equalFold = foldFunc(field.nameBytes)
  1221. // Build nameEscHTML and nameNonEsc ahead of time.
  1222. nameEscBuf.Reset()
  1223. nameEscBuf.WriteString(`"`)
  1224. HTMLEscape(&nameEscBuf, field.nameBytes)
  1225. nameEscBuf.WriteString(`":`)
  1226. field.nameEscHTML = nameEscBuf.String()
  1227. field.nameNonEsc = `"` + field.name + `":`
  1228. fields = append(fields, field)
  1229. if count[f.typ] > 1 {
  1230. // If there were multiple instances, add a second,
  1231. // so that the annihilation code will see a duplicate.
  1232. // It only cares about the distinction between 1 or 2,
  1233. // so don't bother generating any more copies.
  1234. fields = append(fields, fields[len(fields)-1])
  1235. }
  1236. continue
  1237. }
  1238. // Record new anonymous struct to explore in next round.
  1239. nextCount[ft]++
  1240. if nextCount[ft] == 1 {
  1241. next = append(next, field{name: ft.Name(), index: index, typ: ft})
  1242. }
  1243. }
  1244. }
  1245. }
  1246. sort.Slice(fields, func(i, j int) bool {
  1247. x := fields
  1248. // sort field by name, breaking ties with depth, then
  1249. // breaking ties with "name came from json tag", then
  1250. // breaking ties with index sequence.
  1251. if x[i].name != x[j].name {
  1252. return x[i].name < x[j].name
  1253. }
  1254. if len(x[i].index) != len(x[j].index) {
  1255. return len(x[i].index) < len(x[j].index)
  1256. }
  1257. if x[i].tag != x[j].tag {
  1258. return x[i].tag
  1259. }
  1260. return byIndex(x).Less(i, j)
  1261. })
  1262. // Delete all fields that are hidden by the Go rules for embedded fields,
  1263. // except that fields with JSON tags are promoted.
  1264. // The fields are sorted in primary order of name, secondary order
  1265. // of field index length. Loop over names; for each name, delete
  1266. // hidden fields by choosing the one dominant field that survives.
  1267. out := fields[:0]
  1268. for advance, i := 0, 0; i < len(fields); i += advance {
  1269. // One iteration per name.
  1270. // Find the sequence of fields with the name of this first field.
  1271. fi := fields[i]
  1272. name := fi.name
  1273. for advance = 1; i+advance < len(fields); advance++ {
  1274. fj := fields[i+advance]
  1275. if fj.name != name {
  1276. break
  1277. }
  1278. }
  1279. if advance == 1 { // Only one field with this name
  1280. out = append(out, fi)
  1281. continue
  1282. }
  1283. dominant, ok := dominantField(fields[i : i+advance])
  1284. if ok {
  1285. out = append(out, dominant)
  1286. }
  1287. }
  1288. fields = out
  1289. sort.Sort(byIndex(fields))
  1290. for i := range fields {
  1291. f := &fields[i]
  1292. f.encoder = typeEncoder(typeByIndex(t, f.index))
  1293. }
  1294. nameIndex := make(map[string]int, len(fields))
  1295. for i, field := range fields {
  1296. nameIndex[field.name] = i
  1297. }
  1298. return structFields{fields, nameIndex}
  1299. }
  1300. // dominantField looks through the fields, all of which are known to
  1301. // have the same name, to find the single field that dominates the
  1302. // others using Go's embedding rules, modified by the presence of
  1303. // JSON tags. If there are multiple top-level fields, the boolean
  1304. // will be false: This condition is an error in Go and we skip all
  1305. // the fields.
  1306. func dominantField(fields []field) (field, bool) {
  1307. // The fields are sorted in increasing index-length order, then by presence of tag.
  1308. // That means that the first field is the dominant one. We need only check
  1309. // for error cases: two fields at top level, either both tagged or neither tagged.
  1310. if len(fields) > 1 && len(fields[0].index) == len(fields[1].index) && fields[0].tag == fields[1].tag {
  1311. return field{}, false
  1312. }
  1313. return fields[0], true
  1314. }
  1315. var fieldCache sync.Map // map[reflect.Type]structFields
  1316. // cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
  1317. func cachedTypeFields(t reflect.Type) structFields {
  1318. if f, ok := fieldCache.Load(t); ok {
  1319. return f.(structFields)
  1320. }
  1321. f, _ := fieldCache.LoadOrStore(t, typeFields(t))
  1322. return f.(structFields)
  1323. }