JavaScriptSerializer – JSON serialization of enum as string

I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string “name”. Is there a way to get the enum as a string in my json without having to create a custom JavaScriptConverter? Perhaps there’s an attribute that I could decorate the enum definition, or object property, with?

As an example:

enum Gender { Male, Female }

class Person
{
    int Age { get; set; }
    Gender Gender { get; set; }
}

Desired JSON result:

{ "Age": 35, "Gender": "Male" }

Ideally looking for answer with built-in .NET framework classes, if not possible alternatives (like Json.net) are welcome.

2
29

Leave a Comment