public static JSONNode Parse(string aJSON) { Stack <JSONNode> stack = new Stack <JSONNode>(); JSONNode ctx = null; int i = 0; string Token = ""; string TokenName = ""; bool QuoteMode = false; while (i < aJSON.Length) { switch (aJSON[i]) { case '{': if (QuoteMode) { Token += aJSON[i]; break; } stack.Push(new JSONClass()); if (ctx != null) { TokenName = TokenName.Trim(); if (ctx is JSONArray) { ctx.Add(stack.Peek()); } else if (TokenName != "") { ctx.Add(TokenName, stack.Peek()); } } TokenName = ""; Token = ""; ctx = stack.Peek(); break; case '[': if (QuoteMode) { Token += aJSON[i]; break; } stack.Push(new JSONArray()); if (ctx != null) { TokenName = TokenName.Trim(); if (ctx is JSONArray) { ctx.Add(stack.Peek()); } else if (TokenName != "") { ctx.Add(TokenName, stack.Peek()); } } TokenName = ""; Token = ""; ctx = stack.Peek(); break; case '}': case ']': if (QuoteMode) { Token += aJSON[i]; break; } if (stack.Count == 0) { throw new Exception("JSON Parse: Too many closing brackets"); } stack.Pop(); if (Token != "") { TokenName = TokenName.Trim(); if (ctx is JSONArray) { ctx.Add(Token); } else if (TokenName != "") { ctx.Add(TokenName, Token); } } TokenName = ""; Token = ""; if (stack.Count > 0) { ctx = stack.Peek(); } break; case ':': if (QuoteMode) { Token += aJSON[i]; break; } TokenName = Token; Token = ""; break; case '"': QuoteMode ^= true; break; case ',': if (QuoteMode) { Token += aJSON[i]; break; } if (Token != "") { if (ctx is JSONArray) { ctx.Add(Token); } else if (TokenName != "") { ctx.Add(TokenName, Token); } } TokenName = ""; Token = ""; break; case '\r': case '\n': break; case ' ': case '\t': if (QuoteMode) { Token += aJSON[i]; } break; case '\\': ++i; if (QuoteMode) { char C = aJSON[i]; switch (C) { case 't': Token += '\t'; break; case 'r': Token += '\r'; break; case 'n': Token += '\n'; break; case 'b': Token += '\b'; break; case 'f': Token += '\f'; break; case 'u': { string s = aJSON.Substring(i + 1, 4); Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier); i += 4; break; } default: Token += C; break; } } break; default: Token += aJSON[i]; break; } ++i; } if (QuoteMode) { 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 node = null; int num = 0; string aItem = string.Empty; string aKey = string.Empty; bool flag = false; while (num < aJSON.Length) { char ch; char ch2 = aJSON[num]; switch (ch2) { case '\t': case ' ': if (flag) { aItem = aItem + aJSON[num]; } goto Label_0467; case '\n': case '\r': goto Label_0467; case '"': flag ^= true; goto Label_0467; default: switch (ch2) { case '[': if (!flag) { goto Label_0151; } aItem = aItem + aJSON[num]; goto Label_0467; case '\\': num++; if (!flag) { goto Label_0467; } ch = aJSON[num]; switch (ch) { case 'n': aItem = aItem + '\n'; goto Label_0467; case 'r': aItem = aItem + '\r'; goto Label_0467; case 't': aItem = aItem + '\t'; goto Label_0467; case 'u': { string s = aJSON.Substring(num + 1, 4); aItem = aItem + ((char)int.Parse(s, NumberStyles.AllowHexSpecifier)); num += 4; goto Label_0467; } case 'b': aItem = aItem + '\b'; goto Label_0467; case 'f': goto Label_03F6; } goto Label_0437; case ']': goto Label_01C5; default: switch (ch2) { case '{': if (!flag) { break; } aItem = aItem + aJSON[num]; goto Label_0467; case '}': goto Label_01C5; } break; } switch (ch2) { case ',': if (flag) { aItem = aItem + aJSON[num]; } else { if (aItem != string.Empty) { if (node is JSONArray) { node.Add(aItem); } else if (aKey != string.Empty) { node.Add(aKey, aItem); } } aKey = string.Empty; aItem = string.Empty; } goto Label_0467; case ':': if (flag) { aItem = aItem + aJSON[num]; } else { aKey = aItem; aItem = string.Empty; } goto Label_0467; default: aItem = aItem + aJSON[num]; goto Label_0467; } break; } stack.Push(new JSONClass()); if (node != null) { aKey = aKey.Trim(); if (node is JSONArray) { node.Add(stack.Peek()); } else if (aKey != string.Empty) { node.Add(aKey, stack.Peek()); } } aKey = string.Empty; aItem = string.Empty; node = stack.Peek(); goto Label_0467; Label_0151: stack.Push(new JSONArray()); if (node != null) { aKey = aKey.Trim(); if (node is JSONArray) { node.Add(stack.Peek()); } else if (aKey != string.Empty) { node.Add(aKey, stack.Peek()); } } aKey = string.Empty; aItem = string.Empty; node = stack.Peek(); goto Label_0467; Label_01C5: if (flag) { aItem = aItem + aJSON[num]; } else { if (stack.Count == 0) { throw new Exception("JSON Parse: Too many closing brackets"); } stack.Pop(); if (aItem != string.Empty) { aKey = aKey.Trim(); if (node is JSONArray) { node.Add(aItem); } else if (aKey != string.Empty) { node.Add(aKey, aItem); } } aKey = string.Empty; aItem = string.Empty; if (stack.Count > 0) { node = stack.Peek(); } } goto Label_0467; Label_03F6: aItem = aItem + '\f'; goto Label_0467; Label_0437: aItem = aItem + ch; Label_0467: num++; } if (flag) { throw new Exception("JSON Parse: Quotation marks seems to be messed up."); } return(node); }