Lets you serialize and deserialize StrEnum string enums to JSON via System.Text.Json.
Targets .NET Standard 2.0; works with System.Text.Json 4.6.0 – 10.x.
Install StrEnum.System.Text.Json via the .NET CLI:
dotnet add package StrEnum.System.Text.Json
public class Sport : StringEnum<Sport>
{
public static readonly Sport RoadCycling = Define("ROAD_CYCLING");
}
public class Race
{
public string Name { get; set; }
public Sport Sport { get; set; }
}Call UseStringEnums() on a JsonSerializerOptions instance and pass it to JsonSerializer:
var options = new JsonSerializerOptions().UseStringEnums();var race = new Race { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling };
var json = JsonSerializer.Serialize(race, options);Produces:
{"Name":"Cape Town Cycle Tour","Sport":"ROAD_CYCLING"}The same JSON can be deserialized back to a Race:
var race = JsonSerializer.Deserialize<Race>(json, options);
// race is equivalent to:
new { Name = "Cape Town Cycle Tour", Sport = Sport.RoadCycling };Copyright © 2025 Dmytro Khmara.
StrEnum is licensed under the MIT license.