使用ServiceStack.Text使用CustomHeaders反序列化CSV:不工作?

时间:2017-11-17 23:52:36

标签: c# csv deserialization servicestack servicestack-text

考虑以下课程:

class Foo {
    public string bar { get; set; }
    public string rab { get; set; }

    public override string ToString()
    {
        return string.Format("[Foo bar={0}, rab={1}]", bar, rab);
    }
}

以下代码:

var csv1 =
    "bar,rab" + Environment.NewLine +
    "a,b" + Environment.NewLine +
    "c,d" + Environment.NewLine +
    "e,f" + Environment.NewLine;

var csv2 =
    "xbar,xrab" + Environment.NewLine +
    "a,b" + Environment.NewLine +
    "c,d" + Environment.NewLine +
    "e,f" + Environment.NewLine;

Console.WriteLine(CsvSerializer.DeserializeFromString<List<Foo>>(csv1).First());

CsvConfig<TMDBEntity>.CustomHeadersMap = new Dictionary<string, string> {
    {"bar", "xbar"},
    {"rab", "xrab"}
};

Console.WriteLine(CsvSerializer.DeserializeFromString<List<Foo>>(csv2).First());

正在打印:

[Foo bar=a, rab=b]
[Foo bar=, rab=]

虽然我希望它能打印

[Foo bar=a, rab=b]
[Foo bar=a, rab=b]

为什么不使用xbar和xrab来获取自定义标头?

ServiceStack.Text版本为4.5.14.0

1 个答案:

答案 0 :(得分:2)

此问题应从this commit解决,该问题可从现在available on MyGet的ServiceStack v5.0.0获得。

请注意,您需要配置CsvConfig<Foo>才能更改Foo反序列化的方式,例如:

CsvConfig<Foo>.CustomHeadersMap = new Dictionary<string, string> {
    {"bar", "xbar"},
    {"rab", "xrab"}
};