Discriminalizer 5.0.0
dotnet add package Discriminalizer --version 5.0.0
NuGet\Install-Package Discriminalizer -Version 5.0.0
<PackageReference Include="Discriminalizer" Version="5.0.0" />
paket add Discriminalizer --version 5.0.0
#r "nuget: Discriminalizer, 5.0.0"
// Install Discriminalizer as a Cake Addin #addin nuget:?package=Discriminalizer&version=5.0.0 // Install Discriminalizer as a Cake Tool #tool nuget:?package=Discriminalizer&version=5.0.0
Discriminalizer
Deserialize JSON data into objects based on discriminator settings.
Features
Schemabased Type Discrimination
The Schemabased Type Discrimination feature allows JSON deserialization of objects based on specific properties within the JSON data.
JSON Example
[
{ "Prop1": "Class", "Prop2": 1 },
{ "Prop1": "Class", "Prop2": 2 }
]
C# Example
IEnumerable<FirstClass> discriminated = new SchemabasedDiscriminator(JsonSerializerOptions.Default, "Prop1", "Prop2")
.WithType<FirstClass>("Class", "1")
.WithType<SecondClass>("Class", "2")
.Discriminate(json)
.OfType<FirstClass>();
In this example, the JSON data {"Prop1": "Class", "Prop2": 1 }
would be deserialized into an instance of FirstClass
because Prop1
is "Class"
and Prop2
is 1
. If Prop2
was 2
, it would be deserialized into an instance of SecondClass
.
Schemaless Type Discrimination
The Schemaless Discrimination feature allows JSON deserialization of objects without the need for specific properties within the JSON data. This feature is particularly useful when dealing with JSON data where the type of the object to be deserialized is not determined by the values of certain properties in the JSON data.
JSON Example
[
{ "Prop1": "Class" },
{ "Prop1": 50 },
"Hello World"
]
C# Example
IEnumerable<object> discriminated = new SchemalessDiscriminator().Discriminate(jsonNode);
In this example, the given JSON input will be converted to a collection of two dictionaries and one string value, casted as objects.
Composite Type Discrimination
The Composite Type Discrimination feature allows for JSON deserialization of objects using a combination of both Schemabased and Schemaless Discrimination. You can also combine multiple Schemabased discriminators.
JSON Example:
[
{ "Prop1": "Class" },
{ "Prop3": 50 },
{ "Prop6": "Class" }
]
C# Example:
Discriminator discriminator1 = new SchemabasedDiscriminator(JsonSerializerOptions.Default, "Prop1")
.WithType<FirstClass>("Class");
Discriminator discriminator2 = new SchemabasedDiscriminator(JsonSerializerOptions.Default, "Prop3")
.WithType<FirstClass>("50");
Discriminator discriminator3 = new SchemalessDiscriminator();
Discriminator discriminator = new CompositeDiscriminator(discriminator1, discriminator2, discriminator3);
IEnumerable<FirstClass> discriminated = discriminator
.Discriminate(jsonNode)
.OfType<FirstClass>();
In this example, the given JSON input will be discriminated as a collection of two objects with FirstClass
type and
one dictionary.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- FSharp.Core (>= 8.0.200)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.