序列化到csv或jsv时如何配置ServiceStack.Text以使用EnumMember? [英] How to configure ServiceStack.Text to use EnumMember when serializing to csv or jsv?
问题描述
这是我先前关于 ServiceStack.Text并将json反序列化为.Net枚举的问题的后续问题.该问题的答案解决了如何使ServiceStack.Text使用.Net EnumMember 将字符串映射到枚举值.现在,我正在尝试将相同的枚举序列化为各种格式,而json序列化按预期的往返方式工作,csv和jsv转换都忽略了数据协定.有没有一种方法可以配置ServiceStack.Text(5.1.0版,在.Net Core 2.1上运行),以便以下代码将产生所有输出类型相同的结果,并且输出将遵循已定义的数据约定?
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?
推荐答案
Should now be supported from this commit available from v5.1.1 that's now on MyGet.
这篇关于序列化到csv或jsv时如何配置ServiceStack.Text以使用EnumMember?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!