-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.go
executable file
·59 lines (48 loc) · 1.24 KB
/
extension.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
package fastjson
import (
"github.com/json-iterator/go"
"unsafe"
)
type ExtensionEnum uint8
const (
_ ExtensionEnum = iota
CUSTOM_TIME_EXT
METHOD_VALUE_EXT
)
var (
FastJson = jsoniter.ConfigCompatibleWithStandardLibrary
)
func RegisterExt(extension ExtensionEnum) {
switch extension {
case CUSTOM_TIME_EXT:
FastJson.RegisterExtension(&CustomTimeExtension{})
break
case METHOD_VALUE_EXT:
FastJson.RegisterExtension(&MethodValueExtension{})
break
default:
return
}
}
func UnregisterExt(extension ExtensionEnum) {
// todo: json-iterator没有实现且没有暴露[]extension变量,可能需要自己fork来搞一下
}
type funcDecoder struct {
fun jsoniter.DecoderFunc
}
func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
decoder.fun(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)
}