![]() |
VOOZH | about |
dotnet add package JsonSubTypes --version 2.0.1
NuGet\Install-Package JsonSubTypes -Version 2.0.1
<PackageReference Include="JsonSubTypes" Version="2.0.1" />
<PackageVersion Include="JsonSubTypes" Version="2.0.1" />Directory.Packages.props
<PackageReference Include="JsonSubTypes" />Project file
paket add JsonSubTypes --version 2.0.1
#r "nuget: JsonSubTypes, 2.0.1"
#:package JsonSubTypes@2.0.1
#addin nuget:?package=JsonSubTypes&version=2.0.1Install as a Cake Addin
#tool nuget:?package=JsonSubTypes&version=2.0.1Install as a Cake Tool
JsonSubTypes is a discriminated Json sub-type Converter implementation for .NET
๐ Build status
๐ Code Coverage
๐ Quality Gate Status
๐ NuGet
๐ NuGet
๐ CodeFactor
๐ FOSSA Status
[JsonConverter(typeof(JsonSubtypes), "Kind")]
public interface IAnimal
{
string Kind { get; }
}
public class Dog : IAnimal
{
public string Kind { get; } = "Dog";
public string Breed { get; set; }
}
public class Cat : IAnimal {
public string Kind { get; } = "Cat";
public bool Declawed { get; set;}
}
The second parameter of the JsonConverter attribute is the JSON property name that will be use to retreive the type information from JSON.
var animal = JsonConvert.DeserializeObject<IAnimal>("{\"Kind\":\"Dog\",\"Breed\":\"Jack Russell Terrier\"}");
Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed);
N.B.: This only works for types in the same assembly as the base type/interface and either in the same namespace or with a fully qualified type name.
[JsonConverter(typeof(JsonSubtypes), "Sound")]
[JsonSubtypes.KnownSubType(typeof(Dog), "Bark")]
[JsonSubtypes.KnownSubType(typeof(Cat), "Meow")]
public class Animal
{
public virtual string Sound { get; }
public string Color { get; set; }
}
public class Dog : Animal
{
public override string Sound { get; } = "Bark";
public string Breed { get; set; }
}
public class Cat : Animal
{
public override string Sound { get; } = "Meow";
public bool Declawed { get; set; }
}
var animal = JsonConvert.DeserializeObject<IAnimal>("{\"Sound\":\"Bark\",\"Breed\":\"Jack Russell Terrier\"}");
Assert.AreEqual("Jack Russell Terrier", (animal as Dog)?.Breed);
N.B.: Also works with other kind of value than string, i.e.: enums, int, ...
This mode of operation only works when JsonSubTypes is explicitely registered in JSON.NET's serializer settings, and not through the [JsonConverter] attribute.
public abstract class Animal
{
public int Age { get; set; }
}
public class Dog : Animal
{
public bool CanBark { get; set; } = true;
}
public class Cat : Animal
{
public int Lives { get; set; } = 7;
}
public enum AnimalType
{
Dog = 1,
Cat = 2
}
var settings = new JsonSerializerSettings();
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(Animal), "Type") // type property is only defined here
.RegisterSubtype(typeof(Cat), AnimalType.Cat)
.RegisterSubtype(typeof(Dog), AnimalType.Dog)
.SerializeDiscriminatorProperty() // ask to serialize the type property
.Build());
or using syntax with generics:
var settings = new JsonSerializerSettings();
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of<Animal>("Type") // type property is only defined here
.RegisterSubtype<Cat>(AnimalType.Cat)
.RegisterSubtype<Dog>(AnimalType.Dog)
.SerializeDiscriminatorProperty() // ask to serialize the type property
.Build());
var cat = new Cat { Age = 11, Lives = 6 }
var json = JsonConvert.SerializeObject(cat, settings);
Assert.Equal("{\"Lives\":6,\"Age\":11,\"Type\":2}", json);
var result = JsonConvert.DeserializeObject<Animal>(json, settings);
Assert.Equal(typeof(Cat), result.GetType());
Assert.Equal(11, result.Age);
Assert.Equal(6, (result as Cat)?.Lives);
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Employee), "JobTitle")]
[JsonSubtypes.KnownSubTypeWithProperty(typeof(Artist), "Skill")]
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class Artist : Person
{
public string Skill { get; set; }
}
or using syntax with generics:
string json = "[{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}," +
"{\"Department\":\"Department1\",\"JobTitle\":\"JobTitle1\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}," +
"{\"Skill\":\"Painter\",\"FirstName\":\"FirstName1\",\"LastName\":\"LastName1\"}]";
var persons = JsonConvert.DeserializeObject<IReadOnlyCollection<Person>>(json);
Assert.AreEqual("Painter", (persons.Last() as Artist)?.Skill);
settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder
.Of(typeof(Person))
.RegisterSubtypeWithProperty(typeof(Employee), "JobTitle")
.RegisterSubtypeWithProperty(typeof(Artist), "Skill")
.Build());
or
settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder
.Of<Person>()
.RegisterSubtypeWithProperty<Employee>("JobTitle")
.RegisterSubtypeWithProperty<Artist>("Skill")
.Build());
[JsonConverter(typeof(JsonSubtypes))]
[JsonSubtypes.KnownSubType(typeof(ConstantExpression), "Constant")]
[JsonSubtypes.FallBackSubType(typeof(UnknownExpression))]
public interface IExpression
{
string Type { get; }
}
Or with code configuration:
settings.Converters.Add(JsonSubtypesConverterBuilder
.Of(typeof(IExpression), "Type")
.SetFallbackSubtype(typeof(UnknownExpression))
.RegisterSubtype(typeof(ConstantExpression), "Constant")
.Build());
settings.Converters.Add(JsonSubtypesWithPropertyConverterBuilder
.Of(typeof(IExpression))
.SetFallbackSubtype(typeof(UnknownExpression))
.RegisterSubtype(typeof(ConstantExpression), "Value")
.Build());
If this project helped you save money or time or simply makes your life also easier, you can give me a cup of coffee =)
33gxVjey6g4Beha26fSQZLFfWWndT1oY3F| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp1.0 netcoreapp1.0 was computed. netcoreapp1.1 netcoreapp1.1 was computed. netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard1.3 netstandard1.3 is compatible. netstandard1.4 netstandard1.4 was computed. netstandard1.5 netstandard1.5 was computed. netstandard1.6 netstandard1.6 was computed. netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net35 net35 is compatible. net40 net40 is compatible. net403 net403 was computed. net45 net45 is compatible. net451 net451 was computed. net452 net452 was computed. net46 net46 is compatible. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 is compatible. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen30 tizen30 was computed. tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Universal Windows Platform | uap uap was computed. uap10.0 uap10.0 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on JsonSubTypes:
| Package | Downloads |
|---|---|
|
Okta.Sdk
Official .NET SDK for the Okta API |
|
|
InfluxDB.Client
The reference client that allows query, write and management (bucket, organization, users) for the InfluxDB 2.x. |
|
|
Xero.NetStandard.OAuth2
This is a .NETStandard SDK library, used to communicate with the Xero API using OAuth2.0. See https://github.com/XeroAPI/Xero-NetStandard for more information |
|
|
Finbourne.EdpLusidSdk
LUSID SDK for EDP |
|
|
sib_api_v3_sdk
Official SendinBlue provided RESTFul API V3 C# Library |
Showing the top 20 popular GitHub repositories that depend on JsonSubTypes:
| Repository | Stars |
|---|---|
|
BililiveRecorder/BililiveRecorder
ๅฝๆญๅงฌ | mikufans ็ๆพ้ๅฝๅถ
|
|
|
Azure-Samples/cognitive-services-speech-sdk
Sample code for the Microsoft Cognitive Services Speech SDK
|
|
|
antonpup/Aurora
Unified lighting effects across multiple brands and various games.
|
|
|
jlucansky/Quartzmin
Quartzmin is powerful, easy to use web management tool for Quartz.NET
|
|
|
api-bricks/api-bricks-sdk
SDKs for CoinAPI & FinFeedAPI
|
|
|
takuya-takeuchi/DlibDotNet
Dlib .NET wrapper written in C++ and C# for Windows, MacOS, Linux and iOS
|
|
|
IoTSharp/SilkierQuartz
SilkierQuartz can host jobs using HostService and Provide a web management tools for Quartz !
|
|
|
blish-hud/Blish-HUD
A Guild Wars 2 overlay with extreme extensibility through compiled modules.
|
|
|
influxdata/influxdb-client-csharp
InfluxDB 2.x C# Client
|
|
|
microsoft/verisol
A formal verifier and analysis tool for Solidity Smart Contracts
|
|
|
alipay/alipay-sdk-net-all
ๆฏไปๅฎๅผๆพๅนณๅฐ Alipay SDK for .NET
|
|
|
notion-dotnet/notion-sdk-net
A Notion SDK for .Net
|
|
|
Anapher/Strive
Open source video conferencing platform
|
|
| blockchain/lib-exchange-client | |
|
christianhelle/apiclientcodegen
A collection of Visual Studio code generators for Swagger / OpenAPI specification files
|
|
|
LexPredict/lexpredict-contraxsuite
LexPredict ContraxSuite
|
|
|
okta/okta-sdk-dotnet
A .NET SDK for interacting with the Okta management API, enabling server-side code to manage Okta users, groups, applications, and more.
|
|
|
bybit-exchange/api-connectors
Libraries for connecting to the Bybit API.
|
|
|
James231/Start-Menu-Manager
App to add websites/software/files/folders/scripts to the Windows 10 Start Menu and Taskbar, and priority shortcuts to Windows 10 Search.
|
|
|
watson-developer-cloud/dotnet-standard-sdk
:new::new::new:.NET Standard library to access Watson Services.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.1 | 53,577,580 | 10/18/2022 |
| 2.0.0 | 32,212 | 10/18/2022 |
| 1.9.0 | 11,092,785 | 5/9/2022 |
| 1.8.0 | 30,819,795 | 9/23/2020 |
| 1.7.0 | 4,261,725 | 3/28/2020 |
| 1.6.0 | 5,588,214 | 6/24/2019 |
| 1.5.2 | 9,667,171 | 1/19/2019 |
| 1.5.1 | 761,677 | 10/15/2018 |
| 1.5.0 | 107,241 | 8/27/2018 |
| 1.4.0 | 283,396 | 4/17/2018 |
| 1.3.1 | 11,813 | 4/12/2018 |
| 1.3.0 | 91,712 | 1/28/2018 |
| 1.2.0 | 13,624,980 | 11/22/2017 |
| 1.1.3 | 74,923 | 11/15/2017 |
| 1.1.2 | 8,445 | 10/20/2017 |
| 1.1.1 | 7,846 | 9/21/2017 |
| 1.1.0 | 12,857 | 9/19/2017 |
| 1.0.0 | 31,366 | 7/23/2017 |