Skip to content
gcsizmadiaPublic

About

System.Text.Json extensions that support any attribute (e.g. JsonStringEnumMemberNameAttribute, EnumMemberAttribute etc.) for enum member serialization and much more...

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Egonsoft.HU JSON Library

GitHub Nuget Nuget

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 JsonSerializerOptions instances.
  • Extension methods for JsonSerializerOptions.
  • Additional JsonNamingPolicy implementations.

Table of Contents

Introduction

The motivation behind this project is to extend System.Text.Json features.

Releases

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.0
net472
netstandard2.0, netstandard2.1
10.0 2026-10-17 10.0.0 2028-11-14 net8.0, net9.0, net10.0
net472
netstandard2.0, netstandard2.1

Features

This library comes with the following features.

EnumValueSerializer

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.

Default implementation

The default implementation supports the following attributes:

  • System.Text.Json.Serialization.JsonStringEnumMemberNameAttribute
  • System.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:

  1. The value of the JsonStringEnumMemberNameAttribute.Name property, if that attribute is applied to the current enum member.
    • Both reading and writing are case sensitive.
  2. The value of the EnumMemberAttribute.Value property, if that attribute is applied to the current enum member.
    • Both reading and writing are case sensitive.
  3. 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.

JsonEnumValueSerializer

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 in System.Text.Json (>= 9.0.0)
  • System.Runtime.Serialization.EnumMemberAttribute
  • System.ComponentModel.DescriptionAttribute
  • System.ComponentModel.DisplayNameAttribute
  • System.ComponentModel.DataAnnotations.DisplayAttribute
    • Supported properties: Description, Name and ShortName

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
    );

Value precedence

Converting / retrieving an enum member value to / from a string uses these values in the following order, from highest to lowest priority:

  1. The value of the selected attribute, if that attribute is applied to the current enum member.
    • Both reading and writing are case sensitive.
  2. The value produced by the specified JsonNamingPolicy.
    • Both reading and writing are case sensitive.
  3. The name of the enum member.
    • Both reading and writing are case sensitive.

Custom implementation of EnumValueSerializer

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.

EgonsoftJsonStringEnumConverterFactory

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 EnumValueSerializer implementation

JsonSerializerOptionsProvider

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();

JsonSerializerOptions extension methods

The following extensions methods are defined:

  • AddEgonsoftJsonStringEnumConverter()
  • CopyTo()

AddEgonsoftJsonStringEnumConverter()

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 JsonStringEnumMemberNameAttribute attribute is applied to an enum member,
  • uses JsonNamingPolicy.CamelCase and
  • allows numbers.

You can customize the settings by passing an instance of EgonsoftHU.Text.Json.Serialization.Converters.EnumConverterOptions as a parameter.

CopyTo()

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.

Naming policies

Although these are not the most wanted naming policies, they are available in case you need them.

  • EgonsoftHU.Text.Json.JsonUpperCaseNamingPolicy
  • EgonsoftHU.Text.Json.JsonLowerCaseNamingPolicy

Example configuration

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:

  1. JsonStringEnumMemberNameAttribute.Name, if that attribute is applied to the current enum member.
    • Both reading and writing are case sensitive.
  2. The value produced by JsonNamingPolicy.CamelCase.
    • Both reading and writing are case sensitive.
  3. 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()
    );

JsonStringEnumMemberNameAttribute support

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.

About

System.Text.Json extensions that support any attribute (e.g. JsonStringEnumMemberNameAttribute, EnumMemberAttribute etc.) for enum member serialization and much more...

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages