public object ParseSubtree() { IList <object> l = null; IDictionary <string, object> d = null; string rv; object result = null; switch (_state) { case -2: return(null); case -1: if (!Read()) { _pc.Expecting(); } return(ParseSubtree()); case 0: result = Value; break; case 1: // key rv = Value as string; if (!Read()) { _pc.Expecting(); } result = new KeyValuePair <string, object>(rv, ParseSubtree()); break; case 2: // begin array l = new JsonArray(); while (Read() && 3 != _state) { l.Add(ParseSubtree()); } result = l; break; case 3: // end array result = null; break; case 4: // begin object d = new JsonObject(); while (Read() && 5 != _state) // not end object { KeyValuePair <string, object> kvp = (KeyValuePair <string, object>)ParseSubtree(); d.Add(kvp.Key, kvp.Value); } result = d; break; case 5: result = null; break; default: throw new InvalidProgramException("Unhandled state"); } return(result); }
private static object ParseValue(StringReader stringReader) { while (stringReader.Peek() == ' ' || stringReader.Peek() == '\r' || stringReader.Peek() == '\n') { stringReader.Read(); } if (stringReader.Peek() == '\"') { stringReader.Read(); StringBuilder stringBuilder = new StringBuilder(); while (stringReader.Peek() != -1) { if (stringReader.Peek() == '\\') { stringBuilder.Append((char)stringReader.Read()); stringBuilder.Append((char)stringReader.Read()); } else if (stringReader.Peek() == '\"') { string value = stringBuilder.ToString(); while (stringReader.Peek() != ',' && stringReader.Peek() != '}' && stringReader.Peek() != ']') { stringReader.Read(); } if (stringReader.Peek() == ',') { stringReader.Read(); } return(value); } else { stringBuilder.Append((char)stringReader.Read()); } } return(stringBuilder.ToString()); } else if (stringReader.Peek() == '{') { JsonObject jsonObject = ParseObject(stringReader); while (stringReader.Peek() != -1 && stringReader.Read() != ',') { ; } return(jsonObject); } else if (stringReader.Peek() == '[') { JsonArray jsonArray = ParseArray(stringReader); while (stringReader.Peek() != -1 && stringReader.Read() != ',') { ; } return(jsonArray); } else { StringBuilder stringBuilder = new StringBuilder(); while (stringReader.Peek() != -1) { if (stringReader.Peek() == '\\') { stringBuilder.Append((char)stringReader.Read()); stringBuilder.Append((char)stringReader.Read()); } else if (stringReader.Peek() == ',') { string value = stringBuilder.ToString(); stringReader.Read(); return(ToValue(value)); } else if (stringReader.Peek() == '}' || stringReader.Peek() == ']') { return(ToValue(stringBuilder.ToString())); } else { stringBuilder.Append((char)stringReader.Read()); } } return(stringBuilder.ToString()); } }