Discriminalizer 5.0.0

dotnet add package Discriminalizer --version 5.0.0
NuGet\Install-Package Discriminalizer -Version 5.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Discriminalizer" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Discriminalizer --version 5.0.0
#r "nuget: Discriminalizer, 5.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.0.0 74 5/1/2024
4.0.0 366 3/5/2024
3.0.0 97 3/4/2024
2.0.0 83 2/28/2024
1.1.0 81 2/22/2024
1.0.1 92 2/16/2024
1.0.0 102 2/14/2024