How to Save an Enum as Its Name in JSON with JsonStringEnumConverter in C#
Put [JsonConverter(typeof(JsonStringEnumConverter<Size>))] on an enum, and System.Text.Json writes each value as its name, such as "Large", instead of a number such as 2. It reads the names back too, and you don't pass any options for it.
Microsoft's reference for the converter: Converter to convert enums to and from strings. Without one, the System.Text.Json guide says: By default, enums are serialized as numbers.
You can add the converter to a JsonSerializerOptions every time you serialize, or put it on the enum once. The same guide: You can also specify the converter to use by annotating your enum with JsonConverterAttribute. That attribute's reference: When placed on a property or type, specifies the converter type to use.
Syntax
using System.Text.Json.Serialization;
[JsonConverter(typeof(JsonStringEnumConverter<<EnumName>>))]
enum <EnumName>
{
<Member>,
<Member>
}
| Part | What it is |
|---|---|
[JsonConverter(...)] | The attribute that picks a converter, written on the line above the enum |
typeof(JsonStringEnumConverter<<EnumName>>) | The converter's type. typeof hands the attribute a type, not an object |
<EnumName> | The enum's name, written twice: in the converter and in the declaration. The reference: The enum type that this converter targets. |
<Member> | The enum's members. Their names are the text in the JSON |
Because the attribute is on the enum itself, it applies wherever that enum is serialized. The reference names the exceptions: When placed on a type, the specified converter will be used unless a compatible converter is added to the JsonSerializerOptions.Converters collection or there is another JsonConverterAttribute on a property of the same type.
The Example
A console app called EnumNamesInJson has a Size enum for a clothes shop and an Order class with an item and a size. It writes a T-shirt order in size Large as JSON, then reads a hoodie order whose size is the name Small.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n EnumNamesInJson
cd EnumNamesInJson
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/EnumNamesInJson/EnumNamesInJson.csproj:
Determining projects to restore...
Restored /Users/you/source/EnumNamesInJson/EnumNamesInJson.csproj (in 45 ms).
Restore succeeded.
dotnet new console -n EnumNamesInJson creates a folder named EnumNamesInJson with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or EnumNamesInJson.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. The enum and the class go at the end of the file:
[JsonConverter(typeof(JsonStringEnumConverter<Size>))]
enum Size
{
Small,
Medium,
Large
}
class Order
{
public string Item { get; set; } = "";
public Size Size { get; set; }
}
Each member of Size is a number underneath. The enum reference: By default, the associated constant values of enum members are of type int. They start with zero and increase by one following the definition text order. So Small is 0, Medium is 1 and Large is 2. The attribute above the enum makes the JSON hold the names instead.
At the top of the file, an order to write as JSON:
using System.Text.Json;
using System.Text.Json.Serialization;
// An order for a T-shirt in size Large.
Order order = new() { Item = "T-shirt", Size = Size.Large };
// The converter on the Size enum writes the name, not the number.
string json = JsonSerializer.Serialize(order);
Console.WriteLine(json);
JsonSerializer.Serialize(order) gets no options: the attribute on Size is all it needs.
Then an order to read:
// It also reads a name back into the enum value.
string fromShop = """{"Item":"Hoodie","Size":"Small"}""";
Order hoodie = JsonSerializer.Deserialize<Order>(fromShop)!;
Console.WriteLine($"{hoodie.Item}: {hoodie.Size}, stored as the number {(int)hoodie.Size}");
The JSON holds the size as the text "Small". (int)hoodie.Size turns the enum value into the number it's stored as.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
{"Item":"T-shirt","Size":"Large"}
Hoodie: Small, stored as the number 0

- Writing: the T-shirt's size is the text
"Large", not the number2. - Reading: the name
"Small"becameSize.Small, which is stored as the number0.
The Complete File

using System.Text.Json;
using System.Text.Json.Serialization;
// An order for a T-shirt in size Large.
Order order = new() { Item = "T-shirt", Size = Size.Large };
// The converter on the Size enum writes the name, not the number.
string json = JsonSerializer.Serialize(order);
Console.WriteLine(json);
// It also reads a name back into the enum value.
string fromShop = """{"Item":"Hoodie","Size":"Small"}""";
Order hoodie = JsonSerializer.Deserialize<Order>(fromShop)!;
Console.WriteLine($"{hoodie.Item}: {hoodie.Size}, stored as the number {(int)hoodie.Size}");
[JsonConverter(typeof(JsonStringEnumConverter<Size>))]
enum Size
{
Small,
Medium,
Large
}
class Order
{
public string Item { get; set; } = "";
public Size Size { get; set; }
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving the converter out | The same order was written as {"Item":"T-shirt","Size":2} | Put [JsonConverter(typeof(JsonStringEnumConverter<Size>))] on the enum |
| Reading a name without the converter | "Size":"Small" threw JsonException: The JSON value could not be converted to Size., followed by the path $.Size | Add the converter: with it, the same JSON read as Small |
| A name the enum doesn't have | "Size":"Huge" threw the same JsonException: The JSON value could not be converted to Size. | Send one of the enum's names. The case doesn't matter: "large" read as Large |
The non-generic JsonStringEnumConverter on an enum that a source-generated context serializes | The build warned warning SYSLIB1034: The member 'Size' has been annotated with 'JsonStringEnumConverter' which is not supported in native AOT. Consider using the generic 'JsonStringEnumConverter<TEnum>' instead. | Use JsonStringEnumConverter<Size>: the same program built with no warning |
FAQ
Does the name in the JSON have to match the case?
No. With the converter, "large" read as Large, and a number still works too: 2 read as Large. The converter's reference: Reading is case insensitive, and writing can be customized via a JsonNamingPolicy. Its constructor without arguments creates a converter with the default naming policy and that allows integer values.
Does it work with a source-generated JsonSerializerContext?
Yes, with the generic converter. This context wrote {"Item":"T-shirt","Size":"Large"} through ShopJsonContext.Default.Order:
[JsonSerializable(typeof(Order))]
partial class ShopJsonContext : JsonSerializerContext;
The source generation guide: To serialize enum names as strings using source generation, use the JsonStringEnumConverter<TEnum> converter.
Can one member have a different text in the JSON?
Yes. Put [JsonStringEnumMemberName("XL")] above a member called ExtraLarge, and a jacket order was written as {"Item":"Jacket","Size":"XL"} and read back as ExtraLarge. The guide: Starting in .NET 9, you can customize the names of individual enum members for types that are serialized as strings.
Conclusion
Put [JsonConverter(typeof(JsonStringEnumConverter<<EnumName>>))] on an enum, and System.Text.Json writes its names and reads them back, in any case and as numbers too, with no options to pass and with source-generated contexts as well. Without it, an enum is a number in the JSON, and reading a name throws a JsonException.
Additional Resources
JsonStringEnumConverter<TEnum>— the converter and its constructors.- JsonConverterAttribute — the attribute, and when a converter in the options wins over it.
- Enums as strings — the converter in the options and on the enum, and custom member names.
- SYSLIB1034 — why source generation needs the generic converter.
