2

I have a Dictionary that has key: a string, value: a List of Entity, where Entity is a class:

public class Entity
{
    public string myString { get; set; }
    public int myInt { get; set; }
}

Dictionary<string, List<Entity>> myDictionary = new Dictionary<string, List<Entity>>();

List<Entity> aList = new List<Entity>();
Entity entity1 = new Entity( myString = "hi", myInt = 111);
Entity entity2 = new Entity( myString = "hello", myInt = 222);
aList.Add(entity1);
aList.Add(entity2);
myDictionary.Add("keyOne", aList);

I am trying to use ServiceStack.Text's CsvSerializer.SerializeToCsv() to serialise the dictionary and save it to a csv file.

string csv = CsvSerializer.SerializeToCsv(myDictionary);
File.WriteAllText(@"testsave.csv", csv);

But when run it and check the testsave.csv file, each entry in the dictionary is saved as a single element in a cell, and the element is saved horizontally:

Example: there are 3 entries in the dictionary and it will be stored to csv like this

Dictionary

So my question is, am I doing it right? and if I am, how do I deserealize the csv file and store it back into myDictionary later on.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • This question is **specific** to `ServiceStack.Text. This format (which isn't a CSV) is something specific to that library. That library should be able to deserialize the text it produces. Other applications won't be able to read that format though – Panagiotis Kanavos Oct 09 '20 at 06:10
  • ah I see, thanks for helping me edit it! – Sze Toh Ye Yang Oct 09 '20 at 06:39
  • Have you tried the answer's example yet? Did you encounter a problem with `FromCsv`? In the answer's link ServiceStack's author calls this format the [Super CSV](https://docs.servicestack.net/csv-format.html#super-csv-format) format and posts an example. If you want the file to be used by other applications or humans, you'll have to be more specific. Perhaps JSON would be better. Or an Excel sheet, if this is meant for humans – Panagiotis Kanavos Oct 09 '20 at 07:36
  • Yes I changed to using System.Text.Json instead and save the file as a txt file. It is working fine now – Sze Toh Ye Yang Oct 09 '20 at 08:54

2 Answers2

2

CSV is not a very good match for hierarchical data storage. It is working as expected.

You may consider switching to json or xml serialization both of which do support hierarchical data and have feature complete serialization libraries available.

  • JSON.Net
  • System.Text.Json
  • System.Xml.*
Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
2

Yes, this is correct. You can deserealize it using the FromCsv method:

var dictionary = File.ReadAllText("path/to/testsave.csv")
                     .FromCsv<Dictionary<string, List<Entity>>>();

You can read about this method here.

It is remarkable to note here that the derived file, when you use CsvSerializer.SerializeToCsv is not a CSV file with the strict definition of a CSV file, but the file that ServiceStack's class CsvSerializer generates.

Christos
  • 53,228
  • 8
  • 76
  • 108