System.Text.Json serialization support for OneOf types.
$ dotnet add package OneOf.Serialization.SystemTextJsonThis library provides support for JSON serialization and deserialization of OneOf types using System.Text.Json.
Add a reference to OneOf.Serialization.SystemTextJson in your .csproj:
<ItemGroup>
<PackageReference Include="OneOf.Serialization.SystemTextJson" Version="1.1.1"/>
</ItemGroup>
This provides access to two JsonConverter classes. Create a JsonSerializerOptions with both converters:
var serializerOptions = new JsonSerializerOptions
{
Converters =
{
new OneOfJsonConverter(),
new OneOfBaseJsonConverter(),
},
};
(You can omit OneOfBaseJsonConverter if you are not serializing types derived from OneOfBase but not the other way around.)
Then serialize and deserialize using the serializerOptions:
var json = JsonSerializer.Serialize(oneOfToSerialize, serializerOptions);
var deserializeOneOf = JsonSerializer.Deserialize<OneOf<Foo, Bar, Baz>>(json, serializerOptions);
In the case of an OneOf with types Foo, Bar and Baz that all serialize to JSON objects the generated JSON will be one of the following:
When Foo is the value:
{
"Foo": {
// ...
}
}
When Bar is the value:
{
"Bar": {
// ...
}
}
And when Baz is the value:
{
"Baz": {
// ...
}
}
See project repository for more information.