C
C#2mo ago
Demonik

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
zodi
zodi2mo ago
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.
Demonik
DemonikOP2mo ago
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
zodi
zodi2mo ago
the problem is I need to read existing files
IReadOnlyList<T> / IReadOnlyCollection<T> are base types of HashSet<T> so a deserialization should work fine. its manually modifying the collection that would change.
Demonik
DemonikOP2mo ago
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 broken
zodi
zodi2mo ago
I 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.
Demonik
DemonikOP2mo ago
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
zodi
zodi2mo ago
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.
Demonik
DemonikOP2mo ago
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

Did you find this page helpful?