Beispiel #1
0
 private void Set(JSONValue aVal)
 {
     if (m_Key == null)
     {
         m_Node.Add(aVal);
     }
     else
     {
         m_Node.Add(m_Key, aVal);
     }
     m_Node = null; // Be GC friendly.
 }
Beispiel #2
0
    static void ParseElement(JSONValue ctx, string token, string tokenName, bool quoted)
    {
        if (quoted)
        {
            ctx.Add(tokenName, token);
            return;
        }
        string tmp = token.ToLower();

        if (tmp == "false" || tmp == "true")
        {
            ctx.Add(tokenName, tmp == "true");
        }
        else if (tmp == "null")
        {
            ctx.Add(tokenName, null);
        }
        else
        {
            int    intVal = 0;
            long   uLong  = 0;
            double val    = 0;

            if (int.TryParse(token, out intVal))
            {
                ctx.Add(tokenName, intVal);
            }
            else if (long.TryParse(token, out uLong))
            {
                ctx.Add(tokenName, uLong);
            }
            else
            {
                if (double.TryParse(token, out val))
                {
                    ctx.Add(tokenName, val);
                }
                else
                {
                    ctx.Add(tokenName, token);
                }
            }
        }
    }
Beispiel #3
0
    public static JSONObject Parse(string aJSON)
    {
        if (aJSON == null)
        {
            return(null);
        }

        Stack <JSONValue> stack     = new Stack <JSONValue>();
        JSONValue         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)
                {
                    UnityEngine.Debug.Log(aJSON);
                    throw new Exception("JSON Parse: Too many closing brackets");
                }

                stack.Pop();
                if (Token.Length > 0 || TokenIsQuoted)
                {
                    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();
                Token.Length  = 0;
                TokenIsQuoted = false;
                break;

            case '"':
                QuoteMode     ^= true;
                TokenIsQuoted |= QuoteMode;
                break;

            case ',':
                if (QuoteMode)
                {
                    Token.Append(aJSON[i]);
                    break;
                }
                if (Token.Length > 0 || TokenIsQuoted)
                {
                    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 as JSONObject);
    }