Here's what I have so far:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace MyProject
{
[TestClass]
public class MyClass
{
[TestMethod]
public void VerifyJsonString()
{
var json = new JObject
{
new JProperty("Thing", Things.Cats)
};
var actual = json.ToString(Formatting.None);
const string expected = "{\"Thing\":\"Cats\"}";
Assert.AreEqual(expected, actual);
}
}
[JsonConverter(typeof(StringEnumConverter))]
public enum Things
{
Apples,
Bananas,
Cats
}
}
Unfortunately, this test fails as it serializes it as {"Thing":2}
instead. How can I get it to serialize the enum properly? I realize I could explicitly call .ToString()
on it but I don't want to. I'd rather have some attribute so I don't have to remember to do that each time.