forked from bigdrum/phpserialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder.go
153 lines (142 loc) · 3.71 KB
/
encoder.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
package phpserialize
import (
"bytes"
"fmt"
"strconv"
)
func Encode(value interface{}) (result string, err error) {
buf := new(bytes.Buffer)
err = encodeValue(buf, value)
if err == nil {
result = buf.String()
}
return
}
func encodeValue(buf *bytes.Buffer, value interface{}) (err error) {
switch t := value.(type) {
default:
err = fmt.Errorf("Unexpected type %T", t)
case bool:
buf.WriteString("b")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
if t {
buf.WriteString("1")
} else {
buf.WriteString("0")
}
buf.WriteRune(VALUES_SEPARATOR)
case nil:
buf.WriteString("N")
buf.WriteRune(VALUES_SEPARATOR)
case int, int64, int32, int16, int8:
buf.WriteString("i")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
strValue := fmt.Sprintf("%v", t)
buf.WriteString(strValue)
buf.WriteRune(VALUES_SEPARATOR)
case float32:
buf.WriteString("d")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
strValue := strconv.FormatFloat(float64(t), 'f', -1, 64)
buf.WriteString(strValue)
buf.WriteRune(VALUES_SEPARATOR)
case float64:
buf.WriteString("d")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
strValue := strconv.FormatFloat(float64(t), 'f', -1, 64)
buf.WriteString(strValue)
buf.WriteRune(VALUES_SEPARATOR)
case string:
buf.WriteString("s")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
encodeString(buf, t)
buf.WriteRune(VALUES_SEPARATOR)
case map[string]interface{}, []interface{}, map[interface{}]interface{}:
buf.WriteString("a")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
err = encodeArrayCore(buf, t)
case *PhpObject:
buf.WriteString("O")
buf.WriteRune(TYPE_VALUE_SEPARATOR)
encodeString(buf, t.GetClassName())
buf.WriteRune(TYPE_VALUE_SEPARATOR)
err = encodeArrayCore(buf, t.GetMembers())
}
return
}
func encodeString(buf *bytes.Buffer, strValue string) {
valLen := strconv.Itoa(len(strValue))
buf.WriteString(valLen)
buf.WriteRune(TYPE_VALUE_SEPARATOR)
buf.WriteRune('"')
buf.WriteString(strValue)
buf.WriteRune('"')
}
func rangeArrayCore(buf *bytes.Buffer, arrValue interface{}) (err error) {
if arrVal, ok := arrValue.([]interface{}); ok {
for k, v := range arrVal {
if intKey, _err := strconv.Atoi(fmt.Sprintf("%v", k)); _err == nil {
if err = encodeValue(buf, intKey); err != nil {
break
}
} else {
if err = encodeValue(buf, k); err != nil {
break
}
}
if err = encodeValue(buf, v); err != nil {
break
}
}
} else if arrVal, ok := arrValue.(map[interface{}]interface{}); ok {
for k, v := range arrVal {
if intKey, _err := strconv.Atoi(fmt.Sprintf("%v", k)); _err == nil {
if err = encodeValue(buf, intKey); err != nil {
break
}
} else {
if err = encodeValue(buf, k); err != nil {
break
}
}
if err = encodeValue(buf, v); err != nil {
break
}
}
} else if arrVal, ok := arrValue.(map[string]interface{}); ok {
for k, v := range arrVal {
if intKey, _err := strconv.Atoi(fmt.Sprintf("%v", k)); _err == nil {
if err = encodeValue(buf, intKey); err != nil {
break
}
} else {
if err = encodeValue(buf, k); err != nil {
break
}
}
if err = encodeValue(buf, v); err != nil {
break
}
}
}
return err
}
func encodeArrayCore(buf *bytes.Buffer, arrValue interface{}) (err error) {
valLen := ""
if arr, ok := arrValue.([]interface{}); ok {
valLen = strconv.Itoa(len(arr))
} else if arr, ok := arrValue.(map[interface{}]interface{}); ok {
valLen = strconv.Itoa(len(arr))
} else if arr, ok := arrValue.(map[string]interface{}); ok {
valLen = strconv.Itoa(len(arr))
}
if "" == valLen {
return fmt.Errorf("Unexpected type of array %T", arrValue)
}
buf.WriteString(valLen)
buf.WriteRune(TYPE_VALUE_SEPARATOR)
buf.WriteRune('{')
err = rangeArrayCore(buf, arrValue)
buf.WriteRune('}')
return err
}