system_text_json_enumextensions - EnumExtensions.System.Text.Json 1.0.0-ci-12026
Extensions to the JsonStringEnumConverter which supports attributes like EnumMember, Display and Description.
PM> Install-Package EnumExtensions.System.Text.Json -Version 1.0.0-ci-12026 -Source https://www.myget.org/F/system_text_json_enumextensions/api/v3/index.json
> nuget.exe install EnumExtensions.System.Text.Json -Version 1.0.0-ci-12026 -Source https://www.myget.org/F/system_text_json_enumextensions/api/v3/index.json
> dotnet add package EnumExtensions.System.Text.Json --version 1.0.0-ci-12026 --source https://www.myget.org/F/system_text_json_enumextensions/api/v3/index.json
<PackageReference Include="EnumExtensions.System.Text.Json" Version="1.0.0-ci-12026" />
Copy to clipboard
source https://www.myget.org/F/system_text_json_enumextensions/api/v3/index.json
nuget EnumExtensions.System.Text.Json ~> 1.0.0-ci-12026
Copy to clipboard
> choco install EnumExtensions.System.Text.Json --version 1.0.0-ci-12026 --source https://www.myget.org/F/system_text_json_enumextensions/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "system_text_json_enumextensions" -SourceLocation "https://www.myget.org/F/system_text_json_enumextensions/api/v2"
Install-Module -Name "EnumExtensions.System.Text.Json" -RequiredVersion "1.0.0-ci-12026" -Repository "system_text_json_enumextensions" -AllowPreRelease
Copy to clipboard
System.Text.Json.Extensions
Some extensions to the JsonStringEnumConverter which supports attributes like EnumMember, Display and Description
Info
Build Azure | |
NuGet | |
MyGet (preview) |
Installing
You can install from NuGet using the following command in the package manager window:
Install-Package EnumExtensions.System.Text.Json
Or via the Visual Studio NuGet package manager.
If you use the dotnet
command:
dotnet add package EnumExtensions.System.Text.Json
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.
See CHANGELOG.md
- .NETStandard 2.0: 2.0.0.0
OwnersStef |
AuthorsStef Heyenrath |
Project URLhttps://github.com/StefH/System.Text.Json.EnumExtensions |
LicenseUnknown |
TagsSystem Text Json Enum EnumMember EnumMemberAttribute Attribute DisplayAttribute DescriptionAttribute |
Info0 total downloads |
0 downloads for version 1.0.0-ci-12026 |
Download (6.74 KB) |
Found on the current feed only |
Package history
Version | Size | Last updated | Downloads | Mirrored? | |||
---|---|---|---|---|---|---|---|
1.1.0-ci-16545 | 16.1 KB | Sun, 16 Oct 2022 07:49:06 GMT | 0 | ||||
1.1.0-ci-15739 | 16.1 KB | Mon, 03 Jan 2022 11:32:38 GMT | 0 | ||||
1.0.1-ci-14819 | 16.07 KB | Sat, 20 Mar 2021 08:50:37 GMT | 0 | ||||
1.0.1-ci-14818 | 16.08 KB | Sat, 20 Mar 2021 08:46:31 GMT | 0 | ||||
1.0.1-ci-14816 | 16.08 KB | Sat, 20 Mar 2021 08:44:27 GMT | 0 | ||||
1.0.0-preview-01-ci-12025 | 6.82 KB | Sun, 13 Oct 2019 09:26:24 GMT | 0 | ||||
1.0.0-ci-14790 | 6.8 KB | Fri, 12 Mar 2021 16:19:01 GMT | 0 | ||||
1.0.0-ci-13722 | 6.74 KB | Sun, 30 Aug 2020 10:21:36 GMT | 0 | ||||
1.0.0-ci-12163 | 6.74 KB | Fri, 08 Nov 2019 06:39:00 GMT | 0 | ||||
1.0.0-ci-12162 | 6.74 KB | Fri, 08 Nov 2019 06:38:27 GMT | 0 | ||||
1.0.0-ci-12026 | 6.74 KB | Sun, 13 Oct 2019 09:53:09 GMT | 0 |