Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Discriminator Serialization policy (auto/always/never)
- Loading branch information
Michaël Catanzariti
committed
Sep 12, 2019
1 parent
898dd4a
commit cd99186
Showing
10 changed files
with
143 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Dahomey.Cbor.Attributes; | ||
using Xunit; | ||
|
||
namespace Dahomey.Cbor.Tests | ||
{ | ||
public class DiscriminatorTests | ||
{ | ||
[Fact] | ||
public void ReadPolymorphicObject() | ||
{ | ||
const string hexBuffer = "A16A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F62496401"; | ||
BaseObjectHolder obj = Helper.Read<BaseObjectHolder>(hexBuffer); | ||
} | ||
|
||
[Theory] | ||
[InlineData(CborDiscriminatorPolicy.Default, "A26A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F624964016A4E616D654F626A656374A2644E616D656362617262496402")] | ||
[InlineData(CborDiscriminatorPolicy.Auto, "A26A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F624964016A4E616D654F626A656374A2644E616D656362617262496402")] | ||
[InlineData(CborDiscriminatorPolicy.Never, "A26A426173654F626A656374A2644E616D6563666F6F624964016A4E616D654F626A656374A2644E616D656362617262496402")] | ||
[InlineData(CborDiscriminatorPolicy.Always, "A26A426173654F626A656374A3625F746A4E616D654F626A656374644E616D6563666F6F624964016A4E616D654F626A656374A3625F746A4E616D654F626A656374644E616D656362617262496402")] | ||
public void WritePolymorphicObject(CborDiscriminatorPolicy discriminatorPolicy, string hexBuffer) | ||
{ | ||
CborOptions options = new CborOptions(); | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Dahomey.Cbor.Attributes; | ||
using Xunit; | ||
|
||
namespace Dahomey.Cbor.Tests.Issues | ||
{ | ||
public class Issue0027 | ||
{ | ||
[CborDiscriminator("somediscriminator", Policy = CborDiscriminatorPolicy.Always)] | ||
public class Car | ||
{ | ||
public string Description { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void Test() | ||
{ | ||
var car = new Car() | ||
{ | ||
Description = "n" | ||
}; | ||
|
||
string hexBuffer = "A2625F7471736F6D656469736372696D696E61746F726B4465736372697074696F6E616E"; | ||
Helper.TestWrite(car, hexBuffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters