static public JSONNode Parse(ComplexTree <object> t) { JSONNode rtn = null; int ni = t.StartPoint + 1; //nowindex int ti = 0; //tree index int nei = 0; int nextSeparator; if (s[t.StartPoint] == '{') { rtn = new JSONObject(); if (t.separator.Count == 1) { nextSeparator = t.separator.Dequeue(); if (s[nextSeparator] == '}') { return(rtn); } else { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return(null); } } while (t.separator.Count > 0) { nextSeparator = t.separator.Dequeue(); while (s[ni] <= ' ') { ni++; //find next non whitespace } if (s[nextSeparator] != ':') { threadException = new JSONSyntaxErrorCollonNotExist(nextSeparator); return(null); } nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } string keystr; if (s[nei] == '"' && s[ni] == '"') { keystr = s.Substring(ni + 1, nei - ni - 1); } else { threadException = new JSONSyntaxErrorKeyNotExist(ni); return(null); } ni = (nextSeparator + 1); nextSeparator = t.separator.Dequeue(); nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } while (s[ni] <= ' ') { ni++; //find next non whitespace } char k = s[ni]; if (k != '{' && k != '[') { try { rtn[keystr] = _parse[k](ni, nei); } catch { threadException = new JSONSyntaxErrorValueNotExist(ni); return(null); } } else { if (t[ti].EndPoint != nei) { threadException = new JSONSyntaxErrorCommaNotExist(t[ti].EndPoint); return(null); } rtn[keystr] = Parse(t[ti]); ti++; } if (s[nextSeparator] != ',' && s[nextSeparator] != '}') { threadException = new JSONSyntaxErrorCommaNotExist(); return(null); } ni = nextSeparator + 1; } } else// if (_s[si] == '[') { rtn = new JSONArray(); if (t.separator.Count == 1) { nextSeparator = t.separator.Dequeue(); if (s[nextSeparator] == ']') { return(rtn); } else { threadException = new JSONSyntaxErrorCommaNotExist(); return(null); } } while (0 < t.separator.Count) { while (s[ni] <= ' ') { ni++; //find next non whitespace } nextSeparator = t.separator.Dequeue(); nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } char k = s[ni]; if (k != '{' && k != '[') { try { rtn.Add(_parse[k](ni, nei)); } catch { threadException = new JSONSyntaxErrorValueNotExist(ni); return(null); } } else { if (t[ti].EndPoint != nei) { threadException = new JSONSyntaxErrorCommaNotExist(t[ti].EndPoint); return(null); } rtn.Add(Parse(t[ti])); ti++; } if (s[nextSeparator] != ',' && s[nextSeparator] != ']') { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return(null); } ni = nextSeparator + 1; } } return(rtn); }//for single thread
}//for single thread static public void ParseThread(ComplexTree <object> t) { //Console.WriteLine("ParseStart " + t.Index); foreach (ComplexTree <object> c in t) { if (c.EndPoint - c.StartPoint > ComplexityHighThreshold && t.Count > 1) { //Console.WriteLine(c.EndPoint - c.StartPoint); c.task = new Thread(() => ParseThread(c)); c.task.Start(); } /* * else if (c.EndPoint - c.Index > ComplexityLowThreshold && t.Count > 1) * { * c.task = new Thread(() => Parse(c)); * c.task.Start(); * }*/ } JSONNode rtn = null; int ni = t.StartPoint + 1; //nowindex int ti = 0; //tree index int nei = 0; int nextSeparator; if (s[t.StartPoint] == '{') { rtn = new JSONObject(); t.node = rtn; if (t.separator.Count == 1) { nextSeparator = t.separator.Dequeue(); if (s[nextSeparator] == '}') { return; } else { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return; } } while (0 < t.separator.Count) { nextSeparator = t.separator.Dequeue(); while (s[ni] <= ' ') { ni++; //find next non whitespace } if (s[nextSeparator] != ':') { threadException = new JSONSyntaxErrorCollonNotExist(nextSeparator); return; } nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } string keystr; if (s[nei] == '"' && s[ni] == '"') { keystr = s.Substring(ni + 1, nei - ni - 1); } else { threadException = new JSONSyntaxErrorKeyNotExist(ni); return; } ni = (nextSeparator + 1); while (s[ni] <= ' ') { ni++; //find next non whitespace } nextSeparator = t.separator.Dequeue(); nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } char k = s[ni]; if (k != '{' && k != '[') { try { rtn[keystr] = _parse[k](ni, nei); } catch { threadException = new JSONSyntaxErrorValueNotExist(ni); return; } } else { ComplexTree <object> child = t[ti]; if (child.EndPoint != nei) { threadException = new JSONSyntaxErrorCommaNotExist(t[ti].EndPoint); return; } if (child.task == null) { rtn[keystr] = Parse(child); } else { child.nodeIndex = rtn.Count; rtn[keystr] = JSONNull.NullStatic; } //rtn[keystr] = child.task == null ? Parse(child, _s) : child.task.Result; ti++; } if (s[nextSeparator] != ',' && s[nextSeparator] != '}') { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return; } ni = nextSeparator + 1; } } else// if (_s[si] == '[') { rtn = new JSONArray(); t.node = rtn; if (t.separator.Count == 1) { nextSeparator = t.separator.Dequeue(); if (s[nextSeparator] == ']') { return; } else { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return; } } while (0 < t.separator.Count) { nextSeparator = t.separator.Dequeue(); while (s[ni] <= ' ') { ni++; //find next non whitespace } nei = nextSeparator - 1; while (s[nei] <= ' ') { --nei; //find end non whitespace } char k = s[ni]; if (k != '{' && k != '[') { try { rtn.Add(_parse[k](ni, nei)); } catch { threadException = new JSONSyntaxErrorValueNotExist(ni); return; } } else { ComplexTree <object> child = t[ti]; if (child.EndPoint != nei) { threadException = new JSONSyntaxErrorCommaNotExist(t[ti].EndPoint); return; } if (child.task == null) { rtn.Add(Parse(child)); } else { child.nodeIndex = rtn.Count; rtn.Add(JSONNull.NullStatic); } //rtn.Add( child.task == null ? Parse(child, _s) :child.task.Result); ti++; } if (s[nextSeparator] != ',' && s[nextSeparator] != ']') { threadException = new JSONSyntaxErrorCommaNotExist(nextSeparator); return; } ni = nextSeparator + 1; } } if (threadException != null) { return; } foreach (ComplexTree <object> tree in t) { if (tree.task != null) { tree.task.Join(); rtn[tree.nodeIndex] = tree.node; } } //Console.WriteLine("ParseEnd " + t.Index); }//for multithread