/// <summary> Non-generic version of SetAtPath. You probably want to use SetAtPath. </summary> public void SetAtPathNonGeneric(Type type, object value, params string[] path) { if (value != null && value.GetType() != type) { throw new InvalidCastException($"{nameof(value)} is not of type {type}!"); } if (path.Length < 1) { throw new ArgumentException($"{nameof(path)} must have a length greater than 0"); } if (!KeyExists(path[0])) { var newnode = new KeyNode(indentation: 0, key: path[0], file: this); TopLevelNodes.Add(path[0], newnode); TopLevelLines.Add(newnode); } var topNode = TopLevelNodes[path[0]]; for (int i = 1; i < path.Length; i++) { topNode = topNode.GetChildAddressedByName(path[i]); } NodeManager.SetNodeData(topNode, value, type, Style); if (AutoSave) { SaveAllData(); } }
/// <summary> Non-generic version of Set. You probably want to use Set. </summary> /// <param name="type"> the type to save the data as </param> /// <param name="key"> what the data is labeled as within the file </param> /// <param name="value"> the value to save </param> public void SetNonGeneric(Type type, string key, object value) { if (value == null) { throw new Exception("you can't serialize null"); } if (value.GetType() != type) { throw new InvalidCastException($"{value} is not of type {type}!"); } if (!KeyExists(key)) { var newnode = new KeyNode(indentation: 0, key, file: this); TopLevelNodes.Add(key, newnode); TopLevelLines.Add(newnode); } var node = TopLevelNodes[key]; NodeManager.SetNodeData(node, value, type, Style); if (AutoSave) { SaveAllData(); } }
/// <summary> Remove a top-level key and all its data from the file </summary> public void DeleteKey(string key) { if (!KeyExists(key)) { return; } Node node = TopLevelNodes[key]; TopLevelNodes.Remove(key); TopLevelLines.Remove(node); }
/// <summary> Non-generic version of Set. You probably want to use Set. </summary> /// <param name="type"> the type to save the data as </param> /// <param name="key"> what the data is labeled as within the file </param> /// <param name="value"> the value to save </param> public void SetNonGeneric(Type type, string key, object value) { if (value != null && !type.IsAssignableFrom(value.GetType())) { throw new InvalidCastException($"Expected type {type}, but the object is of type {value.GetType()}"); } if (!KeyExists(key)) { var newnode = new KeyNode(indentation: 0, key, file: this); TopLevelNodes.Add(key, newnode); TopLevelLines.Add(newnode); } var node = TopLevelNodes[key]; NodeManager.SetNodeData(node, value, type, Style); if (AutoSave) { SaveAllData(); } }
/// <summary> Whether a top-level key exists in the file </summary> public bool KeyExists(string key) => TopLevelNodes.ContainsKey(key);