Skip to content

Commit 5bb5ecf

Browse files
authored
Adding serialization of objects with arrays (#24)
1 parent 5004667 commit 5bb5ecf

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

consume.go

+23
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ func setField(structFieldValue reflect.Value, value interface{}) error {
189189
m := val.Interface().(map[interface{}]interface{})
190190
fillStruct(structFieldValue, m)
191191

192+
case reflect.Slice:
193+
l := val.Len()
194+
arrayOfObjects := reflect.MakeSlice(structFieldValue.Type(), l, l)
195+
196+
for i := 0; i < l; i++ {
197+
if m, ok := val.Index(i).Interface().(map[interface{}]interface{}); ok {
198+
obj := arrayOfObjects.Index(i)
199+
fillStruct(obj, m)
200+
} else {
201+
switch arrayOfObjects.Index(i).Kind() {
202+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
203+
arrayOfObjects.Index(i).SetInt(val.Index(i).Elem().Int())
204+
case reflect.Float32, reflect.Float64:
205+
arrayOfObjects.Index(i).SetFloat(val.Index(i).Elem().Float())
206+
default:
207+
arrayOfObjects.Index(i).Set(val.Index(i).Elem())
208+
}
209+
210+
}
211+
}
212+
213+
structFieldValue.Set(arrayOfObjects)
214+
192215
default:
193216
structFieldValue.Set(val)
194217
}

serialize_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ type Struct2 struct {
2626
Qux float64
2727
}
2828

29+
type Struct3 struct {
30+
ObjectArray []Struct2
31+
IntArray []int64
32+
FloatArray []float64
33+
StringArray []string
34+
}
35+
2936
type marshalTest struct {
3037
input interface{}
3138
output []byte
@@ -134,6 +141,13 @@ var marshalTests = map[string]marshalTest{
134141
nil,
135142
},
136143

144+
// encode object with array of objects
145+
"struct3{ObjectArray Struct2{Qux float64}, IntArray {1, 2}, FloatArray {1.0, 2.0}, StringArray {'a', 'b'}}": {
146+
Struct3{[]Struct2{{1.1}, {2.2}}, []int64{1, 2}, []float64{1.0, 2.0}, []string{"a", "b"}},
147+
[]byte("O:7:\"Struct3\":4:{s:11:\"objectArray\";a:2:{i:0;O:7:\"Struct2\":1:{s:3:\"qux\";d:1.1;}i:1;O:7:\"Struct2\":1:{s:3:\"qux\";d:2.2;}}s:8:\"intArray\";a:2:{i:0;i:1;i:1;i:2;}s:10:\"floatArray\";a:2:{i:0;d:1;i:1;d:2;}s:11:\"stringArray\";a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}}"),
148+
nil,
149+
},
150+
137151
// encode object (struct with tags)
138152
"structTag{Bar int, Foo Struct2{Qux float64}, hidden bool, Balu string, Nilptr <nil>}": {
139153
structTag{Struct2{1.23}, 10, true, "yay", "", nil},

unserialize_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ func TestUnmarshalArrayThatContainsInteger(t *testing.T) {
598598
}
599599
}
600600

601+
func TestUnmarshalObjectThatContainsArray(t *testing.T) {
602+
data := "O:7:\"Struct3\":4:{s:11:\"objectArray\";a:2:{i:0;O:7:\"Struct2\":1:{s:3:\"qux\";d:1.1;}i:1;O:7:\"Struct2\":1:{s:3:\"qux\";d:2.2;}}s:8:\"intArray\";a:2:{i:0;i:1;i:1;i:2;}s:10:\"floatArray\";a:2:{i:0;d:1;i:1;d:2;}s:11:\"stringArray\";a:2:{i:0;s:1:\"a\";i:1;s:1:\"b\";}}"
603+
var result Struct3
604+
err := phpserialize.Unmarshal([]byte(data), &result)
605+
expectErrorToNotHaveOccurred(t, err)
606+
607+
if len(result.ObjectArray) == 0 {
608+
t.Errorf("Expected %v, got %v", 2, len(result.ObjectArray))
609+
}
610+
if len(result.IntArray) == 0 {
611+
t.Errorf("Expected %v, got %v", 2, len(result.IntArray))
612+
}
613+
if len(result.FloatArray) == 0 {
614+
t.Errorf("Expected %v, got %v", 2, len(result.FloatArray))
615+
}
616+
if len(result.StringArray) == 0 {
617+
t.Errorf("Expected %v, got %v", 2, len(result.StringArray))
618+
}
619+
}
620+
601621
// https://github.com/elliotchance/phpserialize/issues/1
602622
func TestUnmarshalMultibyte(t *testing.T) {
603623
data := `a:3:{i:0;a:2:{i:0;s:6:"白色";i:1;s:6:"黑色";}i:1;a:3:{i:0;s:3:"大";i:1;s:3:"中";i:2;s:3:"小";}i:2;a:2:{i:0;s:3:"女";i:1;s:3:"男";}}`

0 commit comments

Comments
 (0)