private bool isNull(int c, ref JsonParseError jsonError) { if (c == 'n') { if (read() == 'u') { if (read() == 'l') { if (read() == 'l') { return(true); } jsonError.error = ParseError.IllegalValue; } else { jsonError.error = ParseError.IllegalValue; } } else { jsonError.error = ParseError.IllegalValue; } } return(false); }
/// <summary> /// object -> attributes /// </summary> /// <param name="tokens"></param> /// <param name="index"></param> /// <returns></returns> private static List <JsonInfo> GetAttribute(List <Token> tokens, int index, ref JsonParseError jsonError) { List <JsonInfo> list = new List <JsonInfo>(); int end = GetValueEndIndex(tokens, index); for (int i = index + 1; i < end; i++) { if (tokens[i].getType() == TokenType.COLON) { i++; switch (tokens[i].getType()) { case TokenType.START_ARRAY: list.Add(JArray(tokens, i, ref jsonError)); i = GetValueEndIndex(tokens, i); break; case TokenType.START_OBJ: list.Add(JObject(tokens, i, ref jsonError)); i = GetValueEndIndex(tokens, i); break; case TokenType.BOOLEAN: case TokenType.NUMBER: case TokenType.NULL: case TokenType.STRING: list.Add(JValue(tokens, i, ref jsonError)); break; } } } return(list); }
private void Awake() { var json = FileOperate.ReadFileToString("arpg/Code/Json/demo/txt/equip.json"); JsonParseError jsonError = new JsonParseError(); JsonDocument document = JsonDocument.fromJson(json, ref jsonError); if (jsonError.error == ParseError.NoError) { if (document.isObject()) { var o = document.toObject(); foreach (var item in o.keys()) { var v = o.value(item); if (v.isArray()) { var a = v.toArray(); foreach (var t in a.all()) { if (t.isObject()) { Debug.Log(t.toObject().value("name").toString()); } } } } } } }
private bool isFalse(int c, ref JsonParseError jsonError) { if (c == 'f') { if (read() == 'a') { if (read() == 'l') { if (read() == 's') { if (read() == 'e') { return(true); } jsonError.error = ParseError.IllegalValue; } else { jsonError.error = ParseError.IllegalValue; } } else { jsonError.error = ParseError.IllegalValue; } } else { jsonError.error = ParseError.IllegalValue; } } return(false); }
private Token Inspect(ref JsonParseError jsonError) { c = '?'; while (isSpace(c = read())) { ; } if (isNull(c, ref jsonError)) { return(new Token(TokenType.NULL, null)); } else if (c == ',') { return(new Token(TokenType.COMMA, ",")); } else if (c == ':') { return(new Token(TokenType.COLON, ":")); } else if (c == '{') { return(new Token(TokenType.START_OBJ, "{")); } else if (c == '[') { return(new Token(TokenType.START_ARRAY, "[")); } else if (c == ']') { return(new Token(TokenType.END_ARRAY, "]")); } else if (c == '}') { return(new Token(TokenType.END_OBJ, "}")); } else if (isTrue(c, ref jsonError)) { return(new Token(TokenType.BOOLEAN, "true")); } else if (isFalse(c, ref jsonError)) { return(new Token(TokenType.BOOLEAN, "false")); } else if (c == '"') { return(readString(ref jsonError)); } else if (isNum(c)) { unread(); return(readNum(ref jsonError)); } else if (c == -1) { return(new Token(TokenType.END_DOC, "EOF")); } return(null); }
private static JsonInfo JValue(List <Token> tokens, int index, ref JsonParseError jsonError) { JsonInfo info = new JsonInfo(); info.type = GetType(tokens, index); info.key = GetKey(tokens, index, ref jsonError); info.value = tokens[index].getValue(); return(info); }
/// <summary> /// tokens[index].getType() == TokenType.START_OBJ /// </summary> /// <param name="tokens"></param> /// <param name="index"></param> /// <returns></returns> private static JsonInfo JObject(List <Token> tokens, int index, ref JsonParseError jsonError) { JsonInfo info = GetJsonInfo(tokens, index, ref jsonError); if (info == null) { return(null); } info.type = ValueType.Object; info.list = GetAttribute(tokens, index, ref jsonError); return(info); }
private static JsonInfo GetJsonInfo(List <Token> tokens, int index, ref JsonParseError jsonError) { JsonInfo info = new JsonInfo(); info.key = GetKey(tokens, index, ref jsonError); int end = GetValueEndIndex(tokens, index); if (end == -1) { return(null); } info.value = GetValue(tokens, index, end); return(info); }
private Token readNum(ref JsonParseError jsonError) { StringBuilder sb = new StringBuilder(); int c = read(); if (c == '-') { //- sb.Append((char)c); c = read(); if (c == '0') { //-0 sb.Append((char)c); numAppend(sb, ref jsonError); } else if (isDigitOne2Nine(c)) { //-digit1-9 do { sb.Append((char)c); c = read(); } while (isDigit(c)); unread(); numAppend(sb, ref jsonError); } else { jsonError.error = ParseError.IllegalNumber; return(null); } } else if (c == '0') { //0 sb.Append((char)c); numAppend(sb, ref jsonError); } else if (isDigitOne2Nine(c)) { //digit1-9 do { sb.Append((char)c); c = read(); } while (isDigit(c)); unread(); numAppend(sb, ref jsonError); } return(new Token(TokenType.NUMBER, sb.ToString())); //the value of 0 is null }
private bool isEscape(ref JsonParseError jsonError) { if (c == '\\') { c = read(); if (c == '"' || c == '\\' || c == '/' || c == 'b' || c == 'f' || c == 'n' || c == 't' || c == 'r' || c == 'u') { return(true); } else { jsonError.error = ParseError.IllegalEscapeSequence; } } return(false); }
private Token readString(ref JsonParseError jsonError) { StringBuilder sb = new StringBuilder(); while (true) { c = read(); if (isEscape(ref jsonError)) { //判断是否为\", \\, \/, \b, \f, \n, \t, \r. if (c == 'u') { sb.Append('\\' + (char)c); for (int i = 0; i < 4; i++) { c = read(); if (isHex(c)) { sb.Append((char)c); } else { jsonError.error = ParseError.IllegalNumber; return(null); } } } else { sb.Append("\\" + (char)c); } } else if (c == '"') { return(new Token(TokenType.STRING, sb.ToString())); } else if (c == '\r' || c == '\n') { jsonError.error = ParseError.UnterminatedString; return(null); } else { sb.Append((char)c); } } }
static void Main(string[] args) { var json = Read("F://UnityPro/Practice/demo/Assets/equip.json"); JsonParseError error = new JsonParseError(); JsonDocument document = JsonDocument.fromJson(json, ref error); if (error.error == ParseError.NoError) { if (document.isObject()) { IsObject(document.toObject()); } } else { Console.WriteLine(error.error.ToString()); } }
public void JsonParserFailsToParseDoubleDoubleQuotes() { string createTableJsonResponse = GetReportTextContent(); JsonItem job; using (var jsonParser = new JsonParser(createTableJsonResponse)) { job = jsonParser.ParseNext(); if (job.IsError) { JsonParseError error = (JsonParseError)job; Assert.Fail(error.ToString()); } } string queryText; Assert.IsTrue(job.GetProperty("execute").TryGetValue(out queryText), "unable to retrieve the query text string"); Assert.AreEqual("\"select * from hivesampletable limit10\"", queryText); }
public static JsonDocument fromJson(string json, ref JsonParseError jsonError) { if (json == "") { jsonError.error = ParseError.GarbageAtEnd; return(null); } JsonDocument document = new JsonDocument(); Tokenizer tokenizer = new Tokenizer(json); tokenizer.Tokenize(ref jsonError); if (jsonError.error != ParseError.NoError) { return(null); } //检查右括号结尾是否错误 if (-1 == GetValueEndIndex(tokenizer.Tokens, 0)) { jsonError.error = ParseError.UnterminatedObject; return(null); } //检查逗号 if (isSeparate(tokenizer.Tokens, ref jsonError)) { jsonError.error = ParseError.MissingNameSeparator; return(null); } if (tokenizer.Tokens[0].getType() == TokenType.START_OBJ) { document.jsonInfo = JObject(tokenizer.Tokens, 0, ref jsonError); } else if (tokenizer.Tokens[0].getType() == TokenType.START_ARRAY) { document.jsonInfo = JArray(tokenizer.Tokens, 0, ref jsonError); } return(document); }
/// <summary> /// tokens[index].getType() == TokenType.START_ARRAY /// </summary> /// <param name="tokens"></param> /// <param name="index"></param> /// <returns></returns> private static JsonInfo JArray(List <Token> tokens, int index, ref JsonParseError jsonError) { JsonInfo info = GetJsonInfo(tokens, index, ref jsonError); if (info == null) { return(null); } info.type = ValueType.Array; info.list = new List <JsonInfo>(); int end = GetValueEndIndex(tokens, index); for (int i = index + 1; i < end; i++) { switch (tokens[i].getType()) { case TokenType.START_OBJ: info.list.Add(JObject(tokens, i, ref jsonError)); i = GetValueEndIndex(tokens, i); break; case TokenType.START_ARRAY: info.list.Add(JArray(tokens, i, ref jsonError)); i = GetValueEndIndex(tokens, i); break; case TokenType.BOOLEAN: case TokenType.NUMBER: case TokenType.NULL: case TokenType.STRING: JsonInfo ji = new JsonInfo { value = tokens[i].getValue(), type = GetType(tokens, i) }; info.list.Add(ji); break; } } return(info); }
public void JsonParserFailsToParseEmptyObject() { string createTableJsonResponse = "{\"status\": \" abc\\t abc\\r abc\\n abc\\b abc\\f abc\\\\ abc\\f abc\\u0041 \", \"number\" : 2 }"; JsonItem job; using (var jsonParser = new JsonParser(createTableJsonResponse)) { job = jsonParser.ParseNext(); if (job.IsError) { JsonParseError error = (JsonParseError)job; Assert.Fail(error.ToString()); } } string statusString; Assert.IsTrue(job.GetProperty("status").TryGetValue(out statusString), "unable to retrieve the status string"); Assert.AreEqual(" abc\t abc\r abc\n abc\b abc\f abc\\ abc\f abcA ", statusString); }
private static string GetKey(List <Token> tokens, int index, ref JsonParseError jsonError) { if (index == 0) { return(string.Empty); } if (tokens[index - 1].getType() == TokenType.COLON) { return(tokens[index - 2].getValue()); } if (tokens[index - 1].getType() == TokenType.START_ARRAY || tokens[index - 1].getType() == TokenType.COMMA) { return(string.Empty); } jsonError.error = ParseError.MissingValueSeparator; return(string.Empty); }
private void AppendExp(StringBuilder sb, ref JsonParseError jsonError) { int c = read(); if (c == '+' || c == '-') { sb.Append((char)c); //Append '+' or '-' c = read(); if (!isDigit(c)) { jsonError.error = ParseError.IllegalNumber; return; } else { //e+(-) digit do { sb.Append((char)c); c = read(); } while (isDigit(c)); unread(); } } else if (!isDigit(c)) { jsonError.error = ParseError.IllegalNumber; return; } else { //e digit do { sb.Append((char)c); c = read(); } while (isDigit(c)); unread(); } }
public void Tokenize(ref JsonParseError jsonError) { jsonError.error = ParseError.NoError; Token token; do { if ((token = Inspect(ref jsonError)) == null || jsonError.error != ParseError.NoError) { break; } tokens.Add(token); } while (token.getType() != TokenType.END_DOC); if (tokens[tokens.Count - 2].getType() == TokenType.END_OBJ || tokens[tokens.Count - 2].getType() == TokenType.END_ARRAY) { return; } jsonError.error = ParseError.GarbageAtEnd; }
private void numAppend(StringBuilder sb, ref JsonParseError jsonError) { c = read(); if (c == '.') { //int frac sb.Append((char)c); //apppend '.' AppendFrac(sb); if (isExp(c)) { //int frac exp sb.Append((char)c); //Append 'e' or 'E'; AppendExp(sb, ref jsonError); } } else if (isExp(c)) { // int exp sb.Append((char)c); //Append 'e' or 'E' AppendExp(sb, ref jsonError); } else { unread(); } }
private void ParseError(string json, int state, int position, JsonParseError type, char jch, string expected) { throw new JsonException(json, state, position, jch, expected, type); }
private static bool isSeparate(List <Token> tokens, ref JsonParseError jsonError) { TokenType type = TokenType.START_OBJ; int count = tokens.Count; for (int i = 0; i < count; i++) { if (i + 1 < tokens.Count) { type = tokens[i + 1].getType(); } switch (tokens[i].getType()) { case TokenType.END_ARRAY: if (type == TokenType.END_OBJ || type == TokenType.END_DOC) { break; } if (type != TokenType.COMMA) { return(true); } break; case TokenType.END_OBJ: if (type == TokenType.END_ARRAY || type == TokenType.END_DOC) { break; } if (type != TokenType.COMMA) { return(true); } break; case TokenType.NULL: case TokenType.NUMBER: case TokenType.BOOLEAN: if (type == TokenType.END_ARRAY || type == TokenType.END_OBJ) { break; } if (type != TokenType.COMMA) { return(true); } break; case TokenType.STRING: if (tokens[i - 1].getType() == TokenType.COLON) { if (type == TokenType.END_ARRAY || type == TokenType.END_OBJ) { break; } if (type != TokenType.COMMA) { return(true); } } break; } } return(false); }
public JsonException(string json, int state, int position, char ch, string expected, JsonParseError type) { this.Char = ch; this.Expectation = expected; this.Type = type; this.State = state; this.Position = position; var pos = 0; foreach (var c in json) { if (pos == position) { Debug.WriteLine(c); break; } if (c == '\n') { Row++; Col = 0; } Col++; pos++; } message = string.Format("Json Parse Error: {0}. Expected {1} at position {2} (row: {3}, col: {4}), state {5}. Current character is {6}", type, expected, position, Row, Col, state, ch); }