public static JSONNode Parse(string aJSON) { Stack <JSONNode> stack = new Stack <JSONNode>(); JSONNode ctx = null; int num = 0; StringBuilder builder = new StringBuilder(); string tokenName = string.Empty; bool flag = false; bool quoted = false; while (num < aJSON.Length) { char ch = aJSON[num]; switch (ch) { case '\t': goto Label_026C; case '\n': case '\r': goto Label_0368; default: switch (ch) { case '[': if (!flag) { goto Label_0115; } builder.Append(aJSON[num]); goto Label_0368; case '\\': goto Label_0286; case ']': goto Label_0154; default: switch (ch) { case ' ': goto Label_026C; case '"': flag ^= true; quoted |= flag; goto Label_0368; } break; } switch (ch) { case '{': if (!flag) { break; } builder.Append(aJSON[num]); goto Label_0368; case '}': goto Label_0154; } switch (ch) { case ',': if (flag) { builder.Append(aJSON[num]); } else { if (builder.Length > 0) { ParseElement(ctx, builder.ToString(), tokenName, quoted); quoted = false; } tokenName = string.Empty; builder.Length = 0; quoted = false; } goto Label_0368; case ':': if (flag) { builder.Append(aJSON[num]); } else { tokenName = builder.ToString().Trim(); builder.Length = 0; quoted = false; } goto Label_0368; default: goto Label_0355; } break; } stack.Push(new JSONObject()); if (ctx != null) { ctx.Add(tokenName, stack.Peek()); } tokenName = string.Empty; builder.Length = 0; ctx = stack.Peek(); goto Label_0368; Label_0115: stack.Push(new JSONArray()); if (ctx != null) { ctx.Add(tokenName, stack.Peek()); } tokenName = string.Empty; builder.Length = 0; ctx = stack.Peek(); goto Label_0368; Label_0154: if (flag) { builder.Append(aJSON[num]); } else { if (stack.Count == 0) { throw new Exception("JSON Parse: Too many closing brackets"); } stack.Pop(); if (builder.Length > 0) { ParseElement(ctx, builder.ToString(), tokenName, quoted); quoted = false; } tokenName = string.Empty; builder.Length = 0; if (stack.Count > 0) { ctx = stack.Peek(); } } goto Label_0368; Label_026C: if (flag) { builder.Append(aJSON[num]); } goto Label_0368; Label_0286: num++; if (!flag) { goto Label_0368; } char ch2 = aJSON[num]; switch (ch2) { case 'r': builder.Append('\r'); goto Label_0368; case 't': builder.Append('\t'); goto Label_0368; case 'u': { string s = aJSON.Substring(num + 1, 4); builder.Append((char)int.Parse(s, NumberStyles.AllowHexSpecifier)); num += 4; goto Label_0368; } default: switch (ch2) { case 'b': builder.Append('\b'); goto Label_0368; case 'f': builder.Append('\f'); goto Label_0368; case 'n': builder.Append('\n'); goto Label_0368; default: builder.Append(ch2); goto Label_0368; } break; } Label_0355: builder.Append(aJSON[num]); Label_0368: num++; } if (flag) { throw new Exception("JSON Parse: Quotation marks seems to be messed up."); } return(ctx); }
public static JSONNode Parse(string aJSON) { Stack <JSONNode> stack = new Stack <JSONNode>(); JSONNode ctx = null; int i = 0; StringBuilder Token = new StringBuilder(); string TokenName = ""; bool QuoteMode = false; bool TokenIsQuoted = false; while (i < aJSON.Length) { switch (aJSON[i]) { case '{': if (QuoteMode) { Token.Append(aJSON[i]); break; } stack.Push(new JSONObject()); if (ctx != null) { ctx.Add(TokenName, stack.Peek()); } TokenName = ""; Token.Length = 0; ctx = stack.Peek(); break; case '[': if (QuoteMode) { Token.Append(aJSON[i]); break; } stack.Push(new JSONArray()); if (ctx != null) { ctx.Add(TokenName, stack.Peek()); } TokenName = ""; Token.Length = 0; ctx = stack.Peek(); break; case '}': case ']': if (QuoteMode) { Token.Append(aJSON[i]); break; } if (stack.Count == 0) { throw new Exception("JSON Parse: Too many closing brackets"); } stack.Pop(); if (Token.Length > 0) { ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted); TokenIsQuoted = false; } TokenName = ""; Token.Length = 0; if (stack.Count > 0) { ctx = stack.Peek(); } break; case ':': if (QuoteMode) { Token.Append(aJSON[i]); break; } TokenName = Token.ToString().Trim(); Token.Length = 0; TokenIsQuoted = false; break; case '"': QuoteMode ^= true; TokenIsQuoted |= QuoteMode; break; case ',': if (QuoteMode) { Token.Append(aJSON[i]); break; } if (Token.Length > 0) { ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted); TokenIsQuoted = false; } TokenName = ""; Token.Length = 0; TokenIsQuoted = false; break; case '\r': case '\n': break; case ' ': case '\t': if (QuoteMode) { Token.Append(aJSON[i]); } break; case '\\': ++i; if (QuoteMode) { char C = aJSON[i]; switch (C) { case 't': Token.Append('\t'); break; case 'r': Token.Append('\r'); break; case 'n': Token.Append('\n'); break; case 'b': Token.Append('\b'); break; case 'f': Token.Append('\f'); break; case 'u': { string s = aJSON.Substring(i + 1, 4); Token.Append((char)int.Parse( s, System.Globalization.NumberStyles.AllowHexSpecifier)); i += 4; break; } default: Token.Append(C); break; } } break; default: Token.Append(aJSON[i]); break; } ++i; } if (QuoteMode) { throw new Exception("JSON Parse: Quotation marks seems to be messed up."); } return(ctx); }