序列化到csv或jsv时如何配置ServiceStack.Text以使用EnumMember? [英] How to configure ServiceStack.Text to use EnumMember when serializing to csv or jsv?

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

问题描述

这是我先前关于 ServiceStack.Text并将json反序列化为.Net枚举的问题的后续问题.该问题的答案解决了如何使ServiceStack.Text使用.Net

This is a follow up question to my earlier question about ServiceStack.Text and deserializing json to .Net enums. The answer to that question resolves how to get ServiceStack.Text to use .Net DataContract and EnumMember to map strings to enum values. Now I am trying to serialize the same enums to various formats and while json serialization is working in a round-trip way as expected, both csv and jsv conversion are ignoring the data contract. Is there a way to configure ServiceStack.Text (version 5.1.0, running on .Net Core 2.1) in a way so that the following code would produce the same result of all output types and so that the output would honor the defined data contract?

namespace TestNameSpace
{
    using ServiceStack;
    using System;
    using System.Runtime.Serialization;

    class TestClass
    {
        [DataContract]
        enum TestEnum
        {
            [EnumMember(Value = "enum_value")]
            EnumValue = 0,
        }

        static void Main(string[] args)
        {
            var testEnum = TestEnum.EnumValue;

            Console.WriteLine($"To json: {testEnum.ToJson()}");
            Console.WriteLine($"To csv: {testEnum.ToCsv()}");
            Console.WriteLine($"To jsv: {testEnum.ToJsv()}");
            Console.WriteLine($"To xml: {testEnum.ToXml()}");
        }
    }
}

实际输出是

To json: "enum_value"
To csv: EnumValue
To jsv: EnumValue
To xml: <?xml version="1.0" encoding="utf-8"?><TestClass.TestEnum xmlns="http://schemas.datacontract.org/2004/07/TestNameSpace">enum_value</TestClass.TestEnum>

虽然我也想在csv和jsv输出中看到enum_value.这样可以使用ServiceStack.Text吗?

While I would like to see enum_value also in csv and jsv output. Is this possible with ServiceStack.Text?

推荐答案

现在应该从此提交可从MyGet上的 v5.1.1中获得.

Should now be supported from this commit available from v5.1.1 that's now on MyGet.

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

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