JavaScriptSerializer and enum to string: top errors and how to avoid them

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why enum serialization is a headache for every C# developer

\n\n

Have 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\n

Let'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\n

The problem: why does enum turn into a number?

\n\n

By 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).

\n\n
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\n

You'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.

\n\n

Top 5 errors when working with JavaScriptSerializer and enum

\n\n

Error 1: Using EnumMemberAttribute without configuration

\n\n

Many 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.

\n\n
[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\n

Error 2: Ignoring ScriptIgnoreAttribute

\n\n

Some try to hide the enum property and then add a string representation separately. This complicates the code and breaks data integrity.

\n\n

Error 3: Manual conversion via if/switch

\n\n

The most common "crutch" is creating an additional string property and manual conversion:

\n\n
public 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\n

The problem: now you have two properties that need to be synchronized. Eventually they will diverge.

\n\n

Error 4: Forgetting about character case

\n\n

Even if you conquer serialization, the client code might expect "active" but receive "Active". Case differences lead to bugs on the JavaScript side.

\n\n

Error 5: Using outdated JavaScriptSerializer instead of Newtonsoft.Json

\n\n

The 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.

\n\n

How to properly serialize enum to string: 3 working solutions

\n\n

Solution 1: Custom converter (recommended approach)

\n\n

Create your own class inheriting from JavaScriptConverter and register it in the serializer.

\n\n
public class EnumToStringConverter<T> : JavaScriptConverter where T : struct\n{\n    public override IEnumerable<Type> SupportedTypes\n                

Blogs

Book Recommendations