-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserializers.go
45 lines (38 loc) · 959 Bytes
/
serializers.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
package botmeans
import (
"encoding/json"
"reflect"
)
func serialize(current string, value interface{}) string {
if current == "" {
current = "{}"
}
if value == nil {
return current
}
container := make(map[string]*json.RawMessage)
json.Unmarshal([]byte(current), &container)
d, _ := json.Marshal(value)
rm := json.RawMessage(d)
t := ""
if reflect.TypeOf(value).Kind() != reflect.Ptr {
t = reflect.TypeOf(value).Name()
} else {
t = reflect.Indirect(reflect.ValueOf(value)).Type().Name()
}
container[t] = &rm
d, _ = json.Marshal(container)
current = string(d)
return current
}
func deserialize(current string, value interface{}) {
if value == nil || reflect.TypeOf(value).Kind() != reflect.Ptr {
return
}
t := reflect.Indirect(reflect.ValueOf(value)).Type().Name()
container := make(map[string]*json.RawMessage)
json.Unmarshal([]byte(current), &container)
if v, ok := container[t]; ok {
json.Unmarshal(*v, value)
}
}