-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
120 lines (107 loc) · 3.48 KB
/
encode.go
File metadata and controls
120 lines (107 loc) · 3.48 KB
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
package eson
import (
"encoding/json"
"github.com/alt4dev/eson/extension"
"reflect"
)
// EncodeWithTag is similar to Encode but allows an extra tag parameter instead of the default Tag
func EncodeWithTag(tag string, goObject interface{}, pretty bool, extensions ...extension.Extension) (string, error) {
// Use default extensions if None are provided in the call.
if len(extensions) == 0 {
extensions = DefaultExtensions
}
goObject = preProcess(tag, goObject, extensions)
var resp []byte
var err error
if pretty {
resp, err = json.MarshalIndent(goObject, "", " ")
} else {
resp, err = json.Marshal(goObject)
}
if err != nil {
return "", err
}
return string(resp), nil
}
// Encode will convert the object provided to a JSON string.
// you can add extensions to use instead of the default extensions by including them as parameters to this function.
func Encode(goObject interface{}, pretty bool, extensions ...extension.Extension) (string, error) {
return EncodeWithTag(*tagName, goObject, pretty, extensions...)
}
func preProcess(tagToUse string, goObject interface{}, extensions []extension.Extension) (processedObject interface{}) {
objectValue := reflect.ValueOf(goObject)
if objectValue.Type().Kind() == reflect.Ptr {
if objectValue.IsZero() {
return nil
}
objectValue = reflect.Indirect(objectValue)
goObject = objectValue.Interface()
}
processedObject = goObject
switch objectValue.Type().Kind() {
case reflect.Map:
processedObject = processMap(tagToUse, goObject.(map[string]interface{}), extensions)
break
case reflect.Struct:
processedObject = processStruct(tagToUse, goObject, extensions)
break
case reflect.Slice, reflect.Array:
goArray := make([]interface{}, 0)
for i := 0; i < objectValue.Len(); i++ {
goArray = append(goArray, objectValue.Index(i).Interface())
}
processedObject = processArray(tagToUse, goArray, extensions)
break
}
return processedObject
}
func processMap(tagToUse string, theMap map[string]interface{}, extensions []extension.Extension) interface{} {
encoded := make(map[string]interface{})
for key, value := range theMap {
encodedKey, encodedValue := extension.EncodeValue(key, value, extensions)
if key == encodedKey {
// Preprocess the value
encodedValue = preProcess(tagToUse, value, extensions)
}
encoded[encodedKey] = encodedValue
}
return encoded
}
func processStruct(tagToUse string, theStruct interface{}, extensions []extension.Extension) interface{} {
encoded := make(map[string]interface{})
structType := reflect.TypeOf(theStruct)
structValue := reflect.ValueOf(theStruct)
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
// Ignore non-exported fields
if !field.IsExported() {
continue
}
key := field.Name
if jsonTag, ok := field.Tag.Lookup(tagToUse); ok {
if jsonTag == "-" {
continue
}
if jsonTag != "" {
key = jsonTag
}
}
value := structValue.FieldByName(field.Name)
encoded[key] = value.Interface()
}
return preProcess(tagToUse, encoded, extensions)
}
func processArray(tagToUse string, theArray []interface{}, extensions []extension.Extension) interface{} {
arrayOut := make([]interface{}, len(theArray))
for i, value := range theArray {
key, encodedValue := extension.EncodeValue("", value, extensions)
if key != "" {
encodedValue = map[string]interface{}{key: encodedValue}
} else {
// Preprocess the value
encodedValue = preProcess(tagToUse, encodedValue, extensions)
}
arrayOut[i] = encodedValue
}
return arrayOut
}