![]() |
VOOZH | about |
dotnet add package JsonKnownTypes --version 0.7.0
NuGet\Install-Package JsonKnownTypes -Version 0.7.0
<PackageReference Include="JsonKnownTypes" Version="0.7.0" />
<PackageVersion Include="JsonKnownTypes" Version="0.7.0" />Directory.Packages.props
<PackageReference Include="JsonKnownTypes" />Project file
paket add JsonKnownTypes --version 0.7.0
#r "nuget: JsonKnownTypes, 0.7.0"
#:package JsonKnownTypes@0.7.0
#addin nuget:?package=JsonKnownTypes&version=0.7.0Install as a Cake Addin
#tool nuget:?package=JsonKnownTypes&version=0.7.0Install as a Cake Tool
Helps to serialize and deserialize polymorphic types. Adds discriminator to json.
Check this issue https://github.com/dmitry-bym/JsonKnownTypes/issues/35
To add discriminator to swagger(OpenAPI) scheme use this package JsonKnownTypes.Swashbuckle
The simplest way to use it is to add one attribute to base class or interface:
[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
public class BaseClass
{
public string Summary { get; set; }
}
public class ChildClass : BaseClass
{
public string Detailed { get; set; }
}
Serialization and Deserialization:
var entityJson = JsonConvert.SerializeObject(entity);
var obj = DeserializeObject<BaseClass>(entityJson)
Json representation:
{ "Summary":"someValue", "$type":"BaseClass" }
{ "Summary":"someValue", "Detailed":"someValue", "$type":"ChildClass" }
Also, you can use it with interfaces or abstract classes:
[JsonConverter(typeof(JsonKnownTypesConverter<IInterface>))]
public interface IInterface { ... }
public class ChildClass : IInterface { ... }
Json representation:
{ ... "$type":"ChildClass" }
[JsonConverter(typeof(JsonKnownTypesConverter<AbstractClass>))]
public abstract class AbstractClass { ... }
public class ChildClass : AbstractClass { ... }
Json representation:
{ ... "$type":"ChildClass" }
If you need to add a custom discriminator use JsonKnownType attribute.
By default, "$type" is used for discriminator property name, if you need to change that use JsonDiscriminator attribute.
[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
[JsonDiscriminator(Name = "myType")] //add custom discriminator name
[JsonKnownType(typeof(BaseClass1Heir))] //could be deleted if you didn't turn off UseClassNameAsDiscriminator
[JsonKnownType(typeof(BaseClass2Heir), "myDiscriminator")]
public class BaseClass { ... }
public class BaseClass1Heir : BaseClass { ... }
public class BaseClass2Heir : BaseClass { ... }
Json representation:
{ ... , "myType":"BaseClass" }
{ ... , "myType":"BaseClass1Heir" }
{ ... , "myType":"myDiscriminator" }
Add a discriminator for type which is used with it:
[JsonConverter(typeof(JsonKnownTypesConverter<BaseClass>))]
[JsonKnownThisType("do_you_know_that")]
public class BaseClass { ... }
[JsonKnownThisType("html_is_programming_language")]
public class BaseClass1Heir : BaseClass { ... }
[JsonKnownThisType("just_joke=)")]
public class BaseClass2Heir : BaseClass { ... }
Json representation:
{ ... , "$type":"do_you_know_that" }
{ ... , "$type":"html_is_programming_language" }
{ ... , "$type":"just_joke=)" }
To change default discriminator settings use:
JsonKnownTypesSettingsManager.DefaultDiscriminatorSettings = new JsonDiscriminatorSettings
{
DiscriminatorFieldName = "name",
UseClassNameAsDiscriminator = false
};
DiscriminatorFieldNamechange default"$type"name to yours
If
UseClassNameAsDiscriminatorisfalseyou should addJsonKnownTypeorJsonKnownThisTypeattribute for each relative class manually otherwise it will throw an exception.
If you want to add derived types from another assembly you can set custom type resolver Func<Type, Type[]>:
JsonKnownTypesSettingsManager.GetDerivedByBase = parent => parent.Assembly.GetTypes();
public class BaseClass { ... }
public class BaseAbstractClass1Heir : BaseClass { ... }
public class BaseAbstractClass2Heir : BaseClass { ... }
var converter = new JsonKnownTypesConverter<BaseClass>()
var entityJson = JsonConvert.SerializeObject(entity, converter);
var obj = DeserializeObject<BaseClass>(entityJson, converter)
You have to pass a converter directly to method if you do not use
JsonConverterattribute.
Normally you will receive an exception during deserialization of models marked with unknown or
unspecified type discriminator. If you need an exception-free way you can use JsonKnownTypeFallback attribute.
In that case entities marked with unknown or missing type discriminator will be deserialized to specified fallback type.
See example.
Assume you have a bunch of events coming from webhook/frontend/etc.:
[
{ id: "abc", opType: "op_start" },
{ id: "bcd", opType: "on_save" },
{ id: "cde", opType: "op_update" },
{ id: "def", opType: "op_end" }
]
Then in your application you can do the following to handle only events you are only interested of:
[JsonConverter(typeof(JsonKnownTypesConverter<OperationBase>))]
[JsonDiscriminator(Name = "opType")]
[JsonKnownType(typeof(OperationStarted), "op_start")]
[JsonKnownType(typeof(OperationEnded), "op_end")]
[JsonKnownTypeFallback(typeof(UnsupportedOperation))]
public abstract class OperationBase
{
public string Id { get; set; }
}
public class OperationStarted : OperationBase { }
public class OperationEnded : OperationBase { }
public class UnsupportedOperation : OperationBase { }
Documented API can be changed, because it is zero version still. And I'm trying to find better solution and always add some fixes and this fixes also can change some behavior.
There are a lot of undocumented public API and you can use it but i can change it any time.
So when you install new version check all of these.
I need feedback, if you have suggestion or some issue create an issue and describe it, even if you think it's not critical.
Also you can buy me a coffee 🤗
<a href="https://www.buymeacoffee.com/dmitry.bym" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
Authored by: Dmitry Kaznacheev (dmitry-bym)
This project is under MIT license. You can obtain the license copy here.
This work using work of James Newton-King, author of Json.NET. https://www.newtonsoft.com/json
| 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 | 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 | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. 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 | tizen40 tizen40 was computed. tizen60 tizen60 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 4 NuGet packages that depend on JsonKnownTypes:
| Package | Downloads |
|---|---|
|
JsonKnownTypes.Swashbuckle
Package Description |
|
|
Rownd.Xamarin
Integrate simple, frictionless authentication into your Xamarin app. |
|
|
WebConsoleConnector
Package Description |
|
|
Rownd.Maui
Integrate simple, frictionless authentication into your .NET MAUI app. |
Showing the top 1 popular GitHub repositories that depend on JsonKnownTypes:
| Repository | Stars |
|---|---|
|
1Remote/1Remote
One Remote Access Manager to Rule Them All
|
| Version | Downloads | Last Updated |
|---|---|---|
| 0.7.0 | 292,903 | 3/4/2025 |
| 0.6.0 | 352,919 | 5/4/2024 |
| 0.5.5 | 768,064 | 4/12/2022 |
| 0.5.4 | 532,420 | 4/3/2021 |
| 0.5.3 | 23,385 | 3/8/2021 |
| 0.5.2 | 30,893 | 2/15/2021 |
| 0.4.2 | 37,277 | 11/28/2020 |
| 0.4.1 | 96,333 | 5/13/2020 |
| 0.4.0 | 3,392 | 5/1/2020 |
| 0.3.0 | 6,691 | 3/22/2020 |
| 0.2.0 | 3,042 | 2/22/2020 |
| 0.1.0 | 3,037 | 2/19/2020 |