-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgopherjson.go
189 lines (164 loc) · 5.22 KB
/
gopherjson.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gopherjson
import (
"fmt"
"reflect"
)
type SerializableValue interface {
Serialize() interface{}
Deserialize(interface{}) error
}
// Serialize converts a value into its serializable form.
func Serialize(value interface{}) (interface{}, error) {
if sv, ok := value.(SerializableValue); ok {
return sv.Serialize(), nil
}
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Map:
result := make(map[string]interface{})
iter := rv.MapRange()
for iter.Next() {
key := iter.Key().Interface().(string)
val := iter.Value().Interface()
serializedVal, err := Serialize(val)
if err != nil {
return nil, fmt.Errorf("error serializing map value: %w", err)
}
result[key] = serializedVal
}
return result, nil
case reflect.Slice:
result := make([]interface{}, rv.Len())
for i := 0; i < rv.Len(); i++ {
val := rv.Index(i).Interface()
serializedVal, err := Serialize(val)
if err != nil {
return nil, fmt.Errorf("error serializing slice element: %w", err)
}
result[i] = serializedVal
}
return result, nil
default:
return value, nil
}
}
// Deserialize converts a serialized value into its original form.
func Deserialize(serialized interface{}, value interface{}) error {
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("value is not a valid pointer")
}
rv = rv.Elem()
rt := rv.Type()
switch v := serialized.(type) {
case map[string]interface{}:
switch rv.Kind() {
case reflect.Struct:
// Iterate over the struct fields and deserialize each field
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
fieldType := rt.Field(i)
fieldName := fieldType.Name
if !field.CanSet() {
continue
}
// Check if the field implements the SerializableValue interface
if field.CanAddr() {
if sv, ok := field.Addr().Interface().(SerializableValue); ok {
if fieldValue, ok := v[fieldName]; ok {
if err := sv.Deserialize(fieldValue); err != nil {
return fmt.Errorf("error deserializing field %s: %v", fieldName, err)
}
}
continue
}
}
// Check if the field has a custom JSON tag
tag := fieldType.Tag.Get("json")
if tag != "" {
fieldName = tag
}
// Deserialize the field value
if fieldValue, ok := v[fieldName]; ok {
if err := Deserialize(fieldValue, field.Addr().Interface()); err != nil {
return fmt.Errorf("error deserializing field %s: %v", fieldName, err)
}
}
}
case reflect.Map:
// Create a new map with the same type as the target value
mapType := reflect.MapOf(rt.Key(), rt.Elem())
mapValue := reflect.MakeMap(mapType)
// Deserialize each key-value pair in the map
for key, val := range v {
keyValue := reflect.New(rt.Key()).Elem()
valValue := reflect.New(rt.Elem()).Elem()
// Deserialize the key and value
if err := Deserialize(key, keyValue.Addr().Interface()); err != nil {
return fmt.Errorf("error deserializing map key: %v", err)
}
if err := Deserialize(val, valValue.Addr().Interface()); err != nil {
return fmt.Errorf("error deserializing map value: %v", err)
}
// Assign the key-value pair to the map
mapValue.SetMapIndex(keyValue, valValue)
}
// Set the deserialized map as the value
rv.Set(mapValue)
default:
return fmt.Errorf("unsupported type for deserialization: %v", rv.Kind())
}
case []interface{}:
switch rv.Kind() {
case reflect.Slice:
// Create a new slice with the same type as the target value
sliceType := reflect.SliceOf(rt.Elem())
sliceValue := reflect.MakeSlice(sliceType, len(v), len(v))
// Deserialize each element in the slice
for i := 0; i < len(v); i++ {
elemValue := reflect.New(rt.Elem()).Elem()
// Deserialize the element
if err := Deserialize(v[i], elemValue.Addr().Interface()); err != nil {
return fmt.Errorf("error deserializing slice element: %v", err)
}
// Assign the element to the slice
sliceValue.Index(i).Set(elemValue)
}
// Set the deserialized slice as the value
rv.Set(sliceValue)
default:
return fmt.Errorf("unsupported type for deserialization: %v", rv.Kind())
}
case string:
switch rv.Kind() {
case reflect.Struct:
if rv.Type() == reflect.TypeOf(CustomDate{}) {
cd := CustomDate{}
if err := cd.UnmarshalJSON([]byte(v)); err != nil {
return fmt.Errorf("error deserializing CustomDate: %v", err)
}
rv.Set(reflect.ValueOf(cd))
} else if rv.Type() == reflect.TypeOf(CustomRegex{}) {
cr := CustomRegex{}
if err := cr.UnmarshalJSON([]byte(v)); err != nil {
return fmt.Errorf("error deserializing CustomRegex: %v", err)
}
rv.Set(reflect.ValueOf(cr))
} else if rv.Type() == reflect.TypeOf(CustomFunction{}) {
cf := CustomFunction{}
if err := cf.UnmarshalJSON([]byte(v)); err != nil {
return fmt.Errorf("error deserializing CustomFunction: %v", err)
}
rv.Set(reflect.ValueOf(cf))
} else {
return fmt.Errorf("unsupported type for deserialization: %v", rv.Kind())
}
default:
return fmt.Errorf("unsupported type for deserialization: %v", rv.Kind())
}
default:
// Set the serialized value directly if it's a primitive type
rv.Set(reflect.ValueOf(serialized))
}
return nil
}