-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix user defined struct array serialization failure #235
Fix user defined struct array serialization failure #235
Conversation
} | ||
|
||
[Fact] | ||
public void CanSerializeStructArray() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the new spec for the failing serialization.
@@ -30,12 +32,13 @@ private static void WriteValues<T>(T[] array, Stream stream, Type elementType, V | |||
stream.WriteObject(value, elementType, elementSerializer, preserveObjectReferences, session); | |||
} | |||
} | |||
private static void ReadValues<T>(int length, Stream stream, DeserializerSession session, T[] array) | |||
|
|||
private static void ReadValues(int length, Stream stream, DeserializerSession session, Array array) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to change generic type to untyped Array array. Generic typed array does not play nice with user defined structs.
@@ -68,7 +71,8 @@ private static void ReadValues<T>(int length, Stream stream, DeserializerSession | |||
session.TrackSerializedObject(arr); | |||
} | |||
|
|||
WriteValues((dynamic)arr, stream, elementType, elementSerializer, session); | |||
// This janky way of converting array to Array is done to get around the problem of ValueType arrays | |||
WriteValues(((IEnumerable)arr).Cast<object>().ToArray(), stream, elementType, elementSerializer, session); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only way to convert an array of user defined struct object into an Array
is to cast it as an IEnumerable
and then cast all of its elements individually into objects. This is done to convert an array of values into an array of reference objects so that it conforms to the generated IL code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is less janky that casting to a dynamic
type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -68,7 +71,8 @@ private static void ReadValues<T>(int length, Stream stream, DeserializerSession | |||
session.TrackSerializedObject(arr); | |||
} | |||
|
|||
WriteValues((dynamic)arr, stream, elementType, elementSerializer, session); | |||
// This janky way of converting array to Array is done to get around the problem of ValueType arrays | |||
WriteValues(((IEnumerable)arr).Cast<object>().ToArray(), stream, elementType, elementSerializer, session); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is less janky that casting to a dynamic
type
@@ -616,5 +616,87 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type | |||
return os; | |||
} | |||
} | |||
} | |||
|
|||
[Fact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New tests look good
Closes #232
Was moved from #233
Clean up PR, remove all unnescessary codes.