-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.go
65 lines (61 loc) · 1.74 KB
/
adapter.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
package jsoniter
import (
"fmt"
"reflect"
)
type TypeAdapter interface {
Type() interface{}
Unmarshal(iter *Iterator, out interface{})
Marshal(stream *Stream, val interface{})
}
type JsonAdapter struct {
Unmarshal func(bytes []byte, out interface{}) error
Marshal func(val interface{}) ([]byte, error)
MarshalIndent func(val interface{}, prefix, indent string) ([]byte, error)
}
func CreateJsonAdapter(adapters ...TypeAdapter) JsonAdapter {
adapterMap := make(map[reflect.Type]TypeAdapter)
for _, adapter := range adapters {
t := reflect.ValueOf(adapter.Type()).Type()
adapterMap[t] = adapter
}
return JsonAdapter{
Unmarshal: func(bytes []byte, out interface{}) error {
t := reflect.ValueOf(out).Type()
adapter := adapterMap[t.Elem()]
if adapter == nil {
if t.Kind() != reflect.Ptr {
return fmt.Errorf("unmarshal expect pointer, actual type is: %s", t)
}
return fmt.Errorf("unknown type: %s", t)
}
iter := ParseBytes(bytes)
adapter.Unmarshal(iter, out)
return iter.Error
},
Marshal: func(val interface{}) ([]byte, error) {
t := reflect.ValueOf(val).Type()
adapter := adapterMap[t]
if adapter == nil {
fmt.Println("!!!", adapterMap)
fmt.Println("!!!", t)
return nil, fmt.Errorf("unknown type: %s", t)
}
stream := NewStream()
adapter.Marshal(stream, val)
return stream.Buffer(), stream.Error
},
MarshalIndent: func(val interface{}, prefix, indent string) ([]byte, error) {
t := reflect.ValueOf(val).Type()
adapter := adapterMap[t]
if adapter == nil {
return nil, fmt.Errorf("unknown type: %s", t)
}
stream := NewStream()
stream.Prefix = prefix
stream.Indent = indent
adapter.Marshal(stream, val)
return stream.Buffer(), stream.Error
},
}
}