System.Text.Json extensions.
- Supports advanced enum member serialization in the following order:
- Any attribute applied to an enum member, built-in support for 5 attributes.
JsonNamingPolicy- The enum member name as it is written.
- Supports custom serializer implementations for enum members.
- Pre-configured
JsonSerializerOptionsinstances. - Extension methods for
JsonSerializerOptions. - Additional
JsonNamingPolicyimplementations.
The motivation behind this project is to extend System.Text.Json features.
You can download the package from nuget.org: EgonsoftHU.Text.Json
You can find the release notes here.
Releases under active support or development:
| Version | Release Date | Latest Patch Version | End of Support | Targets |
|---|---|---|---|---|
| 11.0 | 2026-11-12 | 11.0.0 | 2028-11-14 | net10.0, net11.0net472netstandard2.0, netstandard2.1 |
| 10.0 | 2026-10-17 | 10.0.0 | 2028-11-14 | net8.0, net9.0, net10.0net472netstandard2.0, netstandard2.1 |
This library comes with the following features.
This library makes use of an implementation of this base class.
Please note:
This base class has been moved from EgonsoftHU.Extensions.Bcl nuget package to this nuget package.
The default implementation supports the following attributes:
System.Text.Json.Serialization.JsonStringEnumMemberNameAttributeSystem.Runtime.Serialization.EnumMemberAttribute
Converting / retrieving an enum member value to / from a string uses these values in the following order, from highest to lowest priority:
- The value of the
JsonStringEnumMemberNameAttribute.Nameproperty, if that attribute is applied to the current enum member.- Both reading and writing are case sensitive.
- The value of the
EnumMemberAttribute.Valueproperty, if that attribute is applied to the current enum member.- Both reading and writing are case sensitive.
- The name of the enum member.
- Both reading and writing are case sensitive.
Please note:
This implementation makes use of the EgonsoftHU.Extensions.Bcl.Enumerations.EnumInfo<TEnum> type.
This is an implementation of the above mentioned EgonsoftHU.Text.Json.Serialization.Converters.EnumValueSerializer type and
it supports attributes and System.Text.Json.JsonNamingPolicy to convert/retrieve enum member values to/from a string.
The following attributes are supported:
System.Text.Json.Serialization.JsonStringEnumMemberNameAttribute- available inSystem.Text.Json(>= 9.0.0)System.Runtime.Serialization.EnumMemberAttributeSystem.ComponentModel.DescriptionAttributeSystem.ComponentModel.DisplayNameAttributeSystem.ComponentModel.DataAnnotations.DisplayAttribute- Supported properties:
Description,NameandShortName
- Supported properties:
To create an instance select a naming policy and an attribute using the EgonsoftHU.Text.Json.Serialization.EnumMemberNameSelectorOption enum type:
using System.Text.Json;
using EgonsoftHU.Text.Json.Serialization;
using EgonsoftHU.Text.Json.Serialization.Converters;
var serializer =
new JsonEnumValueSerializer(
JsonNamingPolicy.CamelCase,
EnumMemberAttributeSelectorOption.UseJsonStringEnumMemberNameAttribute
);Converting / retrieving an enum member value to / from a string uses these values in the following order, from highest to lowest priority:
- The value of the selected attribute, if that attribute is applied to the current enum member.
- Both reading and writing are case sensitive.
- The value produced by the specified
JsonNamingPolicy.- Both reading and writing are case sensitive.
- The name of the enum member.
- Both reading and writing are case sensitive.
You can create and use your own EnumValueSerializer implementation if the above mentioned
JsonEnumValueSerializer implementation does not fit your needs.
Additionally, you can use the EnumInfo<TEnum>.Attributes instance property to access all the
attributes that are applied to an enum member.
This is an implementation of the System.Text.Json.Serialization.JsonConverterFactory that you can add to the
System.Text.Json.JsonSerializerOptions.Converters collection.
You can specify
- a
JsonNamingPolicy - if numbers are allowed during both serialization and deserialization
- an instance of an
EnumValueSerializerimplementation
This is a static class that provides
- read-only default (
Default,DefaultWriteIndented) properties and - a writable current (
Current) property
to hold instances of the System.Text.Json.JsonSerializerOptions type.
The default instance is created this way:
JsonSerializerOptions options =
new(JsonSerializerDefaults.Web)
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
WriteIndented = false
};
options.AddEgonsoftJsonStringEnumConverter();The following extensions methods are defined:
AddEgonsoftJsonStringEnumConverter()CopyTo()
You can easily add this converter to your JsonSerializerOptions instance.
using System.Text.Json;
using EgonsoftHU.Text.Json;
JsonSerializerOptions options = /* omitted for clarity */
options.AddEgonsoftJsonStringEnumConverter();By default, this will add a converter that:
- checks if
JsonStringEnumMemberNameAttributeattribute is applied to an enum member, - uses
JsonNamingPolicy.CamelCaseand - allows numbers.
You can customize the settings by passing an instance of EgonsoftHU.Text.Json.Serialization.Converters.EnumConverterOptions as a parameter.
Suppose you already have an existing instance of the JsonSerializerOptions class that you would like to use when configuring ASP.NET Core.
This CopyTo() extension method comes in handy in this case:
using EgonsoftHU.Text.Json;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
JsonSerializerOptions options = /* your already existing instance */;
builder
.Services
.AddControllers()
// source.CopyTo(target) will override target's settings with the source's settings.
.AddJsonOptions(jsonOptions => options.CopyTo(jsonOptions.JsonSerializerOptions));Please note:
By default, this method will not copy JsonSerializerOptions.TypeInfoResolver property value.
If it should also be copied then set the method's overrideTypeResolver parameter to true.
Although these are not the most wanted naming policies, they are available in case you need them.
EgonsoftHU.Text.Json.JsonUpperCaseNamingPolicyEgonsoftHU.Text.Json.JsonLowerCaseNamingPolicy
In this example configuration converting / retrieving an enum member value to / from a string will use these values in the following order, from highest to lowest priority:
JsonStringEnumMemberNameAttribute.Name, if that attribute is applied to the current enum member.- Both reading and writing are case sensitive.
- The value produced by
JsonNamingPolicy.CamelCase.- Both reading and writing are case sensitive.
- The name of the enum member.
- Both reading and writing are case sensitive.
using System.Text.Json;
using EgonsoftHU.Text.Json;
using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args);
builder
.Services
.AddControllers()
.AddJsonOptions(
jsonOptions => jsonOptions.JsonSerializerOptions.AddEgonsoftJsonStringEnumConverter()
);This nuget package references the 11.0.x version of the System.Text.Json nuget package,
so this attribute is available for all targets of this nuget package.