Example #1
0
        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);
        }
Example #2
0
        // Token: 0x06000028 RID: 40 RVA: 0x00002440 File Offset: 0x00000840
        public static JSONNode Parse(string aJSON)
        {
            Stack <JSONNode> stack    = new Stack <JSONNode>();
            JSONNode         jsonnode = null;
            int    i     = 0;
            string text  = string.Empty;
            string text2 = string.Empty;
            bool   flag  = false;

            while (i < aJSON.Length)
            {
                char c = aJSON[i];
                switch (c)
                {
                case '\t':
                    goto IL_333;

                case '\n':
                case '\r':
                    break;

                default:
                    switch (c)
                    {
                    case '[':
                        if (flag)
                        {
                            text += aJSON[i];
                            goto IL_45C;
                        }
                        stack.Push(new JSONArray());
                        if (jsonnode != null)
                        {
                            text2 = text2.Trim();
                            if (jsonnode is JSONArray)
                            {
                                jsonnode.Add(stack.Peek());
                            }
                            else if (text2 != string.Empty)
                            {
                                jsonnode.Add(text2, stack.Peek());
                            }
                        }
                        text2    = string.Empty;
                        text     = string.Empty;
                        jsonnode = stack.Peek();
                        goto IL_45C;

                    case '\\':
                        i++;
                        if (flag)
                        {
                            char c2 = aJSON[i];
                            switch (c2)
                            {
                            case 'r':
                                text += '\r';
                                break;

                            default:
                                if (c2 != 'b')
                                {
                                    if (c2 != 'f')
                                    {
                                        if (c2 != 'n')
                                        {
                                            text += c2;
                                        }
                                        else
                                        {
                                            text += '\n';
                                        }
                                    }
                                    else
                                    {
                                        text += '\f';
                                    }
                                }
                                else
                                {
                                    text += '\b';
                                }
                                break;

                            case 't':
                                text += '\t';
                                break;

                            case 'u':
                            {
                                string s = aJSON.Substring(i + 1, 4);
                                text += (char)int.Parse(s, NumberStyles.AllowHexSpecifier);
                                i    += 4;
                                break;
                            }
                            }
                        }
                        goto IL_45C;

                    case ']':
                        break;

                    default:
                        switch (c)
                        {
                        case ' ':
                            goto IL_333;

                        default:
                            switch (c)
                            {
                            case '{':
                                if (flag)
                                {
                                    text += aJSON[i];
                                    goto IL_45C;
                                }
                                stack.Push(new JSONClass());
                                if (jsonnode != null)
                                {
                                    text2 = text2.Trim();
                                    if (jsonnode is JSONArray)
                                    {
                                        jsonnode.Add(stack.Peek());
                                    }
                                    else if (text2 != string.Empty)
                                    {
                                        jsonnode.Add(text2, stack.Peek());
                                    }
                                }
                                text2    = string.Empty;
                                text     = string.Empty;
                                jsonnode = stack.Peek();
                                goto IL_45C;

                            default:
                                if (c != ',')
                                {
                                    if (c != ':')
                                    {
                                        text += aJSON[i];
                                        goto IL_45C;
                                    }
                                    if (flag)
                                    {
                                        text += aJSON[i];
                                        goto IL_45C;
                                    }
                                    text2 = text;
                                    text  = string.Empty;
                                    goto IL_45C;
                                }
                                else
                                {
                                    if (flag)
                                    {
                                        text += aJSON[i];
                                        goto IL_45C;
                                    }
                                    if (text != string.Empty)
                                    {
                                        if (jsonnode is JSONArray)
                                        {
                                            jsonnode.Add(text);
                                        }
                                        else if (text2 != string.Empty)
                                        {
                                            jsonnode.Add(text2, text);
                                        }
                                    }
                                    text2 = string.Empty;
                                    text  = string.Empty;
                                    goto IL_45C;
                                }
                                break;

                            case '}':
                                break;
                            }
                            break;

                        case '"':
                            flag ^= true;
                            goto IL_45C;
                        }
                        break;
                    }
                    if (flag)
                    {
                        text += aJSON[i];
                    }
                    else
                    {
                        if (stack.Count == 0)
                        {
                            throw new Exception("JSON Parse: Too many closing brackets");
                        }
                        stack.Pop();
                        if (text != string.Empty)
                        {
                            text2 = text2.Trim();
                            if (jsonnode is JSONArray)
                            {
                                jsonnode.Add(text);
                            }
                            else if (text2 != string.Empty)
                            {
                                jsonnode.Add(text2, text);
                            }
                        }
                        text2 = string.Empty;
                        text  = string.Empty;
                        if (stack.Count > 0)
                        {
                            jsonnode = stack.Peek();
                        }
                    }
                    break;
                }
IL_45C:
                i++;
                continue;
IL_333:
                if (flag)
                {
                    text += aJSON[i];
                }
                goto IL_45C;
            }
            if (flag)
            {
                throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
            }
            return(jsonnode);
        }