C# - How to make Newtonsoft serialize enums as strings instead of integers

Using Newtonsoft.Json you might be annoyed that the default way to serialize enums is serializing them to integers and not strings. However you can change this behavior by providing a JsonSerializerSettings and using a StringEnumConverter when calling your SerializeObject method. You can see an example of this below:

var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.Converters.Add(
   new Newtonsoft.Json.Converters.StringEnumConverter());

var jsonString = JsonConvert.SerializeObject(
   yourObject, jsonSerializerSettings);

You can even provide the StringEnumConverter directly to the SerializeObject` call:

var jsonString = JsonConvert.SerializeObject(
   yourObject, new Newtonsoft.Json.Converters.StringEnumConverter());

I hope this post helps you to serialize your objects enums as strings rather than integers, feel free to leave a comment down below!