Skip to content
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

Merged
merged 1 commit into from
Jul 8, 2021

Conversation

Arkatufus
Copy link
Contributor

Closes #232
Was moved from #233
Clean up PR, remove all unnescessary codes.

}

[Fact]
public void CanSerializeStructArray()
Copy link
Contributor Author

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)
Copy link
Contributor Author

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);
Copy link
Contributor Author

@Arkatufus Arkatufus Jul 7, 2021

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.

Copy link
Member

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

Copy link
Member

@Aaronontheweb Aaronontheweb left a 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);
Copy link
Member

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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests look good

@Aaronontheweb Aaronontheweb merged commit 97baf14 into akkadotnet:dev Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hyperion fails to serialize arrays of user defined structs
2 participants