反序列化时如何配置ServiceStack.Text以使用EnumMember? [英] How to configure ServiceStack.Text to use EnumMember when deserializing?

查看:126
本文介绍了反序列化时如何配置ServiceStack.Text以使用EnumMember?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ServiceStack.Text反序列化在对对象C#的rest api调用中接收到的json.我使用的模型类已使用 EnumMember 属性.问题在于ServiceStack.Text似乎没有使用这些值. ServiceStack.Text 文档有一个名为自定义枚举序列化的部分, EnumMember属性,但只讨论序列化而未涉及反序列化.

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.

可以反序列化枚举时将ServiceStack.Text配置为使用EnumMember吗?

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

推荐答案

我发现了为什么反序列化无法正常工作的原因. ServiceStack.Text没有解释 EnumMember 属性,因为枚举声明没有设置DataContract属性.实际上,我也在问题中链接的 EnumMember 文档链接中对此进行了解释:

I found out why my deserialization was not working. ServiceStack.Text was not interpreting the EnumMember attributes because enum declaration does not have DataContract attribute set. This is actually explained in the EnumMember documentation link I also linked in the question:

在数据协定模型中使用枚举类型的一种方法是将DataContractAttribute属性应用于该类型.然后,您必须将EnumMemberAttribute属性应用于必须包含在数据协定中的每个成员.

One way to use enumeration types in the data contract model is to apply the DataContractAttribute attribute to the type. You must then apply the EnumMemberAttribute attribute to each member that must be included in the data contract.

通过添加缺少的属性产生了预期的结果:

Expected results were produced by adding the missing attribute:

[DataContract] // This was missing
enum TestEnum
{ 
    // ...
}

这篇关于反序列化时如何配置ServiceStack.Text以使用EnumMember?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆