使用ServiceStack.Text

时间:2016-04-25 15:17:02

标签: c# csv servicestack servicestack-text

我正在尝试使用ServiceStack.Text反序列化包含“;”的csv文件作为分离者。

测试csv包含此数据

---------------
| Col1 | Col2 |
---------------
| Val1 | Val2 |
---------------
| Val3 | Val4 |
---------------

public class Line
{
    public string Col1 { get; set; }
    public string Col2 { get; set; }
}

有效的代码:

var CsvWithComma = "Col1,Col2" + Environment.NewLine + 
"Val1,Val2" + Environment.NewLine +
"Val3,Val3" + Environment.NewLine;

var r1 = ServiceStack.Text.CsvSerializer.DeserializeFromString<List<Line>>(CsvWithComma);

Assert.That(r1.Count() == 2, "It should be 2 rows");
Assert.That(r1[0].Col1 == "Val1", "Expected Val1");
Assert.That(r1[0].Col2 == "Val2", "Expected Val2");

失败的代码:

ServiceStack.Text.CsvConfig.ItemSeperatorString = ";";

var CsvWithSemicolon = "Col1;Col2" + Environment.NewLine +
"Val1;Val2" + Environment.NewLine +
"Val3;Val3" + Environment.NewLine;

var r2 = ServiceStack.Text.CsvSerializer.DeserializeFromString<List<Line>>(CsvWithSemicolon);

Assert.That(r2.Count() == 2, "It should be 2 rows");
Assert.That(r2[0].Col1 == "Val1", "Expected Val1");
Assert.That(r2[0].Col2 == "Val2", "Expected Val2");

当我深入研究ServiceStack时,似乎它使用ServiceStack.Text.Common.JsWriter.ItemSeperator作为CSV阅读器的分隔符,而不是ServiceStack.Text.CsvConfig.ItemSeperatorString。

有人有这个工作吗?

1 个答案:

答案 0 :(得分:1)

现在应该this commit支持此功能。

此更改可从v4.0.57 +现在available from MyGet获得。