使用CSVHelper如何使用子项列表反序列化CSV

问题描述 投票:3回答:2

Question:

鉴于对象FooBar包含Bar列表,FooBarBar定义如下:

class FooBar{
    int FooID {get;set;}
    string FooProperty1 {get;set;}
    List<Bar> Bars {get;set;};
}

class Bar{
    int BarID {get;set;}    
    string BarProperty1 {get;set;}  
    string BarProperty2 {get;set;}  
    string BarProperty3 {get;set;}
}

我得到以下CSV作为输入:

1,FooProperty1,BarID_1,BarProperty1_1,BarProperty2_1,BarProperty3_1,BarID_2,BarProperty1_2,BarProperty2_2,BarProperty3_2

字段BarID,BarProperty1,BarProperty2,BarProperty3的后缀是其集合中的indice。

如何将此输入反序列化为我的对象?

enter image description here


input Exemple:

1个FooBar实例,2个子Bar:1,FooProperty1,BarID_1,BarProperty1_1,BarProperty2_1,BarProperty3_1,BarID_2,BarProperty1_2,BarProperty2_2,BarProperty3_2

1个FooBar实例但没有Bar: 1,FooProperty1


Tries:

我尝试使用Convert来将这些属性映射到Bar的新实例,如:

public class FooBarMap : ClassMap<FooBar> 
{
    public FooBarMap()
    {
        Map(m => m.FooID);
        Map(m => m.Bars).ConvertUsing(row =>
        {            
            var list = new List<Bar>
            {
                new Bar { 
                    BarProperty1 = row.GetField("BarProperty1_1"), 
                    BarProperty2 = row.GetField("BarProperty2_1"),
                    // .. Other Properties
                },
                new Bar {}, //.. Same on _2
            };
            return list;
        });
    }
}

当然无法控制输入。我本来就是发送Json / Xml而不是CSV。

c# csv csvhelper
2个回答
4
投票

它可能与自定义类型转换器,但棘手。

你需要使用Index属性来装饰属性(即使它没有被使用)

public class FooBar
{
    [Index(2)]
    public List<Bar> Bars { get; set; }
}

转换器用于读取和写入,因此您需要覆盖两种方法:

  public class BarListConverter : DefaultTypeConverter
  {
    public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
    {
      var list = new List<Bar>();
      if (text == null) return list;
      do
      {
        var barIndex = list.Count + 1;
        var bar = new Bar
        {
          BarID = row.GetField<int>($"BarID_{barIndex}"),
          BarProperty1 = row.GetField<string>($"BarProperty1_{barIndex}"),
          BarProperty2 = row.GetField<string>($"BarProperty2_{barIndex}"),
          BarProperty3 = row.GetField<string>($"BarProperty3_{barIndex}")
        };
        list.Add(bar);
      } while (row.Context.CurrentIndex > 0 && row.Context.CurrentIndex < row.Context.Record.Length - 1);
      return list;
    }

    public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
    {
      var bars = value as List<Bar>;
      if (bars == null) return null;
      foreach (var bar in bars)
      {
        row.WriteField(bar.BarID);
        row.WriteField(bar.BarProperty1);
        row.WriteField(bar.BarProperty2);
        row.WriteField(bar.BarProperty3);
      }
      return null;
    }
  }
}

读:

  public List<FooBar> Reading()
  {
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvReader(reader))
    {
      writer.WriteLine(
        "FooID,FooProperty1,BarID_1,BarProperty1_1,BarProperty2_1,BarProperty3_1,BarID_2,BarProperty1_2,BarProperty2_2,BarProperty3_2");
      writer.WriteLine("1,Foo1,1,2,3,4,5,6,7,8");
      writer.Flush();
      stream.Position = 0;

      csv.Configuration.HeaderValidated = null;
      csv.Configuration.MissingFieldFound = null;
      csv.Configuration.TypeConverterCache.AddConverter<List<Bar>>(new BarListConverter());

      return csv.GetRecords<FooBar>().ToList();
    }
  }

写作:

  public string Writing(List<FooBar> data)
  {
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    using (var reader = new StreamReader(stream))
    using (var csv = new CsvWriter(writer))
    {
      csv.Configuration.HasHeaderRecord = false;
      csv.Configuration.TypeConverterCache.AddConverter<List<Bar>>(new BarListConverter());
      csv.WriteRecords(data);
      writer.Flush();
      stream.Position = 0;

      return reader.ReadToEnd();
    }

1
投票

我已经做了一个非常简化的方法,它将迭代键/值对...

private static FooBar Parse(string value)
{
    // a basic check for null or empty string
    if (String.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));

    // split the string
    string[] split = value.Split(',');

    // a basic check for properly formed key/value pairs in the string
    if (split.Length < 2 || split.Length % 2 != 0)
        throw new ArgumentException("Malformed string.", nameof(value));

    // put the values into our object
    var result = new FooBar();

    // Foo pair
    result.FooID = Int32.Parse(split[0]);
    result.FooProperty1 = split[1];

    // Bar pairs
    result.Bars = new List<Bar>();
    for (int i = 2; i < split.Length; i = i + 4)
    {
        result.Bars.Add(new Bar()
        {
            BarID = split[i],
            BarProperty1 = split[i+1],
            BarProperty2 = split[i+2],
            BarProperty3 = split[i+3]
        });
    }

    return result;
}

我这样反对这两个例子

string value = "1,FooProperty1";
FooBar result = Parse(value); // works

string value = "1,FooProperty1,BarID_1,BarProperty1_1,BarProperty2_1,BarProperty3_1,BarID_2,BarProperty1_2,BarProperty2_2,BarProperty3_2";
FooBar result = Parse(value); // works

请注意,您的课程需要略微更新为

public class FooBar
{
    public int FooID { get; set; }
    public string FooProperty1 { get; set; }
    public List<Bar> Bars { get; set; }
}

public class Bar
{
    public string BarID { get; set; } // you originally had this as int
    public string BarProperty1 { get; set; }
    public string BarProperty2 { get; set; }
    public string BarProperty3 { get; set; }
}

对我来说,这个选项似乎没有那么复杂,而且更容易阅读。我没有特别看到你必须强迫转换通过其他方式完成,例如使用CSVHelper类。

© www.soinside.com 2019 - 2024. All rights reserved.