//**************************************************************************************************** // //**************************************************************************************************** public override JSonEntry Resolve(string[] pathComps, int pathComp) { if (pathComps.Length <= 0) { return(null); } if (pathComp < 0) { return(null); } if (pathComp >= pathComps.Length) { return(this); } JSonEntry o = null; if (m_value.TryGetValue(pathComps[pathComp], out o)) { if (o != null) { return(o.Resolve(pathComps, pathComp + 1)); } } return(null); }
public bool pop(ref JSonEntry entry) { if (m_count > 0) { entry = m_stack[--m_count]; return(true); } return(false); }
public bool push(JSonEntry entry) { if (m_count < MAX_LVL) { m_stack[m_count++] = entry; return(true); } return(false); }
//**************************************************************************************************** // //**************************************************************************************************** public JSonEntry(string Name, JSonEntry Parent) { parent = Parent; name = (Name != null) ? Name : string.Empty; if (Parent != null) { Parent.Add(this); } }
public JSonObject(string Name, JSonEntry Parent) : base(Name, Parent) { }
public abstract void Add(JSonEntry entry);
//**************************************************************************************************** // //**************************************************************************************************** public bool Parse(string json) { if (string.IsNullOrEmpty(json)) { return(false); } ControlStack ctrl = new ControlStack(); JSonEntry cur = null; string name = null; m_root = null; for (int c = 0; c < json.Length; ++c) { //-------------------------------------------------------------------------------------------- // register expression //-------------------------------------------------------------------------------------------- if (char.IsWhiteSpace(json[c]) == false) { if (Semantics.hasProps(json[c], Semantics.PROP.ALL) == false) { if (ctrl.expr.start == -1) { ctrl.expr.end = ctrl.expr.start = c; } else { ctrl.expr.end = c; } if (json[c] == QUOTE) { ++ctrl.expr.quotes; continue; } } } else { continue; } //-------------------------------------------------------------------------------------------- // register name or value //-------------------------------------------------------------------------------------------- if (Semantics.hasProps(json[c], Semantics.PROP.CTRL) == true) { if ((ctrl.expr.quotes & 1) == 0) { bool isName = (json[c] == COLON); bool isValue = (isName == false); bool valid = ctrl.expr.valid; if ((isName) && (valid == false)) { return(false); } if ((isName) && (cur is JSonObject) == false) { return(false); } if (valid) { string expr = json.Substring(ctrl.expr.start, ctrl.expr.end - ctrl.expr.start + 1).TrimEnd(' ', QUOTE, ' ').TrimStart(' ', QUOTE, ' '); if (isName) { name = expr; } else if (isValue) { new JSonValue(name, cur).value = expr; name = null; } } ctrl.expr.Reset(); if (Semantics.hasProps(json[c], Semantics.PROP.GRP) == false) { continue; } } } //-------------------------------------------------------------------------------------------- // register object or array //-------------------------------------------------------------------------------------------- if (ctrl.expr.quotes == 0) { if (json[c] == BGN_OBJ) { if ((cur == null) || ctrl.push(cur)) { cur = new JSonObject(name, cur); if (m_root == null) { m_root = cur; } name = null; ctrl.expr.Reset(); continue; } else { return(false); } } if (json[c] == BGN_ARR) { if ((cur == null) || ctrl.push(cur)) { cur = new JSonArray(name, cur); if (m_root == null) { m_root = cur; } name = null; ctrl.expr.Reset(); continue; } else { return(false); } } if (json[c] == END_OBJ) { if (cur == m_root) { break; } if ((cur is JSonObject) && ctrl.pop(ref cur)) { ctrl.expr.Reset(); continue; } else { return(false); } } if (json[c] == END_ARR) { if (cur == m_root) { break; } if ((cur is JSonArray) && ctrl.pop(ref cur)) { ctrl.expr.Reset(); continue; } else { return(false); } } } } return((m_root != null) && (ctrl.empty)); }
public override void Add(JSonEntry entry) { Debug.Assert(false, "JSonValue: cannot be parent of another json elt"); }
//**************************************************************************************************** // //**************************************************************************************************** public JSonValue(string Name, JSonEntry Parent) : base(Name, Parent) { }
public override void Add(JSonEntry entry) { m_value.Add(entry); }
public JSonArray(string Name, JSonEntry Parent) : base(Name, Parent) { }
//**************************************************************************************************** // //**************************************************************************************************** private void OnItemsQueryResponse(HTTPReq query, params object[] paramsList) { if (query.result != QUERY_RESULT.SUCCESS) { End(); return; } JSon json = new JSon(query.response); if (json["error"] != null) { End(); return; } if ((json.root is JSonArray) == false) { End(); return; } for (int entry = 0; entry < json.count; ++entry) { JSonEntry o = json[entry]; if ((o is JSonObject) == false) { continue; } long ID = -1L; if (long.TryParse(o["id"].value, out ID) == false) { continue; } long siteID = -1L; if (long.TryParse(o["site_id"].value, out siteID) == false) { continue; } float LAT = 0.0f; if (float.TryParse(o["lat"].value, out LAT) == false) { continue; } float LNG = 0.0f; if (float.TryParse(o["lng"].value, out LNG) == false) { continue; } float ALT = 0.0f; if (float.TryParse(o["alt"].value, out ALT) == false) { continue; } Item item = new Item(); item.id = ID; item.parentID = siteID; item.name = o["name"].value; item.m_desc = o["dsc"].value; item.m_pic = o["pic"].value; item.m_coord.latitude.FromAngle(LAT, GPS.UNIT.DD); item.m_coord.longitude.FromAngle(LNG, GPS.UNIT.DD); item.m_coord.altitude = ALT; db.items.Add(item); if (item.m_parent == null) { item.SetParent(db.root); } } End(); }