encode.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. import (
  5. "io"
  6. "reflect"
  7. )
  8. const (
  9. // Some tagging information for error messages.
  10. msgTagEnc = "codec.encoder"
  11. defEncByteBufSize = 1 << 6 // 4:16, 6:64, 8:256, 10:1024
  12. // maxTimeSecs32 = math.MaxInt32 / 60 / 24 / 366
  13. )
  14. // AsSymbolFlag defines what should be encoded as symbols.
  15. type AsSymbolFlag uint8
  16. const (
  17. // AsSymbolDefault is default.
  18. // Currently, this means only encode struct field names as symbols.
  19. // The default is subject to change.
  20. AsSymbolDefault AsSymbolFlag = iota
  21. // AsSymbolAll means encode anything which could be a symbol as a symbol.
  22. AsSymbolAll = 0xfe
  23. // AsSymbolNone means do not encode anything as a symbol.
  24. AsSymbolNone = 1 << iota
  25. // AsSymbolMapStringKeys means encode keys in map[string]XXX as symbols.
  26. AsSymbolMapStringKeysFlag
  27. // AsSymbolStructFieldName means encode struct field names as symbols.
  28. AsSymbolStructFieldNameFlag
  29. )
  30. // encWriter abstracting writing to a byte array or to an io.Writer.
  31. type encWriter interface {
  32. writeUint16(uint16)
  33. writeUint32(uint32)
  34. writeUint64(uint64)
  35. writeb([]byte)
  36. writestr(string)
  37. writen1(byte)
  38. writen2(byte, byte)
  39. atEndOfEncode()
  40. }
  41. // encDriver abstracts the actual codec (binc vs msgpack, etc)
  42. type encDriver interface {
  43. isBuiltinType(rt uintptr) bool
  44. encodeBuiltin(rt uintptr, v interface{})
  45. encodeNil()
  46. encodeInt(i int64)
  47. encodeUint(i uint64)
  48. encodeBool(b bool)
  49. encodeFloat32(f float32)
  50. encodeFloat64(f float64)
  51. encodeExtPreamble(xtag byte, length int)
  52. encodeArrayPreamble(length int)
  53. encodeMapPreamble(length int)
  54. encodeString(c charEncoding, v string)
  55. encodeSymbol(v string)
  56. encodeStringBytes(c charEncoding, v []byte)
  57. //TODO
  58. //encBignum(f *big.Int)
  59. //encStringRunes(c charEncoding, v []rune)
  60. }
  61. type ioEncWriterWriter interface {
  62. WriteByte(c byte) error
  63. WriteString(s string) (n int, err error)
  64. Write(p []byte) (n int, err error)
  65. }
  66. type ioEncStringWriter interface {
  67. WriteString(s string) (n int, err error)
  68. }
  69. type EncodeOptions struct {
  70. // Encode a struct as an array, and not as a map.
  71. StructToArray bool
  72. // AsSymbols defines what should be encoded as symbols.
  73. //
  74. // Encoding as symbols can reduce the encoded size significantly.
  75. //
  76. // However, during decoding, each string to be encoded as a symbol must
  77. // be checked to see if it has been seen before. Consequently, encoding time
  78. // will increase if using symbols, because string comparisons has a clear cost.
  79. //
  80. // Sample values:
  81. // AsSymbolNone
  82. // AsSymbolAll
  83. // AsSymbolMapStringKeys
  84. // AsSymbolMapStringKeysFlag | AsSymbolStructFieldNameFlag
  85. AsSymbols AsSymbolFlag
  86. }
  87. // ---------------------------------------------
  88. type simpleIoEncWriterWriter struct {
  89. w io.Writer
  90. bw io.ByteWriter
  91. sw ioEncStringWriter
  92. }
  93. func (o *simpleIoEncWriterWriter) WriteByte(c byte) (err error) {
  94. if o.bw != nil {
  95. return o.bw.WriteByte(c)
  96. }
  97. _, err = o.w.Write([]byte{c})
  98. return
  99. }
  100. func (o *simpleIoEncWriterWriter) WriteString(s string) (n int, err error) {
  101. if o.sw != nil {
  102. return o.sw.WriteString(s)
  103. }
  104. return o.w.Write([]byte(s))
  105. }
  106. func (o *simpleIoEncWriterWriter) Write(p []byte) (n int, err error) {
  107. return o.w.Write(p)
  108. }
  109. // ----------------------------------------
  110. // ioEncWriter implements encWriter and can write to an io.Writer implementation
  111. type ioEncWriter struct {
  112. w ioEncWriterWriter
  113. x [8]byte // temp byte array re-used internally for efficiency
  114. }
  115. func (z *ioEncWriter) writeUint16(v uint16) {
  116. bigen.PutUint16(z.x[:2], v)
  117. z.writeb(z.x[:2])
  118. }
  119. func (z *ioEncWriter) writeUint32(v uint32) {
  120. bigen.PutUint32(z.x[:4], v)
  121. z.writeb(z.x[:4])
  122. }
  123. func (z *ioEncWriter) writeUint64(v uint64) {
  124. bigen.PutUint64(z.x[:8], v)
  125. z.writeb(z.x[:8])
  126. }
  127. func (z *ioEncWriter) writeb(bs []byte) {
  128. n, err := z.w.Write(bs)
  129. if err != nil {
  130. panic(err)
  131. }
  132. if n != len(bs) {
  133. encErr("write: Incorrect num bytes written. Expecting: %v, Wrote: %v", len(bs), n)
  134. }
  135. }
  136. func (z *ioEncWriter) writestr(s string) {
  137. n, err := z.w.WriteString(s)
  138. if err != nil {
  139. panic(err)
  140. }
  141. if n != len(s) {
  142. encErr("write: Incorrect num bytes written. Expecting: %v, Wrote: %v", len(s), n)
  143. }
  144. }
  145. func (z *ioEncWriter) writen1(b byte) {
  146. if err := z.w.WriteByte(b); err != nil {
  147. panic(err)
  148. }
  149. }
  150. func (z *ioEncWriter) writen2(b1 byte, b2 byte) {
  151. z.writen1(b1)
  152. z.writen1(b2)
  153. }
  154. func (z *ioEncWriter) atEndOfEncode() {}
  155. // ----------------------------------------
  156. // bytesEncWriter implements encWriter and can write to an byte slice.
  157. // It is used by Marshal function.
  158. type bytesEncWriter struct {
  159. b []byte
  160. c int // cursor
  161. out *[]byte // write out on atEndOfEncode
  162. }
  163. func (z *bytesEncWriter) writeUint16(v uint16) {
  164. c := z.grow(2)
  165. z.b[c] = byte(v >> 8)
  166. z.b[c+1] = byte(v)
  167. }
  168. func (z *bytesEncWriter) writeUint32(v uint32) {
  169. c := z.grow(4)
  170. z.b[c] = byte(v >> 24)
  171. z.b[c+1] = byte(v >> 16)
  172. z.b[c+2] = byte(v >> 8)
  173. z.b[c+3] = byte(v)
  174. }
  175. func (z *bytesEncWriter) writeUint64(v uint64) {
  176. c := z.grow(8)
  177. z.b[c] = byte(v >> 56)
  178. z.b[c+1] = byte(v >> 48)
  179. z.b[c+2] = byte(v >> 40)
  180. z.b[c+3] = byte(v >> 32)
  181. z.b[c+4] = byte(v >> 24)
  182. z.b[c+5] = byte(v >> 16)
  183. z.b[c+6] = byte(v >> 8)
  184. z.b[c+7] = byte(v)
  185. }
  186. func (z *bytesEncWriter) writeb(s []byte) {
  187. c := z.grow(len(s))
  188. copy(z.b[c:], s)
  189. }
  190. func (z *bytesEncWriter) writestr(s string) {
  191. c := z.grow(len(s))
  192. copy(z.b[c:], s)
  193. }
  194. func (z *bytesEncWriter) writen1(b1 byte) {
  195. c := z.grow(1)
  196. z.b[c] = b1
  197. }
  198. func (z *bytesEncWriter) writen2(b1 byte, b2 byte) {
  199. c := z.grow(2)
  200. z.b[c] = b1
  201. z.b[c+1] = b2
  202. }
  203. func (z *bytesEncWriter) atEndOfEncode() {
  204. *(z.out) = z.b[:z.c]
  205. }
  206. func (z *bytesEncWriter) grow(n int) (oldcursor int) {
  207. oldcursor = z.c
  208. z.c = oldcursor + n
  209. if z.c > cap(z.b) {
  210. // Tried using appendslice logic: (if cap < 1024, *2, else *1.25).
  211. // However, it was too expensive, causing too many iterations of copy.
  212. // Using bytes.Buffer model was much better (2*cap + n)
  213. bs := make([]byte, 2*cap(z.b)+n)
  214. copy(bs, z.b[:oldcursor])
  215. z.b = bs
  216. } else if z.c > len(z.b) {
  217. z.b = z.b[:cap(z.b)]
  218. }
  219. return
  220. }
  221. // ---------------------------------------------
  222. type encFnInfo struct {
  223. ti *typeInfo
  224. e *Encoder
  225. ee encDriver
  226. xfFn func(reflect.Value) ([]byte, error)
  227. xfTag byte
  228. }
  229. func (f *encFnInfo) builtin(rv reflect.Value) {
  230. f.ee.encodeBuiltin(f.ti.rtid, rv.Interface())
  231. }
  232. func (f *encFnInfo) rawExt(rv reflect.Value) {
  233. f.e.encRawExt(rv.Interface().(RawExt))
  234. }
  235. func (f *encFnInfo) ext(rv reflect.Value) {
  236. bs, fnerr := f.xfFn(rv)
  237. if fnerr != nil {
  238. panic(fnerr)
  239. }
  240. if bs == nil {
  241. f.ee.encodeNil()
  242. return
  243. }
  244. if f.e.hh.writeExt() {
  245. f.ee.encodeExtPreamble(f.xfTag, len(bs))
  246. f.e.w.writeb(bs)
  247. } else {
  248. f.ee.encodeStringBytes(c_RAW, bs)
  249. }
  250. }
  251. func (f *encFnInfo) binaryMarshal(rv reflect.Value) {
  252. var bm binaryMarshaler
  253. if f.ti.mIndir == 0 {
  254. bm = rv.Interface().(binaryMarshaler)
  255. } else if f.ti.mIndir == -1 {
  256. bm = rv.Addr().Interface().(binaryMarshaler)
  257. } else {
  258. for j, k := int8(0), f.ti.mIndir; j < k; j++ {
  259. if rv.IsNil() {
  260. f.ee.encodeNil()
  261. return
  262. }
  263. rv = rv.Elem()
  264. }
  265. bm = rv.Interface().(binaryMarshaler)
  266. }
  267. // debugf(">>>> binaryMarshaler: %T", rv.Interface())
  268. bs, fnerr := bm.MarshalBinary()
  269. if fnerr != nil {
  270. panic(fnerr)
  271. }
  272. if bs == nil {
  273. f.ee.encodeNil()
  274. } else {
  275. f.ee.encodeStringBytes(c_RAW, bs)
  276. }
  277. }
  278. func (f *encFnInfo) kBool(rv reflect.Value) {
  279. f.ee.encodeBool(rv.Bool())
  280. }
  281. func (f *encFnInfo) kString(rv reflect.Value) {
  282. f.ee.encodeString(c_UTF8, rv.String())
  283. }
  284. func (f *encFnInfo) kFloat64(rv reflect.Value) {
  285. f.ee.encodeFloat64(rv.Float())
  286. }
  287. func (f *encFnInfo) kFloat32(rv reflect.Value) {
  288. f.ee.encodeFloat32(float32(rv.Float()))
  289. }
  290. func (f *encFnInfo) kInt(rv reflect.Value) {
  291. f.ee.encodeInt(rv.Int())
  292. }
  293. func (f *encFnInfo) kUint(rv reflect.Value) {
  294. f.ee.encodeUint(rv.Uint())
  295. }
  296. func (f *encFnInfo) kInvalid(rv reflect.Value) {
  297. f.ee.encodeNil()
  298. }
  299. func (f *encFnInfo) kErr(rv reflect.Value) {
  300. encErr("Unsupported kind: %s, for: %#v", rv.Kind(), rv)
  301. }
  302. func (f *encFnInfo) kSlice(rv reflect.Value) {
  303. if rv.IsNil() {
  304. f.ee.encodeNil()
  305. return
  306. }
  307. if shortCircuitReflectToFastPath {
  308. switch f.ti.rtid {
  309. case intfSliceTypId:
  310. f.e.encSliceIntf(rv.Interface().([]interface{}))
  311. return
  312. case intSliceTypId:
  313. f.e.encSliceInt(rv.Interface().([]int))
  314. return
  315. case strSliceTypId:
  316. f.e.encSliceStr(rv.Interface().([]string))
  317. return
  318. }
  319. }
  320. if f.ti.rtid == byteSliceTypId {
  321. f.ee.encodeStringBytes(c_RAW, rv.Bytes())
  322. return
  323. }
  324. l := rv.Len()
  325. if f.ti.mbs {
  326. if l%2 == 1 {
  327. encErr("mapBySlice: invalid length (must be divisible by 2): %v", l)
  328. }
  329. f.ee.encodeMapPreamble(l / 2)
  330. } else {
  331. f.ee.encodeArrayPreamble(l)
  332. }
  333. if l == 0 {
  334. return
  335. }
  336. for j := 0; j < l; j++ {
  337. // TODO: Consider perf implication of encoding odd index values as symbols if type is string
  338. f.e.encodeValue(rv.Index(j))
  339. }
  340. }
  341. func (f *encFnInfo) kArray(rv reflect.Value) {
  342. // f.e.encodeValue(rv.Slice(0, rv.Len()))
  343. f.kSlice(rv.Slice(0, rv.Len()))
  344. }
  345. func (f *encFnInfo) kStruct(rv reflect.Value) {
  346. fti := f.ti
  347. newlen := len(fti.sfi)
  348. rvals := make([]reflect.Value, newlen)
  349. var encnames []string
  350. e := f.e
  351. tisfi := fti.sfip
  352. toMap := !(fti.toArray || e.h.StructToArray)
  353. // if toMap, use the sorted array. If toArray, use unsorted array (to match sequence in struct)
  354. if toMap {
  355. tisfi = fti.sfi
  356. encnames = make([]string, newlen)
  357. }
  358. newlen = 0
  359. for _, si := range tisfi {
  360. if si.i != -1 {
  361. rvals[newlen] = rv.Field(int(si.i))
  362. } else {
  363. rvals[newlen] = rv.FieldByIndex(si.is)
  364. }
  365. if toMap {
  366. if si.omitEmpty && isEmptyValue(rvals[newlen]) {
  367. continue
  368. }
  369. encnames[newlen] = si.encName
  370. } else {
  371. if si.omitEmpty && isEmptyValue(rvals[newlen]) {
  372. rvals[newlen] = reflect.Value{} //encode as nil
  373. }
  374. }
  375. newlen++
  376. }
  377. // debugf(">>>> kStruct: newlen: %v", newlen)
  378. if toMap {
  379. ee := f.ee //don't dereference everytime
  380. ee.encodeMapPreamble(newlen)
  381. // asSymbols := e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  382. asSymbols := e.h.AsSymbols == AsSymbolDefault || e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  383. for j := 0; j < newlen; j++ {
  384. if asSymbols {
  385. ee.encodeSymbol(encnames[j])
  386. } else {
  387. ee.encodeString(c_UTF8, encnames[j])
  388. }
  389. e.encodeValue(rvals[j])
  390. }
  391. } else {
  392. f.ee.encodeArrayPreamble(newlen)
  393. for j := 0; j < newlen; j++ {
  394. e.encodeValue(rvals[j])
  395. }
  396. }
  397. }
  398. // func (f *encFnInfo) kPtr(rv reflect.Value) {
  399. // debugf(">>>>>>> ??? encode kPtr called - shouldn't get called")
  400. // if rv.IsNil() {
  401. // f.ee.encodeNil()
  402. // return
  403. // }
  404. // f.e.encodeValue(rv.Elem())
  405. // }
  406. func (f *encFnInfo) kInterface(rv reflect.Value) {
  407. if rv.IsNil() {
  408. f.ee.encodeNil()
  409. return
  410. }
  411. f.e.encodeValue(rv.Elem())
  412. }
  413. func (f *encFnInfo) kMap(rv reflect.Value) {
  414. if rv.IsNil() {
  415. f.ee.encodeNil()
  416. return
  417. }
  418. if shortCircuitReflectToFastPath {
  419. switch f.ti.rtid {
  420. case mapStringIntfTypId:
  421. f.e.encMapStrIntf(rv.Interface().(map[string]interface{}))
  422. return
  423. case mapIntfIntfTypId:
  424. f.e.encMapIntfIntf(rv.Interface().(map[interface{}]interface{}))
  425. return
  426. case mapIntIntfTypId:
  427. f.e.encMapIntIntf(rv.Interface().(map[int]interface{}))
  428. return
  429. }
  430. }
  431. l := rv.Len()
  432. f.ee.encodeMapPreamble(l)
  433. if l == 0 {
  434. return
  435. }
  436. // keyTypeIsString := f.ti.rt.Key().Kind() == reflect.String
  437. keyTypeIsString := f.ti.rt.Key() == stringTyp
  438. var asSymbols bool
  439. if keyTypeIsString {
  440. asSymbols = f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
  441. }
  442. mks := rv.MapKeys()
  443. // for j, lmks := 0, len(mks); j < lmks; j++ {
  444. for j := range mks {
  445. if keyTypeIsString {
  446. if asSymbols {
  447. f.ee.encodeSymbol(mks[j].String())
  448. } else {
  449. f.ee.encodeString(c_UTF8, mks[j].String())
  450. }
  451. } else {
  452. f.e.encodeValue(mks[j])
  453. }
  454. f.e.encodeValue(rv.MapIndex(mks[j]))
  455. }
  456. }
  457. // --------------------------------------------------
  458. // encFn encapsulates the captured variables and the encode function.
  459. // This way, we only do some calculations one times, and pass to the
  460. // code block that should be called (encapsulated in a function)
  461. // instead of executing the checks every time.
  462. type encFn struct {
  463. i *encFnInfo
  464. f func(*encFnInfo, reflect.Value)
  465. }
  466. // --------------------------------------------------
  467. // An Encoder writes an object to an output stream in the codec format.
  468. type Encoder struct {
  469. w encWriter
  470. e encDriver
  471. h *BasicHandle
  472. hh Handle
  473. f map[uintptr]encFn
  474. x []uintptr
  475. s []encFn
  476. }
  477. // NewEncoder returns an Encoder for encoding into an io.Writer.
  478. //
  479. // For efficiency, Users are encouraged to pass in a memory buffered writer
  480. // (eg bufio.Writer, bytes.Buffer).
  481. func NewEncoder(w io.Writer, h Handle) *Encoder {
  482. ww, ok := w.(ioEncWriterWriter)
  483. if !ok {
  484. sww := simpleIoEncWriterWriter{w: w}
  485. sww.bw, _ = w.(io.ByteWriter)
  486. sww.sw, _ = w.(ioEncStringWriter)
  487. ww = &sww
  488. //ww = bufio.NewWriterSize(w, defEncByteBufSize)
  489. }
  490. z := ioEncWriter{
  491. w: ww,
  492. }
  493. return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
  494. }
  495. // NewEncoderBytes returns an encoder for encoding directly and efficiently
  496. // into a byte slice, using zero-copying to temporary slices.
  497. //
  498. // It will potentially replace the output byte slice pointed to.
  499. // After encoding, the out parameter contains the encoded contents.
  500. func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
  501. in := *out
  502. if in == nil {
  503. in = make([]byte, defEncByteBufSize)
  504. }
  505. z := bytesEncWriter{
  506. b: in,
  507. out: out,
  508. }
  509. return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
  510. }
  511. // Encode writes an object into a stream in the codec format.
  512. //
  513. // Encoding can be configured via the "codec" struct tag for the fields.
  514. //
  515. // The "codec" key in struct field's tag value is the key name,
  516. // followed by an optional comma and options.
  517. //
  518. // To set an option on all fields (e.g. omitempty on all fields), you
  519. // can create a field called _struct, and set flags on it.
  520. //
  521. // Struct values "usually" encode as maps. Each exported struct field is encoded unless:
  522. // - the field's codec tag is "-", OR
  523. // - the field is empty and its codec tag specifies the "omitempty" option.
  524. //
  525. // When encoding as a map, the first string in the tag (before the comma)
  526. // is the map key string to use when encoding.
  527. //
  528. // However, struct values may encode as arrays. This happens when:
  529. // - StructToArray Encode option is set, OR
  530. // - the codec tag on the _struct field sets the "toarray" option
  531. //
  532. // Values with types that implement MapBySlice are encoded as stream maps.
  533. //
  534. // The empty values (for omitempty option) are false, 0, any nil pointer
  535. // or interface value, and any array, slice, map, or string of length zero.
  536. //
  537. // Anonymous fields are encoded inline if no struct tag is present.
  538. // Else they are encoded as regular fields.
  539. //
  540. // Examples:
  541. //
  542. // type MyStruct struct {
  543. // _struct bool `codec:",omitempty"` //set omitempty for every field
  544. // Field1 string `codec:"-"` //skip this field
  545. // Field2 int `codec:"myName"` //Use key "myName" in encode stream
  546. // Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty.
  547. // Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty.
  548. // ...
  549. // }
  550. //
  551. // type MyStruct struct {
  552. // _struct bool `codec:",omitempty,toarray"` //set omitempty for every field
  553. // //and encode struct as an array
  554. // }
  555. //
  556. // The mode of encoding is based on the type of the value. When a value is seen:
  557. // - If an extension is registered for it, call that extension function
  558. // - If it implements BinaryMarshaler, call its MarshalBinary() (data []byte, err error)
  559. // - Else encode it based on its reflect.Kind
  560. //
  561. // Note that struct field names and keys in map[string]XXX will be treated as symbols.
  562. // Some formats support symbols (e.g. binc) and will properly encode the string
  563. // only once in the stream, and use a tag to refer to it thereafter.
  564. func (e *Encoder) Encode(v interface{}) (err error) {
  565. defer panicToErr(&err)
  566. e.encode(v)
  567. e.w.atEndOfEncode()
  568. return
  569. }
  570. func (e *Encoder) encode(iv interface{}) {
  571. switch v := iv.(type) {
  572. case nil:
  573. e.e.encodeNil()
  574. case reflect.Value:
  575. e.encodeValue(v)
  576. case string:
  577. e.e.encodeString(c_UTF8, v)
  578. case bool:
  579. e.e.encodeBool(v)
  580. case int:
  581. e.e.encodeInt(int64(v))
  582. case int8:
  583. e.e.encodeInt(int64(v))
  584. case int16:
  585. e.e.encodeInt(int64(v))
  586. case int32:
  587. e.e.encodeInt(int64(v))
  588. case int64:
  589. e.e.encodeInt(v)
  590. case uint:
  591. e.e.encodeUint(uint64(v))
  592. case uint8:
  593. e.e.encodeUint(uint64(v))
  594. case uint16:
  595. e.e.encodeUint(uint64(v))
  596. case uint32:
  597. e.e.encodeUint(uint64(v))
  598. case uint64:
  599. e.e.encodeUint(v)
  600. case float32:
  601. e.e.encodeFloat32(v)
  602. case float64:
  603. e.e.encodeFloat64(v)
  604. case []byte:
  605. e.e.encodeStringBytes(c_RAW, v)
  606. case []interface{}:
  607. e.encSliceIntf(v)
  608. case []string:
  609. e.encSliceStr(v)
  610. case []int:
  611. e.encSliceInt(v)
  612. case map[int]interface{}:
  613. e.encMapIntIntf(v)
  614. case map[string]interface{}:
  615. e.encMapStrIntf(v)
  616. case map[interface{}]interface{}:
  617. e.encMapIntfIntf(v)
  618. case *string:
  619. e.e.encodeString(c_UTF8, *v)
  620. case *bool:
  621. e.e.encodeBool(*v)
  622. case *int:
  623. e.e.encodeInt(int64(*v))
  624. case *int8:
  625. e.e.encodeInt(int64(*v))
  626. case *int16:
  627. e.e.encodeInt(int64(*v))
  628. case *int32:
  629. e.e.encodeInt(int64(*v))
  630. case *int64:
  631. e.e.encodeInt(*v)
  632. case *uint:
  633. e.e.encodeUint(uint64(*v))
  634. case *uint8:
  635. e.e.encodeUint(uint64(*v))
  636. case *uint16:
  637. e.e.encodeUint(uint64(*v))
  638. case *uint32:
  639. e.e.encodeUint(uint64(*v))
  640. case *uint64:
  641. e.e.encodeUint(*v)
  642. case *float32:
  643. e.e.encodeFloat32(*v)
  644. case *float64:
  645. e.e.encodeFloat64(*v)
  646. case *[]byte:
  647. e.e.encodeStringBytes(c_RAW, *v)
  648. case *[]interface{}:
  649. e.encSliceIntf(*v)
  650. case *[]string:
  651. e.encSliceStr(*v)
  652. case *[]int:
  653. e.encSliceInt(*v)
  654. case *map[int]interface{}:
  655. e.encMapIntIntf(*v)
  656. case *map[string]interface{}:
  657. e.encMapStrIntf(*v)
  658. case *map[interface{}]interface{}:
  659. e.encMapIntfIntf(*v)
  660. default:
  661. e.encodeValue(reflect.ValueOf(iv))
  662. }
  663. }
  664. func (e *Encoder) encodeValue(rv reflect.Value) {
  665. for rv.Kind() == reflect.Ptr {
  666. if rv.IsNil() {
  667. e.e.encodeNil()
  668. return
  669. }
  670. rv = rv.Elem()
  671. }
  672. rt := rv.Type()
  673. rtid := reflect.ValueOf(rt).Pointer()
  674. // if e.f == nil && e.s == nil { debugf("---->Creating new enc f map for type: %v\n", rt) }
  675. var fn encFn
  676. var ok bool
  677. if useMapForCodecCache {
  678. fn, ok = e.f[rtid]
  679. } else {
  680. for i, v := range e.x {
  681. if v == rtid {
  682. fn, ok = e.s[i], true
  683. break
  684. }
  685. }
  686. }
  687. if !ok {
  688. // debugf("\tCreating new enc fn for type: %v\n", rt)
  689. fi := encFnInfo{ti: getTypeInfo(rtid, rt), e: e, ee: e.e}
  690. fn.i = &fi
  691. if rtid == rawExtTypId {
  692. fn.f = (*encFnInfo).rawExt
  693. } else if e.e.isBuiltinType(rtid) {
  694. fn.f = (*encFnInfo).builtin
  695. } else if xfTag, xfFn := e.h.getEncodeExt(rtid); xfFn != nil {
  696. fi.xfTag, fi.xfFn = xfTag, xfFn
  697. fn.f = (*encFnInfo).ext
  698. } else if supportBinaryMarshal && fi.ti.m {
  699. fn.f = (*encFnInfo).binaryMarshal
  700. } else {
  701. switch rk := rt.Kind(); rk {
  702. case reflect.Bool:
  703. fn.f = (*encFnInfo).kBool
  704. case reflect.String:
  705. fn.f = (*encFnInfo).kString
  706. case reflect.Float64:
  707. fn.f = (*encFnInfo).kFloat64
  708. case reflect.Float32:
  709. fn.f = (*encFnInfo).kFloat32
  710. case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
  711. fn.f = (*encFnInfo).kInt
  712. case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16:
  713. fn.f = (*encFnInfo).kUint
  714. case reflect.Invalid:
  715. fn.f = (*encFnInfo).kInvalid
  716. case reflect.Slice:
  717. fn.f = (*encFnInfo).kSlice
  718. case reflect.Array:
  719. fn.f = (*encFnInfo).kArray
  720. case reflect.Struct:
  721. fn.f = (*encFnInfo).kStruct
  722. // case reflect.Ptr:
  723. // fn.f = (*encFnInfo).kPtr
  724. case reflect.Interface:
  725. fn.f = (*encFnInfo).kInterface
  726. case reflect.Map:
  727. fn.f = (*encFnInfo).kMap
  728. default:
  729. fn.f = (*encFnInfo).kErr
  730. }
  731. }
  732. if useMapForCodecCache {
  733. if e.f == nil {
  734. e.f = make(map[uintptr]encFn, 16)
  735. }
  736. e.f[rtid] = fn
  737. } else {
  738. e.s = append(e.s, fn)
  739. e.x = append(e.x, rtid)
  740. }
  741. }
  742. fn.f(fn.i, rv)
  743. }
  744. func (e *Encoder) encRawExt(re RawExt) {
  745. if re.Data == nil {
  746. e.e.encodeNil()
  747. return
  748. }
  749. if e.hh.writeExt() {
  750. e.e.encodeExtPreamble(re.Tag, len(re.Data))
  751. e.w.writeb(re.Data)
  752. } else {
  753. e.e.encodeStringBytes(c_RAW, re.Data)
  754. }
  755. }
  756. // ---------------------------------------------
  757. // short circuit functions for common maps and slices
  758. func (e *Encoder) encSliceIntf(v []interface{}) {
  759. e.e.encodeArrayPreamble(len(v))
  760. for _, v2 := range v {
  761. e.encode(v2)
  762. }
  763. }
  764. func (e *Encoder) encSliceStr(v []string) {
  765. e.e.encodeArrayPreamble(len(v))
  766. for _, v2 := range v {
  767. e.e.encodeString(c_UTF8, v2)
  768. }
  769. }
  770. func (e *Encoder) encSliceInt(v []int) {
  771. e.e.encodeArrayPreamble(len(v))
  772. for _, v2 := range v {
  773. e.e.encodeInt(int64(v2))
  774. }
  775. }
  776. func (e *Encoder) encMapStrIntf(v map[string]interface{}) {
  777. e.e.encodeMapPreamble(len(v))
  778. asSymbols := e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
  779. for k2, v2 := range v {
  780. if asSymbols {
  781. e.e.encodeSymbol(k2)
  782. } else {
  783. e.e.encodeString(c_UTF8, k2)
  784. }
  785. e.encode(v2)
  786. }
  787. }
  788. func (e *Encoder) encMapIntIntf(v map[int]interface{}) {
  789. e.e.encodeMapPreamble(len(v))
  790. for k2, v2 := range v {
  791. e.e.encodeInt(int64(k2))
  792. e.encode(v2)
  793. }
  794. }
  795. func (e *Encoder) encMapIntfIntf(v map[interface{}]interface{}) {
  796. e.e.encodeMapPreamble(len(v))
  797. for k2, v2 := range v {
  798. e.encode(k2)
  799. e.encode(v2)
  800. }
  801. }
  802. // ----------------------------------------
  803. func encErr(format string, params ...interface{}) {
  804. doPanic(msgTagEnc, format, params...)
  805. }