Dahomey.Cbor
Dahomey.Cbor copied to clipboard
Deserialization of abstract record class fails
Consider this case:
public abstract record class Animal(DateTime Timestamp)
{
}
[CborDiscriminator("dog")]
public record class Dog(string Color, DateTime Timestamp) : Ingress(Timestamp)
{
// This example fails if this empty constructor is not present
private Dog() : this("black", default)
{
}
}
public static class DataIngressCborOptions
{
public static readonly CborOptions Value = new();
static DataIngressCborOptions()
{
Value.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention<string>(Value.Registry));
Value.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(Dog));
}
}
Cbor.Deserialize<Animal[]>(..., DataIngressCborOptions.Value);
In the example, if the Dog ctor is not defined, _constructor is never assigned in ObjectConverter causing the exception A CreatorMapping should be defined for interfaces or abstract classes.