//Splits { <value>:<value>, <value>:<value> } and [ <value>, <value> ] into a list of <value> strings private static List <string> Split(string json) { var splitArray = JSONParser.splitArrayPool.Count > 0 ? JSONParser.splitArrayPool.Pop() : new List <string>(); splitArray.Clear(); if (json.Length == 2) { return(splitArray); } var parseDepth = 0; JSONParser.stringBuilder.Length = 0; for (var i = 1; i < json.Length - 1; i++) { switch (json[i]) { case '[': case '{': parseDepth++; break; case ']': case '}': parseDepth--; break; case '"': i = JSONParser.AppendUntilStringEnd(true, i, json); continue; case ',': case ':': if (parseDepth == 0) { splitArray.Add(JSONParser.stringBuilder.ToString()); JSONParser.stringBuilder.Length = 0; continue; } break; } JSONParser.stringBuilder.Append(json[i]); } splitArray.Add(JSONParser.stringBuilder.ToString()); return(splitArray); }
public static object FromJson(string json, System.Type type) { // Initialize, if needed, the ThreadStatic variables if (JSONParser.propertyInfoCache == null) { JSONParser.propertyInfoCache = new Dictionary <Type, Dictionary <string, PropertyInfo> >(); } if (JSONParser.fieldInfoCache == null) { JSONParser.fieldInfoCache = new Dictionary <Type, Dictionary <string, FieldInfo> >(); } if (JSONParser.stringBuilder == null) { JSONParser.stringBuilder = new StringBuilder(); } if (JSONParser.splitArrayPool == null) { JSONParser.splitArrayPool = new Stack <List <string> >(); } //Remove all whitespace not within strings to make parsing simpler JSONParser.stringBuilder.Length = 0; for (var i = 0; i < json.Length; i++) { var c = json[i]; if (c == '"') { i = JSONParser.AppendUntilStringEnd(true, i, json); continue; } if (char.IsWhiteSpace(c)) { continue; } JSONParser.stringBuilder.Append(c); } //Parse the thing! return(JSONParser.ParseValue(type, JSONParser.stringBuilder.ToString())); }