-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
161 lines (138 loc) · 4 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package jsoniterpb
import (
"fmt"
"reflect"
"unsafe"
jsoniter "github.com/json-iterator/go"
"github.com/modern-go/reflect2"
)
type stringModeNumberEncoder struct {
elemEncoder jsoniter.ValEncoder
}
var singleQuote = []byte{'"'}
func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
tempStream := stream.API().BorrowStream(nil)
tempStream.Attachment = stream.Attachment
defer stream.API().ReturnStream(tempStream)
encoder.elemEncoder.Encode(ptr, tempStream)
buf := tempStream.Buffer()
if len(buf) >= 2 && buf[0] == '"' && buf[len(buf)-1] == '"' {
stream.Write(buf)
} else {
stream.Write(singleQuote)
stream.Write(buf)
stream.Write(singleQuote)
}
}
func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.elemEncoder.IsEmpty(ptr)
}
type stringModeNumberDecoder struct {
elemDecoder jsoniter.ValDecoder
}
func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if iter.WhatIsNext() == jsoniter.StringValue {
iter.NextToken()
decoder.elemDecoder.Decode(ptr, iter)
iter.NextToken()
return
}
decoder.elemDecoder.Decode(ptr, iter)
}
type funcEncoder struct {
fun jsoniter.EncoderFunc
isEmptyFunc func(ptr unsafe.Pointer) bool
}
func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
encoder.fun(ptr, stream)
}
func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
if encoder.isEmptyFunc == nil {
return false
}
return encoder.isEmptyFunc(ptr)
}
type funcDecoder struct {
fun jsoniter.DecoderFunc
}
func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
decoder.fun(ptr, iter)
}
type dynamicEncoder struct {
valType reflect2.Type
}
func (encoder *dynamicEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
obj := encoder.valType.UnsafeIndirect(ptr)
stream.WriteVal(obj)
}
func (encoder *dynamicEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.valType.UnsafeIndirect(ptr) == nil
}
type OptionalEncoder struct {
ValueEncoder jsoniter.ValEncoder
IfNil func(stream *jsoniter.Stream)
}
func (encoder *OptionalEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
if *((*unsafe.Pointer)(ptr)) == nil {
if encoder.IfNil != nil {
encoder.IfNil(stream)
} else {
stream.WriteNil()
}
} else {
encoder.ValueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
}
}
func (encoder *OptionalEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return *((*unsafe.Pointer)(ptr)) == nil
}
type OptionalDecoder struct {
ValueType reflect2.Type
ValueDecoder jsoniter.ValDecoder
IfNil func(ptr unsafe.Pointer)
}
func (decoder *OptionalDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
if iter.ReadNil() {
if decoder.IfNil != nil {
decoder.IfNil(ptr)
return
}
*((*unsafe.Pointer)(ptr)) = nil
} else {
if *((*unsafe.Pointer)(ptr)) == nil {
//pointer to null, we have to allocate memory to hold the value
newPtr := decoder.ValueType.UnsafeNew()
decoder.ValueDecoder.Decode(newPtr, iter)
*((*unsafe.Pointer)(ptr)) = newPtr
} else {
//reuse existing instance
decoder.ValueDecoder.Decode(*((*unsafe.Pointer)(ptr)), iter)
}
}
}
func WrapElemEncoder(typ reflect2.Type, enc jsoniter.ValEncoder, ifNil func(stream *jsoniter.Stream)) jsoniter.ValEncoder {
if typ.Kind() == reflect.Ptr {
if typ.(reflect2.PtrType).Elem().Kind() == reflect.Struct {
return &OptionalEncoder{
ValueEncoder: enc,
IfNil: ifNil,
}
}
panic(fmt.Sprintf("WrapElemEncoderWithIfNil does not support type %v", typ))
}
return enc
}
func WrapElemDecoder(typ reflect2.Type, dec jsoniter.ValDecoder, ifNil func(ptr unsafe.Pointer)) jsoniter.ValDecoder {
if typ.Kind() == reflect.Ptr {
elemType := typ.(reflect2.PtrType).Elem()
if elemType.Kind() == reflect.Struct {
return &OptionalDecoder{
ValueType: elemType,
ValueDecoder: dec,
IfNil: ifNil,
}
}
panic(fmt.Sprintf("WrapElemDecoderIfNil does not support type %v", typ))
}
return dec
}