EnumExtensions.System.Text.Json
1.2.1
dotnet add package EnumExtensions.System.Text.Json --version 1.2.1
NuGet\Install-Package EnumExtensions.System.Text.Json -Version 1.2.1
<PackageReference Include="EnumExtensions.System.Text.Json" Version="1.2.1" />
<PackageVersion Include="EnumExtensions.System.Text.Json" Version="1.2.1" />
<PackageReference Include="EnumExtensions.System.Text.Json" />
paket add EnumExtensions.System.Text.Json --version 1.2.1
#r "nuget: EnumExtensions.System.Text.Json, 1.2.1"
#addin nuget:?package=EnumExtensions.System.Text.Json&version=1.2.1
#tool nuget:?package=EnumExtensions.System.Text.Json&version=1.2.1
System.Text.Json.Extensions
Some extensions to the JsonStringEnumConverter which supports attributes like EnumMember, Display and Description
Option 1: Usage Example - EnumMember
Define Enum and add attributes
Define an Enum and annotate the Enum fields with the EnumMemberAttribute:
enum WeatherType
{
[EnumMember(Value = "Zonnig")]
Sunny,
[EnumMember(Value = "Helder")]
Clear
}
Add Converter
Add the new JsonStringEnumConverterWithAttributeSupport
to the Converters via the JsonSerializerOptions:
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverterWithAttributeSupport());
Serialize an object
var weatherForecast = new WeatherForecast
{
WeatherType = WeatherType.Sunny
};
var weatherForecastSerialized = JsonSerializer.Serialize(weatherForecast, options);
Console.WriteLine(weatherForecastSerialized); // {"WeatherType":"Zonnig"}
Deserialize an object
Deserialize works by using the same options:
var json = "{\"WeatherType\":\"Zonnig\"}";
var weatherForecastDeserialized = JsonSerializer.Deserialize<WeatherForecast>(json, options);
Option 2: Usage Example - EnumMember
It's also possible to annotate the Enum with a [JsonConverter]
so that you don't need to manually registerd the JsonStringEnumConverterWithAttributeSupport
to the Converters via the JsonSerializerOptions.
Define Enum and add attributes
Define an Enum
- add the
[JsonConverter(typeof(JsonStringEnumConverterWithAttributeSupport))]
to the Enum - annotate the Enum fields with the EnumMemberAttribute:
[JsonConverter(typeof(JsonStringEnumConverterWithAttributeSupport))]
enum WeatherType
{
[EnumMember(Value = "Zonnig")]
Sunny,
[EnumMember(Value = "Helder")]
Clear
}
Serializing and Deserialize an object
This works the same as using Option 1.
Note that only Enum values which are annotated with EnumMember
are supported.
Usage Example - Display and Description
It's also possible to annotate Enum fields with these attributes:
Define Enum and add attributes
enum WeatherType
{
[EnumMember(Value = "Zonnig")]
Sunny,
[Display(Name = "Helder")]
Clear,
[Description("Bewolkt")]
Cloudy
}
Add Converter
! By default, the Display and Description are disabled, use the following line to enable these.
var options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverterWithAttributeSupport(null, true, true, true, true));
Serializing and Deserializing works the same.
Sponsors
Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of System.Text.Json.Extensions.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 4.7.2)
-
.NETStandard 2.1
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 8.0.5)
-
net8.0
- No dependencies.
NuGet packages (9)
Showing the top 5 NuGet packages that depend on EnumExtensions.System.Text.Json:
Package | Downloads |
---|---|
MercadoLibreSdk
Fork of the Mercado Libre .NET SDK to generate access token and query the Mercado Libre API. |
|
DiscosWebSdk
A C# SDK for the European Space Agency's DISCOSweb API |
|
KpiSchedule.Common
This library contains a set of common tools to pull and parse academic schedules for Kyiv Polytechnic Institute. It supports group and teacher schedules with two main data sources: calling schedules.kpi.ua API and parsing schedule pages from roz.kpi.ua. See project page with more details and examples: https://github.com/noobquire/KpiSchedule.Common |
|
Thinktecture.Blazor.AsyncClipboard
Async Clipboard API wrapper implementation for Blazor. |
|
DeepSecure.ThreatRemoval
A .Net Core SDK for working with Deep Secure's Threat Removal API |
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on EnumExtensions.System.Text.Json:
Repository | Stars |
---|---|
RupertAvery/DiffusionToolkit
Metadata-indexer and Viewer for AI-generated images
|
|
nager/Nager.Country
Worldwide Country Informations (ISO-3166-1 Alpha2, ISO-3166-1 Alpha3, ISO 639-1)
|
See CHANGELOG.md