In Json.NET, you can use the JsonProperty attribute to specify the JSON property name for a class property. This attribute can be used to specify a different name for an enum value in the JSON serialization.
Here is an example of how to serialize enums to different property names using Json.NET:
using Newtonsoft.Json; public enum Status { [JsonProperty("open")] Open, [JsonProperty("in_progress")] InProgress, [JsonProperty("closed")] Closed } public class Item { public int Id { get; set; } [JsonProperty("item_name")] public string Name { get; set; } public Status Status { get; set; } } In this example, the JsonProperty attribute is used to specify a different name for each enum value in the Status enumeration. This attribute is applied to each enum value using the [JsonProperty("name")] syntax.
In the Item class, the JsonProperty attribute is also used to specify a different name for the Name property. The Status property is not decorated with a JsonProperty attribute, so it will use the default property name in the JSON serialization.
When this class is serialized to JSON using the JsonConvert.SerializeObject() method, the enum values will be serialized to the property names specified by the JsonProperty attribute:
Item item = new Item { Id = 1, Name = "My Item", Status = Status.InProgress }; string json = JsonConvert.SerializeObject(item); // Output: {"Id":1,"item_name":"My Item","status":"in_progress"} Console.WriteLine(json); In this output, you can see that the Status enum value is serialized to the status property in the JSON, using the name specified by the JsonProperty attribute.
Serialize enums with custom property names using JSON.NET
public enum Status { [JsonProperty("Active")] Active, [JsonProperty("Inactive")] Inactive } Customize enum serialization property names JSON.NET
[JsonConverter(typeof(StringEnumConverter))] public enum Status { [EnumMember(Value = "Active")] Active, [EnumMember(Value = "Inactive")] Inactive } JSON.NET enum serialization with different property names
public class MyClass { [JsonConverter(typeof(StringEnumConverter))] public Status MyStatus { get; set; } } Rename enum properties during JSON serialization using JSON.NET
public class MyClass { [JsonProperty("NewPropertyName")] public Status MyStatus { get; set; } } Change JSON property names for enum serialization in C#
public class MyClass { [JsonProperty("CustomPropertyName")] public Status MyStatus { get; set; } } Serialize enums to JSON with custom property names in C#
public enum Status { [EnumMember(Value = "CustomActive")] Active, [EnumMember(Value = "CustomInactive")] Inactive } JSON.NET enum serialization with alternate property names
public enum Status { [JsonProperty("Enabled")] Active, [JsonProperty("Disabled")] Inactive } Customize JSON property names for enum serialization JSON.NET
public class MyClass { [JsonConverter(typeof(StringEnumConverter))] [JsonProperty("CustomName")] public Status MyStatus { get; set; } } Change enum property names in JSON serialization JSON.NET
public enum Status { [EnumMember(Value = "NewActiveName")] Active, [EnumMember(Value = "NewInactiveName")] Inactive } Serialize enums with custom names in JSON.NET
public enum Status { [EnumMember(Value = "CustomActive")] Active, [EnumMember(Value = "CustomInactive")] Inactive } dfa derived-column dynamic-html scalar waitress scrapy date-fns built-in google-sheets shapefile