private static void SaveWords() { //System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(WordRepository); List<Word> listOfWords = wordDictionary.Values.OrderBy(w => w.Token).ToList(); WordRepository repository = new WordRepository() { WordList = new List<Word>(), }; foreach (Word word in listOfWords) { Dictionary<string, FriendlyGrammar> friendlyGrammaticalDictionary = null; if (word.GrammaticalClass != null) { friendlyGrammaticalDictionary = new Dictionary<string, FriendlyGrammar>(); foreach (Grammar grammar in word.GrammaticalClass.Values) { friendlyGrammaticalDictionary.Add(grammar.GrammaticalClass.ToString(), new FriendlyGrammar() { GenderVariation = grammar.GenderVariation.ToString(), NumberVariation = grammar.NumberVariation.ToString(), GrammaticalClass = grammar.GrammaticalClass.ToString(), }); } } Word repositoryWord = new Word() { Token = word.Token, GrammaticalClass = word.GrammaticalClass, FriendlyGrammaticalClass = friendlyGrammaticalDictionary, GrammaticalClassAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(word.GrammaticalClass), }; repository.WordList.Add(repositoryWord); } string serializedJson = Newtonsoft.Json.JsonConvert.SerializeObject(repository); File.WriteAllText(jsonRepoPath, serializedJson); //using (Stream stream = new FileStream(xmlRepoPath, FileMode.Create)) //{ // System.Xml.Serialization.XmlSerializer xmlserializer = new System.Xml.Serialization.XmlSerializer(typeof(WordRepository)); // xmlserializer.Serialize(stream, repository); //} //string serializedXml = ServiceStack.Text.XmlSerializer.SerializeToString<WordRepository>(repository); //File.WriteAllText(repoPath, serializedXml); }
private static void LoadDictionary() { Stopwatch watch = new Stopwatch(); watch.Start(); WordRepository repository = new WordRepository(); string serializedJson = File.ReadAllText(jsonRepoPath); repository = Newtonsoft.Json.JsonConvert.DeserializeObject<WordRepository>(serializedJson); foreach (Word word in repository.WordList) { if (!wordDictionary.ContainsKey(word.Token)) { word.GrammaticalClass = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<GrammaticalClassEnum, Grammar>>(word.GrammaticalClassAsJson); wordDictionary.TryAdd(word.Token, word); } } watch.Stop(); System.Diagnostics.Debug.WriteLine(string.Format("LoadDictionary: {0:D2}h{1:D2}m{2:D2}s", watch.Elapsed.Hours, watch.Elapsed.Minutes, watch.Elapsed.Seconds)); }