ISerializationSurrogate and collections issue
I'm trying to port old code that was using BinaryFormatter and I'm using ISerializationSurrogate to be able to deserialize old files and it works except for one thing - all collections (Dictionary, HashSet) are returned empty from SerializationEntry.Value. I know they are not empty because in old project when I deserialize directly into type it works fine.
8 Replies
maybe the deserializer does not support HashSet collection. maybe try changing the hashset in the model(entity) to an IReadOnlyList<T> if you need the index or IReadOnlyCollection<T> if you dont. they are more widely supported for deserializers. Check if this one change allows the collection to be deserialized. if not then change it back to a hashset cause this might not be the issue.
the problem is I need to read existing files
that use hashset<string>
BinarySerializer can read it without any issues if I deserialize in old unity, but when I deserialize with ISerializationSurrogate it returns empty collection (not null)
I'm going to try without surrogate and just with serialization binder instead
at least I'll figure out if the problem is with surrogate or with BinarySerializer in new unity version
the problem is I need to read existing filesIReadOnlyList<T> / IReadOnlyCollection<T> are base types of HashSet<T> so a deserialization should work fine. its manually modifying the collection that would change.
well it deserializes into hashset<T>
SerializationInfo contains the field and the type is
HashSet<string>
like it's supposed to be but the collection itself is empty
dictionary is also brokenI had a similar issue using LiteDB with an entity that had some types of dictionary and immutable hash set. they returned empty with LiteDB so i had to change the collection types.
does it use BinarySerializer?
that thing is deprecated/removed in modern .net but the code wasn't written by me and I've been trying to move to memorypack but I need to be able to convert old files
no. it was using the LiteDB serializer which is old and does not support newer C# types.
so it causes errors when trying to deserialize some types from the db.
with binaryserializer I don't think you can change anything, it serializes information about types and assemblies along with the data
I couldn't even rename the field where I had typo in the name
so without
ISerializationSurrogate
it works
I guess I'll have to copy serializable objects from old project and bind them directly instead of surrogates