jesus_
jesus_

Reputation: 11

Deserialize CSV with CustomHeaders using ServiceStack.Text: not working?

consider following class:

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);
    }
}

And following code:

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());

It is printing:

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

while I would expect it to print

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

Why is it not picking up the custom headers using xbar and xrab?

ServiceStack.Text version is 4.5.14.0

Upvotes: 1

Views: 872

Answers (1)

mythz
mythz

Reputation: 143319

This issue should be resolved from this commit which is available from ServiceStack v5.0.0 that's now available on MyGet.

Note you need to configure CsvConfig<Foo> in order to change how Foo is deserialized, e.g:

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

Upvotes: 2

Related Questions