/// <summary> /// 解析文本读取器中的数据,用文本读取器中的数据构建对应的JObject对象,JObject对象内部最大嵌套层数为100 /// </summary> /// <param name="reader">文本读取器</param> /// <param name="max_nest">对象内部最大嵌套层数</param> /// <returns>输出文本读取器中的数据构建的JObject对象</returns> public static JObject Parse(TextReader reader, int max_nest = 100) { if (max_nest < 0) { throw new FormatException(); } SkipSpace(reader); char firstChar = (char)reader.Peek(); if (firstChar == '\"' || firstChar == '\'') { return(JString.Parse(reader)); } if (firstChar == '[') { return(JArray.Parse(reader, max_nest)); } if ((firstChar >= '0' && firstChar <= '9') || firstChar == '-') { return(JNumber.Parse(reader)); } if (firstChar == 't' || firstChar == 'f') { return(JBoolean.Parse(reader)); } if (firstChar == 'n') { return(ParseNull(reader)); } if (reader.Read() != '{') { throw new FormatException(); } SkipSpace(reader); JObject obj = new JObject(); while (reader.Peek() != '}') { if (reader.Peek() == ',') { reader.Read(); } SkipSpace(reader); string name = JString.Parse(reader).Value; SkipSpace(reader); if (reader.Read() != ':') { throw new FormatException(); } JObject value = Parse(reader, max_nest - 1); obj.properties.Add(name, value); SkipSpace(reader); } reader.Read(); return(obj); }
public static JObject Parse(TextReader reader, int max_nest = 100) { if (max_nest < 0) { throw new FormatException(); } SkipSpace(reader); char firstChar = (char)reader.Peek(); if (firstChar == LITERAL_FALSE[0]) { return(JBoolean.ParseFalse(reader)); } if (firstChar == LITERAL_NULL[0]) { return(ParseNull(reader)); } if (firstChar == LITERAL_TRUE[0]) { return(JBoolean.ParseTrue(reader)); } if (firstChar == BEGIN_OBJECT) { return(ParseObject(reader, max_nest)); } if (firstChar == BEGIN_ARRAY) { return(JArray.Parse(reader, max_nest)); } if ((firstChar >= '0' && firstChar <= '9') || firstChar == '-') { return(JNumber.Parse(reader)); } if (firstChar == QUOTATION_MARK) { return(JString.Parse(reader)); } throw new FormatException(); }