private static Dictionary <TKey, TValue> RetrieveDictionaryGeneric <TKey, TValue>(Node node) { bool keyIsBase = BaseTypesManager.IsBaseType(typeof(TKey)); var dictionary = new Dictionary <TKey, TValue>(capacity: node.ChildNodes.Count); if (keyIsBase && node.ChildNodeType == NodeChildrenType.key) { foreach (var child in node.ChildNodes) { string childKey = (child as KeyNode).Key; var key = BaseTypesManager.ParseBaseType <TKey>(childKey); var value = NodeManager.GetNodeData <TValue>(child); dictionary.Add(key, value); } } else { var array = NodeManager.GetNodeData <WritableKeyValuePair <TKey, TValue>[]>(node); foreach (var kvp in array) { dictionary.Add(kvp.key, kvp.value); } } return(dictionary); }
private static void SetDictionaryNodeGeneric <TKey, TValue>(Node node, Dictionary <TKey, TValue> dictionary, FileStyle style, bool forceArrayMode = false) { bool keyIsBase = BaseTypesManager.IsBaseType(typeof(TKey)); if (keyIsBase && !forceArrayMode && !style.AlwaysArrayDictionaries) { // we might have switched between standard and array dictionary storage, and if so, children need to be reset if (node.ChildNodeType != NodeChildrenType.key) { node.ClearChildren(newChildrenType: NodeChildrenType.key); } var CurrentKeys = new List <string>(capacity: dictionary.Count); foreach (var key in dictionary.Keys) { var value = dictionary[key]; string keyAsText = BaseTypesManager.SerializeBaseType <TKey>(key, style); if (!Utilities.IsValidKey(keyAsText)) { SetDictionaryNodeGeneric(node, dictionary, style, forceArrayMode: true); return; } CurrentKeys.Add(keyAsText); KeyNode child = node.GetChildAddressedByName(keyAsText); NodeManager.SetNodeData <TValue>(child, value, style); } // make sure that old data in the file is deleted when a new dictionary is saved. // node.ClearChildren() is not used because we want to keep comments and whitespace intact as much as possible. foreach (var key in node.GetChildKeys()) { if (!CurrentKeys.Contains(key)) { node.RemoveChild(key); } } } else // save dictionary as KeyValuePair<TKey, TValue>[] { // we might have switched between standard and array dictionary storage, and if so, children need to be reset if (node.ChildNodeType != NodeChildrenType.list) { node.ClearChildren(newChildrenType: NodeChildrenType.list); } var array = GetWritableKeyValuePairArray(dictionary); NodeManager.SetNodeData(node, array, array.GetType(), style); } }
/// <summary> Interpret this file as a dictionary. Top-level keys in the file are interpreted as keys in the dictionary. </summary> /// <remarks> TKey must be a Base Type </remarks> public Dictionary <TKey, TValue> GetAsDictionary <TKey, TValue>() { if (!BaseTypesManager.IsBaseType(typeof(TKey))) { throw new Exception("When using GetAsDictionary, TKey must be a base type"); } var keys = this.TopLevelKeys; var dictionary = new Dictionary <TKey, TValue>(capacity: keys.Count); foreach (var keyText in keys) { TKey key = BaseTypesManager.ParseBaseType <TKey>(keyText); TValue value = NodeManager.GetNodeData <TValue>(TopLevelNodes[keyText]); dictionary.Add(key, value); } return(dictionary); }