JavaScriptSerializer: how to serialize enum to JSON string instead of number?
Hello everyone! I'm a beginner C# developer, writing a small web application in ASP.NET Web Forms (old school, but it's a legacy project). I'm using `JavaScriptSerializer` to serialize an object to JSON to send to the client.
I have this class:
```csharp
public class User
{
public string Name { get; set; }
public UserRole Role { get; set; }
}
public enum UserRole
{
Admin,
Moderator,
User
}
```
I create an object and serialize it:
```csharp
var user = new User { Name = "Ivan", Role = UserRole.Admin };
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(user);
Console.WriteLine(json);
```
The output is: `{"Name":"Ivan","Role":0}`.
The problem is that `Role` is serialized as a number (0, 1, 2), but I need it to be
No answers yet.
Log in to leave an answer