Bug: Key comparer is not preserved in serializing/deserializing immutable collections
dbolin opened this issue · comments
Test code:
var config = new SerializerConfig();
config.UseImmutableFormatters();
var s = new CerasSerializer(config);
var o = ImmutableDictionary<string, int>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase);
var x = s.Serialize(o);
var y = s.Deserialize<ImmutableDictionary<string, int>>(x);
Console.WriteLine(o.KeyComparer.GetType());
Console.WriteLine(y.KeyComparer.GetType());
Actual output:
System.OrdinalIgnoreCaseComparer
System.Collections.Generic.GenericEqualityComparer`1[System.String]
Expected output:
System.OrdinalIgnoreCaseComparer
System.OrdinalIgnoreCaseComparer
Yep, none of the collection formatters persist the equality comparer.
I guess that's something that should be fixed for some of them.
I think the commonly known types like list, hash set, dictionary,... Should have their equality comparer saved and restored.
For all others, it is not possible I think.
We can not assume anything about some custom IList implementation.
For those troublesome cases a custom formatter can be written (which is extremely).
For immutable collections I'll count that as a bug as well. I've added it to my Todo list.
Until then, you can use a custom formatter. (let me know if anything is unclear about that 😄)