1
2
3
4 package codec
5
6 import (
7 "encoding"
8 "errors"
9 "io"
10 "reflect"
11 "sort"
12 "strconv"
13 "time"
14 )
15
16
17
18 const defEncByteBufSize = 1 << 10
19
20 var errEncoderNotInitialized = errors.New("Encoder not initialized")
21
22
23 type encDriver interface {
24 EncodeNil()
25 EncodeInt(i int64)
26 EncodeUint(i uint64)
27 EncodeBool(b bool)
28 EncodeFloat32(f float32)
29 EncodeFloat64(f float64)
30 EncodeRawExt(re *RawExt)
31 EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext)
32
33 EncodeString(v string)
34 EncodeStringBytesRaw(v []byte)
35 EncodeTime(time.Time)
36 WriteArrayStart(length int)
37 WriteArrayEnd()
38 WriteMapStart(length int)
39 WriteMapEnd()
40
41
42 reset()
43
44 encoder() *Encoder
45
46 driverStateManager
47 }
48
49 type encDriverContainerTracker interface {
50 WriteArrayElem()
51 WriteMapElemKey()
52 WriteMapElemValue()
53 }
54
55 type encDriverNoState struct{}
56
57 func (encDriverNoState) captureState() interface{} { return nil }
58 func (encDriverNoState) reset() {}
59 func (encDriverNoState) resetState() {}
60 func (encDriverNoState) restoreState(v interface{}) {}
61
62 type encDriverNoopContainerWriter struct{}
63
64 func (encDriverNoopContainerWriter) WriteArrayStart(length int) {}
65 func (encDriverNoopContainerWriter) WriteArrayEnd() {}
66 func (encDriverNoopContainerWriter) WriteMapStart(length int) {}
67 func (encDriverNoopContainerWriter) WriteMapEnd() {}
68
69
70 type encStructFieldObj struct {
71 key string
72 rv reflect.Value
73 intf interface{}
74 ascii bool
75 isRv bool
76 }
77
78 type encStructFieldObjSlice []encStructFieldObj
79
80 func (p encStructFieldObjSlice) Len() int { return len(p) }
81 func (p encStructFieldObjSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] }
82 func (p encStructFieldObjSlice) Less(i, j int) bool {
83 return p[uint(i)].key < p[uint(j)].key
84 }
85
86
87 type EncodeOptions struct {
88
89
90
91 WriterBufferSize int
92
93
94
95
96
97
98
99 ChanRecvTimeout time.Duration
100
101
102 StructToArray bool
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 Canonical bool
119
120
121
122
123
124
125
126
127 CheckCircularRef bool
128
129
130
131
132
133
134
135
136
137
138
139 RecursiveEmptyCheck bool
140
141
142
143
144
145
146 Raw bool
147
148
149
150
151
152
153
154
155
156
157
158
159 StringToRaw bool
160
161
162
163
164
165
166
167
168
169 OptimumSize bool
170
171
172
173
174
175
176
177 NoAddressableReadonly bool
178 }
179
180
181
182 func (e *Encoder) rawExt(f *codecFnInfo, rv reflect.Value) {
183 e.e.EncodeRawExt(rv2i(rv).(*RawExt))
184 }
185
186 func (e *Encoder) ext(f *codecFnInfo, rv reflect.Value) {
187 e.e.EncodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn)
188 }
189
190 func (e *Encoder) selferMarshal(f *codecFnInfo, rv reflect.Value) {
191 rv2i(rv).(Selfer).CodecEncodeSelf(e)
192 }
193
194 func (e *Encoder) binaryMarshal(f *codecFnInfo, rv reflect.Value) {
195 bs, fnerr := rv2i(rv).(encoding.BinaryMarshaler).MarshalBinary()
196 e.marshalRaw(bs, fnerr)
197 }
198
199 func (e *Encoder) textMarshal(f *codecFnInfo, rv reflect.Value) {
200 bs, fnerr := rv2i(rv).(encoding.TextMarshaler).MarshalText()
201 e.marshalUtf8(bs, fnerr)
202 }
203
204 func (e *Encoder) jsonMarshal(f *codecFnInfo, rv reflect.Value) {
205 bs, fnerr := rv2i(rv).(jsonMarshaler).MarshalJSON()
206 e.marshalAsis(bs, fnerr)
207 }
208
209 func (e *Encoder) raw(f *codecFnInfo, rv reflect.Value) {
210 e.rawBytes(rv2i(rv).(Raw))
211 }
212
213 func (e *Encoder) encodeComplex64(v complex64) {
214 if imag(v) != 0 {
215 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v))
216 }
217 e.e.EncodeFloat32(real(v))
218 }
219
220 func (e *Encoder) encodeComplex128(v complex128) {
221 if imag(v) != 0 {
222 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v))
223 }
224 e.e.EncodeFloat64(real(v))
225 }
226
227 func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
228 e.e.EncodeBool(rvGetBool(rv))
229 }
230
231 func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
232 e.e.EncodeTime(rvGetTime(rv))
233 }
234
235 func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
236 e.e.EncodeString(rvGetString(rv))
237 }
238
239 func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
240 e.e.EncodeFloat32(rvGetFloat32(rv))
241 }
242
243 func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
244 e.e.EncodeFloat64(rvGetFloat64(rv))
245 }
246
247 func (e *Encoder) kComplex64(f *codecFnInfo, rv reflect.Value) {
248 e.encodeComplex64(rvGetComplex64(rv))
249 }
250
251 func (e *Encoder) kComplex128(f *codecFnInfo, rv reflect.Value) {
252 e.encodeComplex128(rvGetComplex128(rv))
253 }
254
255 func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
256 e.e.EncodeInt(int64(rvGetInt(rv)))
257 }
258
259 func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
260 e.e.EncodeInt(int64(rvGetInt8(rv)))
261 }
262
263 func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
264 e.e.EncodeInt(int64(rvGetInt16(rv)))
265 }
266
267 func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
268 e.e.EncodeInt(int64(rvGetInt32(rv)))
269 }
270
271 func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
272 e.e.EncodeInt(int64(rvGetInt64(rv)))
273 }
274
275 func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
276 e.e.EncodeUint(uint64(rvGetUint(rv)))
277 }
278
279 func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
280 e.e.EncodeUint(uint64(rvGetUint8(rv)))
281 }
282
283 func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
284 e.e.EncodeUint(uint64(rvGetUint16(rv)))
285 }
286
287 func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
288 e.e.EncodeUint(uint64(rvGetUint32(rv)))
289 }
290
291 func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
292 e.e.EncodeUint(uint64(rvGetUint64(rv)))
293 }
294
295 func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
296 e.e.EncodeUint(uint64(rvGetUintptr(rv)))
297 }
298
299 func (e *Encoder) kErr(f *codecFnInfo, rv reflect.Value) {
300 e.errorf("unsupported kind %s, for %#v", rv.Kind(), rv)
301 }
302
303 func chanToSlice(rv reflect.Value, rtslice reflect.Type, timeout time.Duration) (rvcs reflect.Value) {
304 rvcs = rvZeroK(rtslice, reflect.Slice)
305 if timeout < 0 {
306 for {
307 recv, recvOk := rv.Recv()
308 if !recvOk {
309 break
310 }
311 rvcs = reflect.Append(rvcs, recv)
312 }
313 } else {
314 cases := make([]reflect.SelectCase, 2)
315 cases[0] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: rv}
316 if timeout == 0 {
317 cases[1] = reflect.SelectCase{Dir: reflect.SelectDefault}
318 } else {
319 tt := time.NewTimer(timeout)
320 cases[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(tt.C)}
321 }
322 for {
323 chosen, recv, recvOk := reflect.Select(cases)
324 if chosen == 1 || !recvOk {
325 break
326 }
327 rvcs = reflect.Append(rvcs, recv)
328 }
329 }
330 return
331 }
332
333 func (e *Encoder) kSeqFn(rtelem reflect.Type) (fn *codecFn) {
334 for rtelem.Kind() == reflect.Ptr {
335 rtelem = rtelem.Elem()
336 }
337
338
339 if rtelem.Kind() != reflect.Interface {
340 fn = e.h.fn(rtelem)
341 }
342 return
343 }
344
345 func (e *Encoder) kSliceWMbs(rv reflect.Value, ti *typeInfo) {
346 var l = rvLenSlice(rv)
347 if l == 0 {
348 e.mapStart(0)
349 } else {
350 e.haltOnMbsOddLen(l)
351 e.mapStart(l >> 1)
352 fn := e.kSeqFn(ti.elem)
353 for j := 0; j < l; j++ {
354 if j&1 == 0 {
355 e.mapElemKey()
356 } else {
357 e.mapElemValue()
358 }
359 e.encodeValue(rvSliceIndex(rv, j, ti), fn)
360 }
361 }
362 e.mapEnd()
363 }
364
365 func (e *Encoder) kSliceW(rv reflect.Value, ti *typeInfo) {
366 var l = rvLenSlice(rv)
367 e.arrayStart(l)
368 if l > 0 {
369 fn := e.kSeqFn(ti.elem)
370 for j := 0; j < l; j++ {
371 e.arrayElem()
372 e.encodeValue(rvSliceIndex(rv, j, ti), fn)
373 }
374 }
375 e.arrayEnd()
376 }
377
378 func (e *Encoder) kArrayWMbs(rv reflect.Value, ti *typeInfo) {
379 var l = rv.Len()
380 if l == 0 {
381 e.mapStart(0)
382 } else {
383 e.haltOnMbsOddLen(l)
384 e.mapStart(l >> 1)
385 fn := e.kSeqFn(ti.elem)
386 for j := 0; j < l; j++ {
387 if j&1 == 0 {
388 e.mapElemKey()
389 } else {
390 e.mapElemValue()
391 }
392 e.encodeValue(rv.Index(j), fn)
393 }
394 }
395 e.mapEnd()
396 }
397
398 func (e *Encoder) kArrayW(rv reflect.Value, ti *typeInfo) {
399 var l = rv.Len()
400 e.arrayStart(l)
401 if l > 0 {
402 fn := e.kSeqFn(ti.elem)
403 for j := 0; j < l; j++ {
404 e.arrayElem()
405 e.encodeValue(rv.Index(j), fn)
406 }
407 }
408 e.arrayEnd()
409 }
410
411 func (e *Encoder) kChan(f *codecFnInfo, rv reflect.Value) {
412 if f.ti.chandir&uint8(reflect.RecvDir) == 0 {
413 e.errorf("send-only channel cannot be encoded")
414 }
415 if !f.ti.mbs && uint8TypId == rt2id(f.ti.elem) {
416 e.kSliceBytesChan(rv)
417 return
418 }
419 rtslice := reflect.SliceOf(f.ti.elem)
420 rv = chanToSlice(rv, rtslice, e.h.ChanRecvTimeout)
421 ti := e.h.getTypeInfo(rt2id(rtslice), rtslice)
422 if f.ti.mbs {
423 e.kSliceWMbs(rv, ti)
424 } else {
425 e.kSliceW(rv, ti)
426 }
427 }
428
429 func (e *Encoder) kSlice(f *codecFnInfo, rv reflect.Value) {
430 if f.ti.mbs {
431 e.kSliceWMbs(rv, f.ti)
432 } else if f.ti.rtid == uint8SliceTypId || uint8TypId == rt2id(f.ti.elem) {
433 e.e.EncodeStringBytesRaw(rvGetBytes(rv))
434 } else {
435 e.kSliceW(rv, f.ti)
436 }
437 }
438
439 func (e *Encoder) kArray(f *codecFnInfo, rv reflect.Value) {
440 if f.ti.mbs {
441 e.kArrayWMbs(rv, f.ti)
442 } else if handleBytesWithinKArray && uint8TypId == rt2id(f.ti.elem) {
443 e.e.EncodeStringBytesRaw(rvGetArrayBytes(rv, []byte{}))
444 } else {
445 e.kArrayW(rv, f.ti)
446 }
447 }
448
449 func (e *Encoder) kSliceBytesChan(rv reflect.Value) {
450
451
452
453 bs0 := e.blist.peek(32, true)
454 bs := bs0
455
456 irv := rv2i(rv)
457 ch, ok := irv.(<-chan byte)
458 if !ok {
459 ch = irv.(chan byte)
460 }
461
462 L1:
463 switch timeout := e.h.ChanRecvTimeout; {
464 case timeout == 0:
465 for {
466 select {
467 case b := <-ch:
468 bs = append(bs, b)
469 default:
470 break L1
471 }
472 }
473 case timeout > 0:
474 tt := time.NewTimer(timeout)
475 for {
476 select {
477 case b := <-ch:
478 bs = append(bs, b)
479 case <-tt.C:
480
481 break L1
482 }
483 }
484 default:
485 for b := range ch {
486 bs = append(bs, b)
487 }
488 }
489
490 e.e.EncodeStringBytesRaw(bs)
491 e.blist.put(bs)
492 if !byteSliceSameData(bs0, bs) {
493 e.blist.put(bs0)
494 }
495 }
496
497 func (e *Encoder) kStructSfi(f *codecFnInfo) []*structFieldInfo {
498 if e.h.Canonical {
499 return f.ti.sfi.sorted()
500 }
501 return f.ti.sfi.source()
502 }
503
504 func (e *Encoder) kStructNoOmitempty(f *codecFnInfo, rv reflect.Value) {
505 var tisfi []*structFieldInfo
506 if f.ti.toArray || e.h.StructToArray {
507 tisfi = f.ti.sfi.source()
508 e.arrayStart(len(tisfi))
509 for _, si := range tisfi {
510 e.arrayElem()
511 e.encodeValue(si.path.field(rv), nil)
512 }
513 e.arrayEnd()
514 } else {
515 tisfi = e.kStructSfi(f)
516 e.mapStart(len(tisfi))
517 keytyp := f.ti.keyType
518 for _, si := range tisfi {
519 e.mapElemKey()
520 e.kStructFieldKey(keytyp, si.path.encNameAsciiAlphaNum, si.encName)
521 e.mapElemValue()
522 e.encodeValue(si.path.field(rv), nil)
523 }
524 e.mapEnd()
525 }
526 }
527
528 func (e *Encoder) kStructFieldKey(keyType valueType, encNameAsciiAlphaNum bool, encName string) {
529 encStructFieldKey(encName, e.e, e.w(), keyType, encNameAsciiAlphaNum, e.js)
530 }
531
532 func (e *Encoder) kStruct(f *codecFnInfo, rv reflect.Value) {
533 var newlen int
534 ti := f.ti
535 toMap := !(ti.toArray || e.h.StructToArray)
536 var mf map[string]interface{}
537 if ti.flagMissingFielder {
538 mf = rv2i(rv).(MissingFielder).CodecMissingFields()
539 toMap = true
540 newlen += len(mf)
541 } else if ti.flagMissingFielderPtr {
542 rv2 := e.addrRV(rv, ti.rt, ti.ptr)
543 mf = rv2i(rv2).(MissingFielder).CodecMissingFields()
544 toMap = true
545 newlen += len(mf)
546 }
547 tisfi := ti.sfi.source()
548 newlen += len(tisfi)
549
550 var fkvs = e.slist.get(newlen)[:newlen]
551
552 recur := e.h.RecursiveEmptyCheck
553
554 var kv sfiRv
555 var j int
556 if toMap {
557 newlen = 0
558 for _, si := range e.kStructSfi(f) {
559 kv.r = si.path.field(rv)
560 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) {
561 continue
562 }
563 kv.v = si
564 fkvs[newlen] = kv
565 newlen++
566 }
567
568 var mf2s []stringIntf
569 if len(mf) > 0 {
570 mf2s = make([]stringIntf, 0, len(mf))
571 for k, v := range mf {
572 if k == "" {
573 continue
574 }
575 if ti.infoFieldOmitempty && isEmptyValue(reflect.ValueOf(v), e.h.TypeInfos, recur) {
576 continue
577 }
578 mf2s = append(mf2s, stringIntf{k, v})
579 }
580 }
581
582 e.mapStart(newlen + len(mf2s))
583
584
585
586
587
588 if len(mf2s) > 0 && e.h.Canonical {
589 mf2w := make([]encStructFieldObj, newlen+len(mf2s))
590 for j = 0; j < newlen; j++ {
591 kv = fkvs[j]
592 mf2w[j] = encStructFieldObj{kv.v.encName, kv.r, nil, kv.v.path.encNameAsciiAlphaNum, true}
593 }
594 for _, v := range mf2s {
595 mf2w[j] = encStructFieldObj{v.v, reflect.Value{}, v.i, false, false}
596 j++
597 }
598 sort.Sort((encStructFieldObjSlice)(mf2w))
599 for _, v := range mf2w {
600 e.mapElemKey()
601 e.kStructFieldKey(ti.keyType, v.ascii, v.key)
602 e.mapElemValue()
603 if v.isRv {
604 e.encodeValue(v.rv, nil)
605 } else {
606 e.encode(v.intf)
607 }
608 }
609 } else {
610 keytyp := ti.keyType
611 for j = 0; j < newlen; j++ {
612 kv = fkvs[j]
613 e.mapElemKey()
614 e.kStructFieldKey(keytyp, kv.v.path.encNameAsciiAlphaNum, kv.v.encName)
615 e.mapElemValue()
616 e.encodeValue(kv.r, nil)
617 }
618 for _, v := range mf2s {
619 e.mapElemKey()
620 e.kStructFieldKey(keytyp, false, v.v)
621 e.mapElemValue()
622 e.encode(v.i)
623 }
624 }
625
626 e.mapEnd()
627 } else {
628 newlen = len(tisfi)
629 for i, si := range tisfi {
630 kv.r = si.path.field(rv)
631
632
633 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) {
634 switch kv.r.Kind() {
635 case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice:
636 kv.r = reflect.Value{}
637 }
638 }
639 fkvs[i] = kv
640 }
641
642 e.arrayStart(newlen)
643 for j = 0; j < newlen; j++ {
644 e.arrayElem()
645 e.encodeValue(fkvs[j].r, nil)
646 }
647 e.arrayEnd()
648 }
649
650
651
652
653 e.slist.put(fkvs)
654 }
655
656 func (e *Encoder) kMap(f *codecFnInfo, rv reflect.Value) {
657 l := rvLenMap(rv)
658 e.mapStart(l)
659 if l == 0 {
660 e.mapEnd()
661 return
662 }
663
664
665
666
667
668
669
670
671
672 var keyFn, valFn *codecFn
673
674 ktypeKind := reflect.Kind(f.ti.keykind)
675 vtypeKind := reflect.Kind(f.ti.elemkind)
676
677 rtval := f.ti.elem
678 rtvalkind := vtypeKind
679 for rtvalkind == reflect.Ptr {
680 rtval = rtval.Elem()
681 rtvalkind = rtval.Kind()
682 }
683 if rtvalkind != reflect.Interface {
684 valFn = e.h.fn(rtval)
685 }
686
687 var rvv = mapAddrLoopvarRV(f.ti.elem, vtypeKind)
688
689 rtkey := f.ti.key
690 var keyTypeIsString = stringTypId == rt2id(rtkey)
691 if keyTypeIsString {
692 keyFn = e.h.fn(rtkey)
693 } else {
694 for rtkey.Kind() == reflect.Ptr {
695 rtkey = rtkey.Elem()
696 }
697 if rtkey.Kind() != reflect.Interface {
698 keyFn = e.h.fn(rtkey)
699 }
700 }
701
702 if e.h.Canonical {
703 e.kMapCanonical(f.ti, rv, rvv, keyFn, valFn)
704 e.mapEnd()
705 return
706 }
707
708 var rvk = mapAddrLoopvarRV(f.ti.key, ktypeKind)
709
710 var it mapIter
711 mapRange(&it, rv, rvk, rvv, true)
712
713 for it.Next() {
714 e.mapElemKey()
715 if keyTypeIsString {
716 e.e.EncodeString(it.Key().String())
717 } else {
718 e.encodeValue(it.Key(), keyFn)
719 }
720 e.mapElemValue()
721 e.encodeValue(it.Value(), valFn)
722 }
723 it.Done()
724
725 e.mapEnd()
726 }
727
728 func (e *Encoder) kMapCanonical(ti *typeInfo, rv, rvv reflect.Value, keyFn, valFn *codecFn) {
729
730
731
732
733 rtkey := ti.key
734 rtkeydecl := rtkey.PkgPath() == "" && rtkey.Name() != ""
735
736 mks := rv.MapKeys()
737 rtkeyKind := rtkey.Kind()
738 kfast := mapKeyFastKindFor(rtkeyKind)
739 visindirect := mapStoresElemIndirect(uintptr(ti.elemsize))
740 visref := refBitset.isset(ti.elemkind)
741
742 switch rtkeyKind {
743 case reflect.Bool:
744
745
746
747
748
749
750 if len(mks) == 2 && mks[0].Bool() {
751 mks[0], mks[1] = mks[1], mks[0]
752 }
753 for i := range mks {
754 e.mapElemKey()
755 if rtkeydecl {
756 e.e.EncodeBool(mks[i].Bool())
757 } else {
758 e.encodeValueNonNil(mks[i], keyFn)
759 }
760 e.mapElemValue()
761 e.encodeValue(mapGet(rv, mks[i], rvv, kfast, visindirect, visref), valFn)
762 }
763 case reflect.String:
764 mksv := make([]stringRv, len(mks))
765 for i, k := range mks {
766 v := &mksv[i]
767 v.r = k
768 v.v = k.String()
769 }
770 sort.Sort(stringRvSlice(mksv))
771 for i := range mksv {
772 e.mapElemKey()
773 if rtkeydecl {
774 e.e.EncodeString(mksv[i].v)
775 } else {
776 e.encodeValueNonNil(mksv[i].r, keyFn)
777 }
778 e.mapElemValue()
779 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
780 }
781 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
782 mksv := make([]uint64Rv, len(mks))
783 for i, k := range mks {
784 v := &mksv[i]
785 v.r = k
786 v.v = k.Uint()
787 }
788 sort.Sort(uint64RvSlice(mksv))
789 for i := range mksv {
790 e.mapElemKey()
791 if rtkeydecl {
792 e.e.EncodeUint(mksv[i].v)
793 } else {
794 e.encodeValueNonNil(mksv[i].r, keyFn)
795 }
796 e.mapElemValue()
797 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
798 }
799 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
800 mksv := make([]int64Rv, len(mks))
801 for i, k := range mks {
802 v := &mksv[i]
803 v.r = k
804 v.v = k.Int()
805 }
806 sort.Sort(int64RvSlice(mksv))
807 for i := range mksv {
808 e.mapElemKey()
809 if rtkeydecl {
810 e.e.EncodeInt(mksv[i].v)
811 } else {
812 e.encodeValueNonNil(mksv[i].r, keyFn)
813 }
814 e.mapElemValue()
815 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
816 }
817 case reflect.Float32:
818 mksv := make([]float64Rv, len(mks))
819 for i, k := range mks {
820 v := &mksv[i]
821 v.r = k
822 v.v = k.Float()
823 }
824 sort.Sort(float64RvSlice(mksv))
825 for i := range mksv {
826 e.mapElemKey()
827 if rtkeydecl {
828 e.e.EncodeFloat32(float32(mksv[i].v))
829 } else {
830 e.encodeValueNonNil(mksv[i].r, keyFn)
831 }
832 e.mapElemValue()
833 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
834 }
835 case reflect.Float64:
836 mksv := make([]float64Rv, len(mks))
837 for i, k := range mks {
838 v := &mksv[i]
839 v.r = k
840 v.v = k.Float()
841 }
842 sort.Sort(float64RvSlice(mksv))
843 for i := range mksv {
844 e.mapElemKey()
845 if rtkeydecl {
846 e.e.EncodeFloat64(mksv[i].v)
847 } else {
848 e.encodeValueNonNil(mksv[i].r, keyFn)
849 }
850 e.mapElemValue()
851 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
852 }
853 default:
854 if rtkey == timeTyp {
855 mksv := make([]timeRv, len(mks))
856 for i, k := range mks {
857 v := &mksv[i]
858 v.r = k
859 v.v = rv2i(k).(time.Time)
860 }
861 sort.Sort(timeRvSlice(mksv))
862 for i := range mksv {
863 e.mapElemKey()
864 e.e.EncodeTime(mksv[i].v)
865 e.mapElemValue()
866 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
867 }
868 break
869 }
870
871
872
873 bs0 := e.blist.get(len(mks) * 16)
874 mksv := bs0
875 mksbv := make([]bytesRv, len(mks))
876
877 func() {
878
879 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) {
880 e.wb = wb
881 e.bytes = bytes
882 e.c = c
883 e.e.restoreState(state)
884 }(e.wb, e.bytes, e.c, e.e.captureState())
885
886
887 e.wb = bytesEncAppender{mksv[:0], &mksv}
888 e.bytes = true
889 e.c = 0
890 e.e.resetState()
891
892 for i, k := range mks {
893 v := &mksbv[i]
894 l := len(mksv)
895
896 e.c = containerMapKey
897 e.encodeValue(k, nil)
898 e.atEndOfEncode()
899 e.w().end()
900
901 v.r = k
902 v.v = mksv[l:]
903 }
904 }()
905
906 sort.Sort(bytesRvSlice(mksbv))
907 for j := range mksbv {
908 e.mapElemKey()
909 e.encWr.writeb(mksbv[j].v)
910 e.mapElemValue()
911 e.encodeValue(mapGet(rv, mksbv[j].r, rvv, kfast, visindirect, visref), valFn)
912 }
913 e.blist.put(mksv)
914 if !byteSliceSameData(bs0, mksv) {
915 e.blist.put(bs0)
916 }
917 }
918 }
919
920
921
922
923
924
925
926
927
928 type Encoder struct {
929 panicHdl
930
931 e encDriver
932
933 h *BasicHandle
934
935
936 encWr
937
938
939 hh Handle
940
941 blist bytesFreelist
942 err error
943
944
945
946
947
948
949
950
951
952
953
954
955 ci []interface{}
956
957 perType encPerType
958
959 slist sfiRvFreelist
960 }
961
962
963
964
965
966 func NewEncoder(w io.Writer, h Handle) *Encoder {
967 e := h.newEncDriver().encoder()
968 if w != nil {
969 e.Reset(w)
970 }
971 return e
972 }
973
974
975
976
977
978
979 func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
980 e := h.newEncDriver().encoder()
981 if out != nil {
982 e.ResetBytes(out)
983 }
984 return e
985 }
986
987 func (e *Encoder) HandleName() string {
988 return e.hh.Name()
989 }
990
991 func (e *Encoder) init(h Handle) {
992 initHandle(h)
993 e.err = errEncoderNotInitialized
994 e.bytes = true
995 e.hh = h
996 e.h = h.getBasicHandle()
997 e.be = e.hh.isBinary()
998 }
999
1000 func (e *Encoder) w() *encWr {
1001 return &e.encWr
1002 }
1003
1004 func (e *Encoder) resetCommon() {
1005 e.e.reset()
1006 if e.ci != nil {
1007 e.ci = e.ci[:0]
1008 }
1009 e.c = 0
1010 e.calls = 0
1011 e.seq = 0
1012 e.err = nil
1013 }
1014
1015
1016
1017
1018
1019 func (e *Encoder) Reset(w io.Writer) {
1020 e.bytes = false
1021 if e.wf == nil {
1022 e.wf = new(bufioEncWriter)
1023 }
1024 e.wf.reset(w, e.h.WriterBufferSize, &e.blist)
1025 e.resetCommon()
1026 }
1027
1028
1029 func (e *Encoder) ResetBytes(out *[]byte) {
1030 e.bytes = true
1031 e.wb.reset(encInBytes(out), out)
1032 e.resetCommon()
1033 }
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 func (e *Encoder) Encode(v interface{}) (err error) {
1121
1122
1123
1124 if !debugging {
1125 defer func() {
1126
1127
1128 if x := recover(); x != nil {
1129 panicValToErr(e, x, &e.err)
1130 err = e.err
1131 }
1132 }()
1133 }
1134
1135 e.MustEncode(v)
1136 return
1137 }
1138
1139
1140
1141
1142 func (e *Encoder) MustEncode(v interface{}) {
1143 halt.onerror(e.err)
1144 if e.hh == nil {
1145 halt.onerror(errNoFormatHandle)
1146 }
1147
1148 e.calls++
1149 e.encode(v)
1150 e.calls--
1151 if e.calls == 0 {
1152 e.atEndOfEncode()
1153 e.w().end()
1154 }
1155 }
1156
1157
1158
1159
1160
1161 func (e *Encoder) Release() {
1162 }
1163
1164 func (e *Encoder) encode(iv interface{}) {
1165
1166
1167
1168 if iv == nil {
1169 e.e.EncodeNil()
1170 return
1171 }
1172
1173 rv, ok := isNil(iv)
1174 if ok {
1175 e.e.EncodeNil()
1176 return
1177 }
1178
1179 switch v := iv.(type) {
1180
1181
1182 case Raw:
1183 e.rawBytes(v)
1184 case reflect.Value:
1185 e.encodeValue(v, nil)
1186
1187 case string:
1188 e.e.EncodeString(v)
1189 case bool:
1190 e.e.EncodeBool(v)
1191 case int:
1192 e.e.EncodeInt(int64(v))
1193 case int8:
1194 e.e.EncodeInt(int64(v))
1195 case int16:
1196 e.e.EncodeInt(int64(v))
1197 case int32:
1198 e.e.EncodeInt(int64(v))
1199 case int64:
1200 e.e.EncodeInt(v)
1201 case uint:
1202 e.e.EncodeUint(uint64(v))
1203 case uint8:
1204 e.e.EncodeUint(uint64(v))
1205 case uint16:
1206 e.e.EncodeUint(uint64(v))
1207 case uint32:
1208 e.e.EncodeUint(uint64(v))
1209 case uint64:
1210 e.e.EncodeUint(v)
1211 case uintptr:
1212 e.e.EncodeUint(uint64(v))
1213 case float32:
1214 e.e.EncodeFloat32(v)
1215 case float64:
1216 e.e.EncodeFloat64(v)
1217 case complex64:
1218 e.encodeComplex64(v)
1219 case complex128:
1220 e.encodeComplex128(v)
1221 case time.Time:
1222 e.e.EncodeTime(v)
1223 case []byte:
1224 e.e.EncodeStringBytesRaw(v)
1225 case *Raw:
1226 e.rawBytes(*v)
1227 case *string:
1228 e.e.EncodeString(*v)
1229 case *bool:
1230 e.e.EncodeBool(*v)
1231 case *int:
1232 e.e.EncodeInt(int64(*v))
1233 case *int8:
1234 e.e.EncodeInt(int64(*v))
1235 case *int16:
1236 e.e.EncodeInt(int64(*v))
1237 case *int32:
1238 e.e.EncodeInt(int64(*v))
1239 case *int64:
1240 e.e.EncodeInt(*v)
1241 case *uint:
1242 e.e.EncodeUint(uint64(*v))
1243 case *uint8:
1244 e.e.EncodeUint(uint64(*v))
1245 case *uint16:
1246 e.e.EncodeUint(uint64(*v))
1247 case *uint32:
1248 e.e.EncodeUint(uint64(*v))
1249 case *uint64:
1250 e.e.EncodeUint(*v)
1251 case *uintptr:
1252 e.e.EncodeUint(uint64(*v))
1253 case *float32:
1254 e.e.EncodeFloat32(*v)
1255 case *float64:
1256 e.e.EncodeFloat64(*v)
1257 case *complex64:
1258 e.encodeComplex64(*v)
1259 case *complex128:
1260 e.encodeComplex128(*v)
1261 case *time.Time:
1262 e.e.EncodeTime(*v)
1263 case *[]byte:
1264 if *v == nil {
1265 e.e.EncodeNil()
1266 } else {
1267 e.e.EncodeStringBytesRaw(*v)
1268 }
1269 default:
1270
1271 if skipFastpathTypeSwitchInDirectCall || !fastpathEncodeTypeSwitch(iv, e) {
1272 e.encodeValue(rv, nil)
1273 }
1274 }
1275 }
1276
1277
1278
1279
1280
1281 func (e *Encoder) encodeValue(rv reflect.Value, fn *codecFn) {
1282
1283
1284
1285
1286 var sptr interface{}
1287 var rvp reflect.Value
1288 var rvpValid bool
1289 TOP:
1290 switch rv.Kind() {
1291 case reflect.Ptr:
1292 if rvIsNil(rv) {
1293 e.e.EncodeNil()
1294 return
1295 }
1296 rvpValid = true
1297 rvp = rv
1298 rv = rv.Elem()
1299 goto TOP
1300 case reflect.Interface:
1301 if rvIsNil(rv) {
1302 e.e.EncodeNil()
1303 return
1304 }
1305 rvpValid = false
1306 rvp = reflect.Value{}
1307 rv = rv.Elem()
1308 goto TOP
1309 case reflect.Struct:
1310 if rvpValid && e.h.CheckCircularRef {
1311 sptr = rv2i(rvp)
1312 for _, vv := range e.ci {
1313 if eq4i(sptr, vv) {
1314 e.errorf("circular reference found: %p, %T", sptr, sptr)
1315 }
1316 }
1317 e.ci = append(e.ci, sptr)
1318 }
1319 case reflect.Slice, reflect.Map, reflect.Chan:
1320 if rvIsNil(rv) {
1321 e.e.EncodeNil()
1322 return
1323 }
1324 case reflect.Invalid, reflect.Func:
1325 e.e.EncodeNil()
1326 return
1327 }
1328
1329 if fn == nil {
1330 fn = e.h.fn(rv.Type())
1331 }
1332
1333 if !fn.i.addrE {
1334
1335 } else if rvpValid {
1336 rv = rvp
1337 } else {
1338 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr)
1339 }
1340 fn.fe(e, &fn.i, rv)
1341
1342 if sptr != nil {
1343 e.ci = e.ci[:len(e.ci)-1]
1344 }
1345 }
1346
1347
1348
1349 func (e *Encoder) encodeValueNonNil(rv reflect.Value, fn *codecFn) {
1350 if fn == nil {
1351 fn = e.h.fn(rv.Type())
1352 }
1353
1354 if fn.i.addrE {
1355 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr)
1356 }
1357 fn.fe(e, &fn.i, rv)
1358 }
1359
1360
1361 func (e *Encoder) addrRV(rv reflect.Value, typ, ptrType reflect.Type) (rva reflect.Value) {
1362 if rv.CanAddr() {
1363 return rvAddr(rv, ptrType)
1364 }
1365 if e.h.NoAddressableReadonly {
1366 rva = reflect.New(typ)
1367 rvSetDirect(rva.Elem(), rv)
1368 return
1369 }
1370 return rvAddr(e.perType.AddressableRO(rv), ptrType)
1371 }
1372
1373 func (e *Encoder) marshalUtf8(bs []byte, fnerr error) {
1374 e.onerror(fnerr)
1375 if bs == nil {
1376 e.e.EncodeNil()
1377 } else {
1378 e.e.EncodeString(stringView(bs))
1379 }
1380 }
1381
1382 func (e *Encoder) marshalAsis(bs []byte, fnerr error) {
1383 e.onerror(fnerr)
1384 if bs == nil {
1385 e.e.EncodeNil()
1386 } else {
1387 e.encWr.writeb(bs)
1388 }
1389 }
1390
1391 func (e *Encoder) marshalRaw(bs []byte, fnerr error) {
1392 e.onerror(fnerr)
1393 if bs == nil {
1394 e.e.EncodeNil()
1395 } else {
1396 e.e.EncodeStringBytesRaw(bs)
1397 }
1398 }
1399
1400 func (e *Encoder) rawBytes(vv Raw) {
1401 v := []byte(vv)
1402 if !e.h.Raw {
1403 e.errorf("Raw values cannot be encoded: %v", v)
1404 }
1405 e.encWr.writeb(v)
1406 }
1407
1408 func (e *Encoder) wrapErr(v error, err *error) {
1409 *err = wrapCodecErr(v, e.hh.Name(), 0, true)
1410 }
1411
1412
1413
1414
1415
1416 func (e *Encoder) mapStart(length int) {
1417 e.e.WriteMapStart(length)
1418 e.c = containerMapStart
1419 }
1420
1421 func (e *Encoder) mapElemKey() {
1422 if e.js {
1423 e.jsondriver().WriteMapElemKey()
1424 }
1425 e.c = containerMapKey
1426 }
1427
1428 func (e *Encoder) mapElemValue() {
1429 if e.js {
1430 e.jsondriver().WriteMapElemValue()
1431 }
1432 e.c = containerMapValue
1433 }
1434
1435 func (e *Encoder) mapEnd() {
1436 e.e.WriteMapEnd()
1437 e.c = 0
1438 }
1439
1440 func (e *Encoder) arrayStart(length int) {
1441 e.e.WriteArrayStart(length)
1442 e.c = containerArrayStart
1443 }
1444
1445 func (e *Encoder) arrayElem() {
1446 if e.js {
1447 e.jsondriver().WriteArrayElem()
1448 }
1449 e.c = containerArrayElem
1450 }
1451
1452 func (e *Encoder) arrayEnd() {
1453 e.e.WriteArrayEnd()
1454 e.c = 0
1455 }
1456
1457
1458
1459 func (e *Encoder) haltOnMbsOddLen(length int) {
1460 if length&1 != 0 {
1461 e.errorf("mapBySlice requires even slice length, but got %v", length)
1462 }
1463 }
1464
1465 func (e *Encoder) atEndOfEncode() {
1466
1467 if e.js {
1468 e.jsondriver().atEndOfEncode()
1469 }
1470 }
1471
1472 func (e *Encoder) sideEncode(v interface{}, basetype reflect.Type, bs *[]byte) {
1473
1474
1475
1476
1477
1478
1479 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) {
1480 e.wb = wb
1481 e.bytes = bytes
1482 e.c = c
1483 e.e.restoreState(state)
1484 }(e.wb, e.bytes, e.c, e.e.captureState())
1485
1486 e.wb = bytesEncAppender{encInBytes(bs)[:0], bs}
1487 e.bytes = true
1488 e.c = 0
1489 e.e.resetState()
1490
1491
1492 rv := baseRV(v)
1493 e.encodeValue(rv, e.h.fnNoExt(basetype))
1494 e.atEndOfEncode()
1495 e.w().end()
1496 }
1497
1498 func encInBytes(out *[]byte) (in []byte) {
1499 in = *out
1500 if in == nil {
1501 in = make([]byte, defEncByteBufSize)
1502 }
1503 return
1504 }
1505
1506 func encStructFieldKey(encName string, ee encDriver, w *encWr,
1507 keyType valueType, encNameAsciiAlphaNum bool, js bool) {
1508
1509
1510
1511 if keyType == valueTypeString {
1512 if js && encNameAsciiAlphaNum {
1513 w.writeqstr(encName)
1514 } else {
1515 ee.EncodeString(encName)
1516 }
1517 } else if keyType == valueTypeInt {
1518 ee.EncodeInt(must.Int(strconv.ParseInt(encName, 10, 64)))
1519 } else if keyType == valueTypeUint {
1520 ee.EncodeUint(must.Uint(strconv.ParseUint(encName, 10, 64)))
1521 } else if keyType == valueTypeFloat {
1522 ee.EncodeFloat64(must.Float(strconv.ParseFloat(encName, 64)))
1523 } else {
1524 halt.errorf("invalid struct key type: %v", keyType)
1525 }
1526 }
1527
View as plain text