JsonEnum.Swagger 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package JsonEnum.Swagger --version 1.0.0
                    
NuGet\Install-Package JsonEnum.Swagger -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonEnum.Swagger" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonEnum.Swagger" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="JsonEnum.Swagger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JsonEnum.Swagger --version 1.0.0
                    
#r "nuget: JsonEnum.Swagger, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=JsonEnum.Swagger&version=1.0.0
                    
Install JsonEnum.Swagger as a Cake Addin
#tool nuget:?package=JsonEnum.Swagger&version=1.0.0
                    
Install JsonEnum.Swagger as a Cake Tool

CI Nuget alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image alternate text is missing from this package README image https://editorconfig.org/

JsonEnum

Converters to customize your enum serialization on System.Text.Json

Getting started

NuGet package available:

$ dotnet add package JsonEnum

Enum Converters

This library defines the following converters:

  • JsonEnumDescriptionConverter
  • JsonEnumMemberValueConverter
  • JsonEnumNumericStringConverter
  • JsonEnumNumericConverter

For each converter above there is also an named attribute to set converters on properties or types.

There is also two others converter attributes to force one of the predefined behaviors(number| string), useful when you have already set a default converter for enums but need a type to be serialized as number or just the name. they are:

  • JsonEnumString - applies EnumStringJsonConverter
  • JsonEnumNumeric - force to use number value

Using [Description]

Enabled adding JsonEnumDescriptionConverter to your json options or using the attribute [JsonEnumDescription] on your type.

using System.Text.Json;
using System.Text.Json.Serialization;
using System.ComponentModel;

public enum MyEnum
{
    [Description("first_value")] Value1,
    [Description("second_value")] Value2,
}
public class Foo
{
    [JsonEnumDescription]
    public MyEnum Value { get; init; }
}

var foo = new Foo { Value = MyEnum.Value1 };

On the above sample foo will be serialized to/from:

{
    "Value": "first_value"
}

EnumMember [Description]

Enabled adding JsonEnumDescriptionConverter to your json options or using the attribute [JsonEnumDescription] on your type.

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Runtime.Serialization;

public enum MyEnum
{
    [EnumMember(Value = "first-value")] Value1,
    [EnumMember(Value = "second-value")] Value2,
}
public class Foo
{
    [JsonEnumMemberValue]
    public MyEnum Value { get; init; }
}

var foo = new Foo { Value = MyEnum.Value1 };

On the above sample foo will be serialized to/from:

{
    "Value": "first-value"
}

EnumMember [Description]

Enabled adding JsonEnumDescriptionConverter to your json options or using the attribute [JsonEnumDescription] on your type.

using System.Text.Json;
using System.Text.Json.Serialization;
using System.Runtime.Serialization;

public enum MyEnum
{
    [EnumMember(Value = "first-value")] Value1,
    [EnumMember(Value = "second-value")] Value2,
}
public class Foo
{
    [JsonEnumMemberValue]
    public MyEnum Value { get; init; }
}

var foo = new Foo { Value = MyEnum.Value1 };

On the above sample foo will be serialized to/from:

{
    "Value": "first-value"
}

Numeric string

Serialize enum as a string of the numeric value of the enum.

using System.Text.Json;
using System.Text.Json.Serialization;

public enum MyEnum
{
    Value1 = 10,
    Value2 = 20,
}
public class Foo
{
    [JsonEnumNumericString]
    public MyEnum Value { get; init; }
}

var foo = new Foo { Value = MyEnum.Value1 };

On the above sample foo will be serialized to/from:

{
    "Value": "10"
}

Numeric

Usually you will don't need this because is the default behavior of enum serialization in .NET, but can be used when you already have set a converter on ASP.NET or JsonSerializerOptions.Converters

using System.Text.Json;
using System.Text.Json.Serialization;

public enum MyEnum
{
    Value1 = 10,
    Value2 = 20,
}
public class Foo
{
    [JsonEnumNumeric]
    public MyEnum Value { get; init; }
}

var foo = new Foo { Value = MyEnum.Value1 };

JsonSerializer.Serialize(foo, new JsonSerializerOptions()
{
    Converters = { new JsonStringEnumConverter() }, // will be ignored on Foo type
};
)

On the above sample foo will be serialized to/from:

{
    "Value": 10
}

Swagger

If you are using Swashbuckle.AspNetCore you will notice that those attributes and converters are not respected by the swagger schema. You can use this nuget package to fix it:

$ dotnet add package JsonEnum.Swagger

And use the EnumJsonSchemaFilter on your swagger configuration:

services.AddSwaggerGen(options =>
{
    options.SchemaFilter<EnumJsonSchemaFilter>();
});
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 233 1/19/2024
1.0.0 441 7/6/2023