private static void CsvTestEx() { Dictionary <string, VerbWrapper> dictionary = new Dictionary <string, VerbWrapper>(); string filename = @"C:\Temp\jehle_dcomtois_updates.csv"; var contents = File.ReadAllText(filename, Encoding.UTF7).Split('\n'); var csv = from line in contents select line.Split(',').ToArray(); foreach (var row in csv) { var result = ProcessLine(row[0]); if (row[0].Length > 0) { if (!dictionary.ContainsKey(result.Name)) { dictionary.Add(result.Name, new VerbWrapper() { Name = result.Name, Translation = GetTranslation(result.Name), Suffix = GetSuffix(result.Name), Irregular = false, VerbForms = new List <Verb> { result } }); } else { VerbWrapper val = dictionary[result.Name]; val.VerbForms.Add(result); dictionary[result.Name] = val; //val.Add(result); //dictionary[result.Name] = val; } } } int count = dictionary.Count; JsonSerializer serializer = new JsonSerializer(); serializer.NullValueHandling = NullValueHandling.Ignore; serializer.Formatting = Formatting.Indented; using (StreamWriter sw = new StreamWriter(@"c:\temp\outEx.json")) { using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, dictionary); } } Console.ReadKey(); }
private static void ReadBack() { Dictionary <string, VerbWrapper> source = new Dictionary <string, VerbWrapper>(); using (StreamReader reader = new StreamReader(@"C:\Temp\outEx.json")) { string json = reader.ReadToEnd(); source = JsonConvert.DeserializeObject <Dictionary <string, VerbWrapper> >(json); } Dictionary <string, VerbWrapper2> target = new Dictionary <string, VerbWrapper2>(); foreach (var input in source) { VerbWrapper vw = input.Value; List <Verb> lst = vw.VerbForms; List <Verb2> lst2 = new List <Verb2>(); foreach (var l in lst) { var v2 = new Verb2(); v2.Modo = l.Modo; v2.Tense = l.Tense; v2.Inflections = l.Inflections; lst2.Add(v2); } VerbWrapper2 vw2 = new VerbWrapper2(); vw2.Name = vw.Name; vw2.Irregular = vw.Irregular; vw2.Suffix = vw.Suffix; vw2.Translation = vw.Translation; vw2.VerbForms = lst2; target.Add(input.Key, vw2); } int n = target.Count; JsonSerializer serializer = new JsonSerializer(); serializer.NullValueHandling = NullValueHandling.Ignore; serializer.Formatting = Formatting.Indented; using (StreamWriter sw = new StreamWriter(@"c:\temp\Better.json")) { using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, target); } } n = target.Count; }