private void freestructure(ref TJSONRECORD p) { switch (p.recordtype) { case TJSONRECORDTYPE.JSON_STRUCT: for (int i = p.membercount - 1; i >= 0; i += -1) { freestructure(ref p.members[i]); } p.members = new TJSONRECORD[1]; break; case TJSONRECORDTYPE.JSON_ARRAY: for (int i = p.itemcount - 1; i >= 0; i += -1) { freestructure(ref p.items[i]); } p.items = new TJSONRECORD[1]; break; } }
private void DumpStructureRec(ref TJSONRECORD p, ref int deep) { string line = null; string indent = null; int i = 0; line = ""; indent = ""; for (i = 0; i <= deep * 2; i++) { indent = indent + " "; } line = indent + p.name + ":"; switch (p.recordtype) { case TJSONRECORDTYPE.JSON_STRING: line = line + " str=" + p.svalue; Console.WriteLine(line); break; case TJSONRECORDTYPE.JSON_INTEGER: line = line + " int =" + p.ivalue.ToString(); Console.WriteLine(line); break; case TJSONRECORDTYPE.JSON_BOOLEAN: if (p.bvalue) line = line + " bool = TRUE"; else line = line + " bool = FALSE"; Console.WriteLine(line); break; case TJSONRECORDTYPE.JSON_STRUCT: Console.WriteLine(line + " struct"); for (i = 0; i <= p.membercount - 1; i++) { DumpStructureRec(ref p.members[i], ref deep); } break; case TJSONRECORDTYPE.JSON_ARRAY: Console.WriteLine(line + " array"); for (i = 0; i <= p.itemcount - 1; i++) { DumpStructureRec(ref p.items[i], ref deep); } break; } }
private void add2ArrayRecord(ref TJSONRECORD container, ref TJSONRECORD element) { if (container.recordtype != TJSONRECORDTYPE.JSON_ARRAY) throw new System.Exception("container is not an array type"); if ((container.itemcount >= container.itemAllocated)) { Array.Resize(ref container.items, container.itemAllocated + JSONGRANULARITY); container.itemAllocated = container.itemAllocated + JSONGRANULARITY; } container.items[container.itemcount] = element; container.itemcount = container.itemcount + 1; }
private void add2StructRecord(ref TJSONRECORD container, ref TJSONRECORD element) { if (container.recordtype != TJSONRECORDTYPE.JSON_STRUCT) throw new System.Exception("container is not a struct type"); if ((container.membercount >= container.memberAllocated)) { Array.Resize(ref container.members, container.memberAllocated + JSONGRANULARITY); container.memberAllocated = container.memberAllocated + JSONGRANULARITY; } container.members[container.membercount] = element; container.membercount = container.membercount + 1; }
public TJsonParser(string jsonData) { const string httpheader = "HTTP/1.1 "; string errmsg = null; int p1 = 0; int p2 = 0; const string CR = "\r\n"; if (jsonData.Substring(0, httpheader.Length) != httpheader) { errmsg = "data should start with " + httpheader; throw new System.Exception(errmsg); } p1 = jsonData.IndexOf(" ", httpheader.Length - 1); p2 = jsonData.IndexOf(" ", p1 + 1); httpcode = Convert.ToInt32(jsonData.Substring(p1, p2 - p1 + 1)); if (httpcode != 200) return; p1 = jsonData.IndexOf(CR + CR + "{"); //json data is a structure if (p1 < 0) p1 = jsonData.IndexOf(CR + CR + "["); // json data is an array if (p1 < 0) { errmsg = "data does not contain JSON data"; throw new System.Exception(errmsg); } jsonData = jsonData.Substring(p1 + 4, jsonData.Length - p1 - 4); data = (TJSONRECORD)Parse(jsonData); }