/// <summary> /// Creates or sets all values along the path to Object, last object on the path is /// set to the provided value. /// </summary> public Ferr_JSONValue Set(string aPath, Ferr_JSONValue aVal) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; Ferr_JSONObject currObj = null; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject result = curr.GetImmidiateChild(words[i]); if (result == null) { Ferr_JSONObject[] list = null; if (curr.type == Ferr_JSONType.Object) { list = (Ferr_JSONObject[])curr.data; if (list == null) { list = new Ferr_JSONObject[1]; } else { Array.Resize <Ferr_JSONObject>(ref list, list.Length + 1); } curr.data = list; } else { curr.type = Ferr_JSONType.Object; curr.data = list = new Ferr_JSONObject[1]; } list[list.Length - 1] = new Ferr_JSONObject(words[i], new Ferr_JSONValue()); result = list[list.Length - 1]; } currObj = result; curr = result.val; } if (currObj != null) { currObj.val = aVal; return(currObj.val); } else { return(null); } }
/// <summary> /// Gets the value at the specific path. Paths are separated using '.'s Ex: /// "scene.player.name", returns null if anything along the path cannot be found, /// case sensitive. /// </summary> public Ferr_JSONValue Get(string aPath) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject child = curr.GetImmidiateChild(words[i]); if (child != null) { curr = child.val; } else { return(null); } } return(curr); }
/// <summary> /// Creates or sets all values along the path to Object, last object on the path is /// set to the provided value. /// </summary> public Ferr_JSONValue Set (string aPath, Ferr_JSONValue aVal) { string[] words = aPath.Split('.'); Ferr_JSONValue curr = this; Ferr_JSONObject currObj = null; for (int i = 0; i < words.Length; i++) { Ferr_JSONObject result = curr.GetImmidiateChild(words[i]); if (result == null) { Ferr_JSONObject[] list = null; if (curr.type == Ferr_JSONType.Object) { list = (Ferr_JSONObject[])curr.data; if (list == null) list = new Ferr_JSONObject[1]; else Array.Resize<Ferr_JSONObject>(ref list, list.Length + 1); curr.data = list; } else { curr.type = Ferr_JSONType.Object; curr.data = list = new Ferr_JSONObject[1]; } list[list.Length-1] = new Ferr_JSONObject(words[i], new Ferr_JSONValue()); result = list[list.Length-1]; } currObj = result; curr = result.val; } if (currObj != null){ currObj.val = aVal; return currObj.val; } else { return null; } }
/// <summary> /// Recursively converts a JSON string into a JSON data structure. Throws an exception /// if given an empty string. Unrecognized data is converted into a string. /// </summary> public void FromString(string aValue) { string text = aValue.Trim(); float fVal = 0; if (text.Length <= 0) throw new Exception("Bad JSON value: " + aValue); if (text[0] == '{') { type = Ferr_JSONType.Object; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONObject(items[i]); } data = result; } else if (text[0] == '[') { type = Ferr_JSONType.Array; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONValue(); result[i].FromString (items[i]); } data = result; } else if (text[0] == '\"') { type = Ferr_JSONType.String; text = text.Remove(0,1); text = text.Remove(text.Length-1,1); data = text; } else if (float.TryParse(text, out fVal) ) { type = Ferr_JSONType.Number; data = fVal; } else { string lower = text.ToLower(); if (lower == "false" || lower == "true") { type = Ferr_JSONType.Bool; data = bool.Parse(text); } else if (lower == "null") { type = Ferr_JSONType.Object; data = null; } else { type = Ferr_JSONType.String; data = text; } } }
/// <summary> /// Recursively converts a JSON string into a JSON data structure. Throws an exception /// if given an empty string. Unrecognized data is converted into a string. /// </summary> public void FromString(string aValue) { string text = aValue.Trim(); float fVal = 0; if (text.Length <= 0) { throw new Exception("Bad JSON value: " + aValue); } if (text[0] == '{') { type = Ferr_JSONType.Object; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONObject(items[i]); } data = result; } else if (text[0] == '[') { type = Ferr_JSONType.Array; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); string [] items = Ferr_JSON._Split(text, ',', true); Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length]; for (int i = 0; i < items.Length; i++) { result[i] = new Ferr_JSONValue(); result[i].FromString(items[i]); } data = result; } else if (text[0] == '\"') { type = Ferr_JSONType.String; text = text.Remove(0, 1); text = text.Remove(text.Length - 1, 1); data = text; } else if (float.TryParse(text, out fVal)) { type = Ferr_JSONType.Number; data = fVal; } else { string lower = text.ToLower(); if (lower == "false" || lower == "true") { type = Ferr_JSONType.Bool; data = bool.Parse(text); } else if (lower == "null") { type = Ferr_JSONType.Object; data = null; } else { type = Ferr_JSONType.String; data = text; } } }