Skip to content

Commit

Permalink
Adding serialization of objects with arrays (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
golyakov authored Jun 2, 2021
1 parent 5004667 commit 5bb5ecf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ func setField(structFieldValue reflect.Value, value interface{}) error {
m := val.Interface().(map[interface{}]interface{})
fillStruct(structFieldValue, m)

case reflect.Slice:
l := val.Len()
arrayOfObjects := reflect.MakeSlice(structFieldValue.Type(), l, l)

for i := 0; i < l; i++ {
if m, ok := val.Index(i).Interface().(map[interface{}]interface{}); ok {
obj := arrayOfObjects.Index(i)
fillStruct(obj, m)
} else {
switch arrayOfObjects.Index(i).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
arrayOfObjects.Index(i).SetInt(val.Index(i).Elem().Int())
case reflect.Float32, reflect.Float64:
arrayOfObjects.Index(i).SetFloat(val.Index(i).Elem().Float())
default:
arrayOfObjects.Index(i).Set(val.Index(i).Elem())
}

}
}

structFieldValue.Set(arrayOfObjects)

default:
structFieldValue.Set(val)
}
Expand Down
14 changes: 14 additions & 0 deletions serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type Struct2 struct {
Qux float64
}

type Struct3 struct {
ObjectArray []Struct2
IntArray []int64
FloatArray []float64
StringArray []string
}

type marshalTest struct {
input interface{}
output []byte
Expand Down Expand Up @@ -134,6 +141,13 @@ var marshalTests = map[string]marshalTest{
nil,
},

// encode object with array of objects
"struct3{ObjectArray Struct2{Qux float64}, IntArray {1, 2}, FloatArray {1.0, 2.0}, StringArray {'a', 'b'}}": {
Struct3{[]Struct2{{1.1}, {2.2}}, []int64{1, 2}, []float64{1.0, 2.0}, []string{"a", "b"}},
[]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\";}}"),
nil,
},

// encode object (struct with tags)
"structTag{Bar int, Foo Struct2{Qux float64}, hidden bool, Balu string, Nilptr <nil>}": {
structTag{Struct2{1.23}, 10, true, "yay", "", nil},
Expand Down
20 changes: 20 additions & 0 deletions unserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,26 @@ func TestUnmarshalArrayThatContainsInteger(t *testing.T) {
}
}

func TestUnmarshalObjectThatContainsArray(t *testing.T) {
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\";}}"
var result Struct3
err := phpserialize.Unmarshal([]byte(data), &result)
expectErrorToNotHaveOccurred(t, err)

if len(result.ObjectArray) == 0 {
t.Errorf("Expected %v, got %v", 2, len(result.ObjectArray))
}
if len(result.IntArray) == 0 {
t.Errorf("Expected %v, got %v", 2, len(result.IntArray))
}
if len(result.FloatArray) == 0 {
t.Errorf("Expected %v, got %v", 2, len(result.FloatArray))
}
if len(result.StringArray) == 0 {
t.Errorf("Expected %v, got %v", 2, len(result.StringArray))
}
}

// https://github.com/elliotchance/phpserialize/issues/1
func TestUnmarshalMultibyte(t *testing.T) {
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:"男";}}`
Expand Down

0 comments on commit 5bb5ecf

Please sign in to comment.