JSON serialization of c# enum as string

Posted by ob on Stack Overflow See other posts from Stack Overflow or by ob
Published on 2010-03-14T05:18:56Z Indexed on 2010/03/14 5:25 UTC
Read the original article Hit count: 2005

Filed under:
|
|
|

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" }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about enum