public void WritePolymorphicObject(CborDiscriminatorPolicy discriminatorPolicy, string hexBuffer) { CborOptions options = new CborOptions(); options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry)); options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject)); options.Registry.ObjectMappingRegistry.Register <NameObject>(om => { om.AutoMap(); om.SetDiscriminatorPolicy(discriminatorPolicy); }); options.Registry.ObjectMappingRegistry.Register <BaseObject>(om => { om.AutoMap(); om.SetDiscriminatorPolicy(discriminatorPolicy); }); BaseObjectHolder obj = new BaseObjectHolder { BaseObject = new NameObject { Id = 1, Name = "foo" }, NameObject = new NameObject { Id = 2, Name = "bar" } }; Helper.TestWrite(obj, hexBuffer, null, options); }
public void ReadPolymorphicObject() { CborOptions options = new CborOptions(); options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention <string>(options.Registry)); options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject)); const string hexBuffer = "A16A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F62496401"; BaseObjectHolder obj = Helper.Read <BaseObjectHolder>(hexBuffer, options); Assert.NotNull(obj); Assert.IsType <NameObject>(obj.BaseObject); Assert.Equal("foo", ((NameObject)obj.BaseObject).Name); Assert.Equal(1, obj.BaseObject.Id); }
public void ReadWithCustomDiscriminator() { CborOptions options = new CborOptions(); options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new CustomDiscriminatorConvention()); options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject)); const string hexBuffer = "A16A426173654F626A656374A364747970651A22134C83644E616D6563666F6F62496401"; BaseObjectHolder obj = Helper.Read <BaseObjectHolder>(hexBuffer, options); Assert.NotNull(obj); Assert.IsType <NameObject>(obj.BaseObject); Assert.Equal("foo", ((NameObject)obj.BaseObject).Name); Assert.Equal(1, obj.BaseObject.Id); }
public void WriteWithCustomDiscriminator() { CborOptions options = new CborOptions(); options.Registry.DiscriminatorConventionRegistry.RegisterConvention(new CustomDiscriminatorConvention()); options.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(NameObject)); const string hexBuffer = "A26A426173654F626A656374A364747970651A22134C83644E616D6563666F6F624964016A4E616D654F626A656374F6"; BaseObjectHolder obj = new BaseObjectHolder { BaseObject = new NameObject { Id = 1, Name = "foo" }, }; Helper.TestWrite(obj, hexBuffer, null, options); }