I am using ServiceStack.Text to deserialize json received in rest api calls to objects C#. The model classes I use have defined the string representation using EnumMember attributes. The problem is that ServiceStack.Text does not seem to use those values. ServiceStack.Text documentation has a section called Custom enum serialization that discusses EnumMember attribute, but it talks only about serialization with no mention of deserialization.
It is possible to configure ServiceStack.Text to use EnumMember when deserializing enums?
The following is an example of the situation:
namespace TestNameSpace
{
using System;
using System.Runtime.Serialization;
class TestClass
{
enum TestEnum
{
[EnumMember(Value = "default_value")]
DefaultValue = 0,
[EnumMember(Value = "real_value")]
RealValue = 1
}
class TestEnumWrapper
{
public TestEnum EnumProperty { get; set; }
public override string ToString()
{
return $"EnumProperty: {EnumProperty}";
}
}
static void Main(string[] args)
{
string json = @"{ ""enumProperty"": ""real_value"" }";
TestEnumWrapper deserialized =
ServiceStack.Text.JsonSerializer.DeserializeFromString<TestEnumWrapper>(json);
Console.WriteLine($"Deserialized: {deserialized}");
// Prints: "Deserialized: EnumProperty: DefaultValue"
// Expected: "Deserialized: EnumProperty: RealValue"
}
}
}