public static string ToJson <T>(T[] array, bool prettyPrint) { WrapperArray <T> wrapper = new WrapperArray <T>(); wrapper.Items = array; return(JsonUtility.ToJson(wrapper, prettyPrint)); }
public static T[] getJsonArray <T>(string json) { string newJson = "{ \"array\": " + json + "}"; WrapperArray <T> wrapper = JsonUtility.FromJson <WrapperArray <T> >(newJson); return(wrapper.array); }
private void BuildArray(WrapperArray mainNode, string tabs) { if (mainNode.WrapperName != string.Empty) { this.Results += $"{tabs}\"{mainNode.WrapperName}\": [\n"; } for (int index = 0; index < mainNode.Value.Count; index++) { var node = mainNode.Value[index]; switch (node) { case WrapperObject wrapperObject: wrapperObject.WrapperName = string.Empty; this.Results += $"{tabs + "\t"}{{\n"; BuildObject(wrapperObject, tabs + "\t"); if (index != mainNode.Value.Count - 1) { this.Results += $"{tabs + "\t"}}},\n"; } else { this.Results += $"{tabs + "\t"}}}\n"; } break; default: throw new Exception("This type is invalid for build the file."); } } if (mainNode.WrapperName != string.Empty) { this.Results += $"{tabs}]"; } }
public static string ToJson <T>(T[] array) { WrapperArray <T> wrapper = new WrapperArray <T>(); wrapper.Items = array; return(JsonUtility.ToJson(wrapper)); }
private int ParseValue(int index, string valueName, WrapperObject parentNode) { WrapperType actualValue; if (this.TokenList[index].Contains(".")) { var proposedDouble = this.TokenList[index].Split("."); if (int.TryParse(proposedDouble[0], out _) && int.TryParse(proposedDouble[1], out _)) { actualValue = new WrapperDouble(valueName, double.Parse(this.TokenList[index++])); } else { actualValue = new WrapperString(valueName, this.TokenList[index++]); } } else if (int.TryParse(this.TokenList[index], out _)) { actualValue = new WrapperInt(valueName, int.Parse(this.TokenList[index++])); } else if (this.TokenList[index].ToLower() == "true" || this.TokenList[index].ToLower() == "false") { bool boolVal = bool.Parse(this.TokenList[index++]); actualValue = new WrapperBool(valueName, boolVal); } else if (this.TokenList[index] == "<") { if (valueName.ToLower().Contains("array")) { actualValue = new WrapperArray(valueName, null); index = ParseArray(index, actualValue as WrapperArray); } else { actualValue = new WrapperObject(valueName, null); index = ParseObject(index, actualValue as WrapperObject); } } else { actualValue = new WrapperString(valueName, this.TokenList[index++]); } index = RulesUtility.ValidateToken(this.TokenList[index], "<", "Invalid Token. Need \'<\' for name of value", index); if (valueName.ToLower().Contains("array")) { index = RulesUtility.ValidateToken(this.TokenList[index], $"/{valueName.ToLower()}", $"Invalid Token. Need closing name {valueName} for name of value", index); } else { index = RulesUtility.ValidateToken(this.TokenList[index], $"/{valueName}", $"Invalid Token. Need closing name {valueName} for name of value", index); } index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need \'>\' for name of value", index); parentNode.Value.Add(actualValue); return(index); }
private int ParseValue(int index, string valueName, WrapperObject parentNode) { WrapperType actualValue = null; if (this.TokenList[index].Contains("\"")) { index = RulesUtility.ValidateToken(this.TokenList[index], "\"", "Invalid Token. Need first double quote for string value", index); actualValue = new WrapperString(valueName, this.TokenList[index++]); index = RulesUtility.ValidateToken(this.TokenList[index], "\"", "Invalid Token. Need first double quote for string value", index); } else if (this.TokenList[index].Contains(".")) { var proposedDouble = this.TokenList[index].Split("."); if (int.TryParse(proposedDouble[0], out _) && int.TryParse(proposedDouble[1], out _)) { actualValue = new WrapperDouble(valueName, double.Parse(this.TokenList[index++])); } else { throw new Exception("This is an invlid Double Value"); } } else if (int.TryParse(this.TokenList[index], out _)) { actualValue = new WrapperInt(valueName, int.Parse(this.TokenList[index++])); } else if (this.TokenList[index] == "true" || this.TokenList[index] == "false") { bool boolVal = bool.Parse(this.TokenList[index++]); actualValue = new WrapperBool(valueName, boolVal); } else if (this.TokenList[index] == "{") { index++; actualValue = new WrapperObject(valueName, null); index = ParseObject(index, actualValue as WrapperObject); index++; } else if (this.TokenList[index] == "[") { index++; actualValue = new WrapperArray(valueName, null); index = ParseArray(index, actualValue as WrapperArray); index++; } parentNode.Value.Add(actualValue); return(index); }
private void BuildArray(WrapperArray mainNode, string tabs) { this.Results += $"{tabs}<{mainNode.WrapperName}Array>\n"; foreach (var node in mainNode.Value) { switch (node) { case WrapperObject wrapperObject: wrapperObject.WrapperName = mainNode.WrapperName; BuildObject(wrapperObject, tabs + " "); break; throw new Exception("This type is invalid for build the file."); } } this.Results += $"{tabs}</{mainNode.WrapperName}Array>\n"; }
private int ParseArray(int index, WrapperArray parentNode) { parentNode.Value = new List <WrapperObject>(); int id = 0; while (this.TokenList[index] != "]") { index = RulesUtility.ValidateToken(this.TokenList[index], "{", "Need object for arrays", index); var actualValue = new WrapperObject(string.Format("ID-", id++), null); index = ParseObject(index, actualValue); index++; bool hasNoId = true; foreach (var new_id in actualValue.Value) { if (new_id.WrapperName.ToLower() == "id") { var tempNode = new_id as WrapperInt; actualValue.WrapperName = tempNode.WrapperName + "-" + tempNode.Value; hasNoId = false; break; } } if (hasNoId) { throw new Exception("Array entry has no valid id. Please place id value in Json."); } parentNode.Value.Add(actualValue); if (this.TokenList[index] == ",") { index++; } else if (this.TokenList[index] == "]") { // will cancel out } else { throw new Exception("Invalid Token. Need last double quote for name of value"); } } return(index); }
public override void ParseFile() { Log.Info("Starting to Parse XML file."); WrapperType mainNode; int index = 0; index = VerifyHeader(index); if (this.TokenList[index] == "<") { index++; if (this.TokenList[index].Contains("Array")) { mainNode = new WrapperArray(this.TokenList[index++], null); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need first double quote for name of value", index); index = ParseArray(index, mainNode as WrapperArray); index = RulesUtility.ValidateToken(this.TokenList[index], "<", "Invalid Token. Need \'<\' for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], $"/{mainNode.WrapperName + "Array"}", $"Invalid Token. Need closing name {mainNode.WrapperName} for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need \'>\' for name of value", index); } else { mainNode = new WrapperObject(this.TokenList[index++], null); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need first double quote for name of value", index); index = ParseObject(index, mainNode as WrapperObject); index = RulesUtility.ValidateToken(this.TokenList[index], "<", "Invalid Token. Need \'<\' for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], $"/{mainNode.WrapperName}", $"Invalid Token. Need closing name {mainNode.WrapperName} for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need \'>\' for name of value", index); } } else { throw new Exception("Invalid start to XML Parsing."); } this.Structure.Add(mainNode); Log.Success("XML Parse Successfully Completed."); }
private int ParseArray(int index, WrapperArray parentNode) { parentNode.Value = new List <WrapperObject>(); var testArray = parentNode.WrapperName.Split("Array"); string jsonName = string.Empty; for (int testIndex = 0; testIndex < testArray.Length - 1; testIndex++) { if (testArray[testIndex] == "") { jsonName += "Array"; } jsonName += testArray[testIndex]; } int id = 0; while (index < this.TokenList.Count) { index = RulesUtility.ValidateToken(this.TokenList[index], "<", "Invalid Token. Need \'<\' for name of value", index); if (this.TokenList[index] == $"/{parentNode.WrapperName}") { parentNode.WrapperName = jsonName; break; } index = RulesUtility.ValidateToken(this.TokenList[index], jsonName, $"Invalid Token. Need {jsonName} for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need \'>\' for name of value", index); WrapperObject childNode = new WrapperObject(jsonName, null); index = ParseObject(index, childNode); childNode.Value.Add(new WrapperInt("id", id)); childNode.WrapperName = "ID-" + id++; parentNode.Value.Add(childNode); index = RulesUtility.ValidateToken(this.TokenList[index], "<", "Invalid Token. Need \'<\' for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], $"/{jsonName}", "Invalid Token. Need \'<\' for name of value", index); index = RulesUtility.ValidateToken(this.TokenList[index], ">", "Invalid Token. Need \'>\' for name of value", index); } return(index - 1); }
public override void ParseFile() { Log.Info("Staring to Parse Json file."); WrapperType mainNode; int index = 0; if (this.TokenList[index] == "{") { mainNode = new WrapperObject(this.Filename.Split('.')[0], null); _ = ParseObject(++index, mainNode as WrapperObject); } else if (this.TokenList[index] == "[") { mainNode = new WrapperArray(this.Filename.Split('.')[0], null); _ = ParseArray(++index, mainNode as WrapperArray); } else { throw new Exception("Invalid start to JSON Parsing."); } this.Structure.Add(mainNode); Log.Success("Parsing Json is Completed."); }
static extern IntPtr UserTrackerFrameRef_destroyUsersArray(WrapperArray array);
private static extern IntPtr HandTrackerFrameRef_destroyHandsArray(WrapperArray array);
public static T[] FromJson <T>(string json) { WrapperArray <T> wrapper = JsonUtility.FromJson <WrapperArray <T> >(json); return(wrapper.Items); }
static extern IntPtr HandTrackerFrameRef_destroyGesturesArray(WrapperArray array);