Why enum serialization is a headache for every C# developer
\n\nHave you ever encountered a situation where your neat C# code working with enumerations (enum) suddenly starts outputting strange numbers instead of clear names? If so, you are not alone. The question "JavaScriptSerializer - JSON serialization of enum as string" on Stack Overflow has gathered thousands of views, meaning this problem is familiar to many.
\n\nLet's figure out why JavaScriptSerializer by default turns your beautiful enum names into faceless numbers, and most importantly — how to fix this once and for all.
\n\nThe problem: why does enum turn into a number?
\n\nBy default, JavaScriptSerializer (a class from the System.Web.Script.Serialization namespace) serializes enumerations as their integer values. This is because each enum is based on an integer (int by default).
public enum Status\n{\n Active = 1,\n Inactive = 2,\n Pending = 3\n}\n\npublic class Order\n{\n public int Id { get; set; }\n public Status OrderStatus { get; set; }\n}\n\n// Serialization\nvar order = new Order { Id = 1, OrderStatus = Status.Active };\nvar serializer = new JavaScriptSerializer();\nstring json = serializer.Serialize(order);\n// Result: {"Id":1,"OrderStatus":1}\n\n\nYou'll agree, "OrderStatus":1 looks uninformative. Imagine such JSON going to an API — the client code will have to guess what the number 1 means.
Top 5 errors when working with JavaScriptSerializer and enum
\n\nError 1: Using EnumMemberAttribute without configuration
\n\nMany developers try to apply [EnumMember] from the System.Runtime.Serialization namespace, thinking it will solve the problem. But JavaScriptSerializer does not process this attribute by default.
[DataContract]\npublic enum Status\n{\n [EnumMember(Value = "active")]\n Active = 1,\n [EnumMember(Value = "inactive")]\n Inactive = 2\n}\n\n// JavaScriptSerializer will still output numbers\n\n\nError 2: Ignoring ScriptIgnoreAttribute
\n\nSome try to hide the enum property and then add a string representation separately. This complicates the code and breaks data integrity.
\n\nError 3: Manual conversion via if/switch
\n\nThe most common "crutch" is creating an additional string property and manual conversion:
\n\npublic class Order\n{\n public int Id { get; set; }\n public Status OrderStatus { get; set; }\n \n // Data duplication — bad practice\n public string OrderStatusString\n {\n get { return OrderStatus.ToString(); }\n }\n}\n\n\nThe problem: now you have two properties that need to be synchronized. Eventually they will diverge.
\n\nError 4: Forgetting about character case
\n\nEven if you conquer serialization, the client code might expect "active" but receive "Active". Case differences lead to bugs on the JavaScript side.
Error 5: Using outdated JavaScriptSerializer instead of Newtonsoft.Json
\n\nThe JavaScriptSerializer class is considered obsolete in modern versions of .NET. Microsoft recommends using System.Text.Json or Newtonsoft.Json. But if you are working with a legacy project or Web Forms, you cannot avoid it.
How to properly serialize enum to string: 3 working solutions
\n\nSolution 1: Custom converter (recommended approach)
\n\nCreate your own class inheriting from JavaScriptConverter and register it in the serializer.
public class EnumToStringConverter<T> : JavaScriptConverter where T : struct\n{\n public override IEnumerable<Type> SupportedTypes\n