Beispiel #1
0
    //	public static bool output_debug = false;

    void Parse(string inStr, int maxDepth = -2, bool storeExcessLevels = false, bool strict = false)
    {
        if (!string.IsNullOrEmpty(inStr))
        {
            inStr = inStr.Trim(WHITESPACE);
            if (strict)
            {
                if (inStr [0] != '[' && inStr [0] != '{')
                {
                    type = Type.NULL;
                    Debug.LogWarning("Improper (strict) JSON formatting.  First character must be [ or {");
                    return;
                }
            }
            if (inStr.Length > 0)
            {
                if (string.Compare(inStr, "true", true) == 0)
                {
                    type = Type.BOOL;
                    b    = true;
                }
                else if (string.Compare(inStr, "false", true) == 0)
                {
                    type = Type.BOOL;
                    b    = false;
                }
                else if (string.Compare(inStr, "null", true) == 0)
                {
                    type = Type.NULL;
//#if USEFLOAT
                }
                else if (inStr == INFINITY)
                {
                    type = Type.NUMBER;
                    f    = float.PositiveInfinity;
                }
                else if (inStr == NEGINFINITY)
                {
                    type = Type.NUMBER;
                    f    = float.NegativeInfinity;
                }
                else if (inStr == NaN)
                {
                    type = Type.NUMBER;
                    f    = float.NaN;
//#else
//            } else if(str == INFINITY) {
//                type = Type.NUMBER;
//                n = double.PositiveInfinity;
//            } else if(str == NEGINFINITY) {
//                type = Type.NUMBER;
//                n = double.NegativeInfinity;
//            } else if(str == NaN) {
//                type = Type.NUMBER;
//                n = double.NaN;
//#endif
                }
                else if (inStr [0] == '"')
                {
                    type = Type.STRING;
                    str  = inStr.Substring(1, inStr.Length - 2);
                    if (str.Length > 0 && str.Contains("\\u"))
                    {
                        str = UnicodeUtil.Convert(str);
                    }
                }
                else
                {
                    int tokenTmp = 1;

                    /*
                     * Checking for the following formatting (www.json.org)
                     * object - {"field1":value,"field2":value}
                     * array - [value,value,value]
                     * value - string	- "string"
                     *		 - number	- 0.0
                     *		 - bool		- true -or- false
                     *		 - null		- null
                     */
                    int offset = 0;
                    switch (inStr [offset])
                    {
                    case '{':
                        type = Type.OBJECT;
                        keys = new List <string> ();
                        list = new List <JSONObject> ();
                        break;

                    case '[':
                        type = Type.ARRAY;
                        list = new List <JSONObject> ();
                        break;

                    default:
                        try {
                                                        #if USEFLOAT
                            if (inStr.IndexOf('.') >= 0 || inStr.IndexOf('e') >= 0 || inStr.IndexOf('E') >= 0)
                            {
                                f = System.Convert.ToSingle(inStr);
                            }
                            else
                            {
                                n = System.Convert.ToInt64(inStr);
                            }
                                                        #else
                            n = System.Convert.ToDouble(str);
                                                        #endif
                            type = Type.NUMBER;
                        } catch (System.FormatException) {
                            type = Type.NULL;
                            //Debug.LogWarning ("improper JSON formatting:" + str);
                        }
                        return;
                    }
                    string propName  = "";
                    bool   openQuote = false;
                    bool   inProp    = false;
                    int    depth     = 0;
                    while (++offset < inStr.Length)
                    {
                        if (System.Array.IndexOf(WHITESPACE, inStr [offset]) > -1)
                        {
                            continue;
                        }
                        if (inStr [offset] == '\\')
                        {
                            offset += 2;
                        }
                        if (inStr [offset] == '"')
                        {
                            if (openQuote)
                            {
                                if (!inProp && depth == 0 && type == Type.OBJECT)
                                {
                                    propName = inStr.Substring(tokenTmp + 1, offset - tokenTmp - 1);
                                }
                                openQuote = false;
                            }
                            else
                            {
                                if (depth == 0 && type == Type.OBJECT)
                                {
                                    tokenTmp = offset;
                                }
                                openQuote = true;
                            }
                        }
                        if (openQuote)
                        {
                            continue;
                        }
                        if (type == Type.OBJECT && depth == 0)
                        {
                            if (inStr [offset] == ':')
                            {
                                tokenTmp = offset + 1;
                                inProp   = true;
                            }
                        }

                        if (inStr [offset] == '[' || inStr [offset] == '{')
                        {
                            depth++;
                        }
                        else if (inStr [offset] == ']' || inStr [offset] == '}')
                        {
                            depth--;
                        }
                        //if  (encounter a ',' at top level)  || a closing ]/}
                        if ((inStr [offset] == ',' && depth == 0) || depth < 0)
                        {
                            inProp = false;
                            string inner = inStr.Substring(tokenTmp, offset - tokenTmp).Trim(WHITESPACE);
                            if (inner.Length > 0)
                            {
//								if (output_debug) {
//									Log.D ("{0}", inner);
//									if (inner.CompareTo ("1442809048") == 0)
//										Log.D ("{0}", inner);
//								}
                                if (type == Type.OBJECT)
                                {
                                    keys.Add(propName);
                                }
                                if (maxDepth != -1)                                                                                                                                                     //maxDepth of -1 is the end of the line
                                {
                                    list.Add(Create(inner, (maxDepth < -1) ? -2 : maxDepth - 1));
                                }
                                else if (storeExcessLevels)
                                {
                                    list.Add(CreateBakedObject(inner));
                                }
                            }
                            tokenTmp = offset + 1;
                        }
                    }
                }
            }
            else
            {
                type = Type.NULL;
            }
        }
        else
        {
            type = Type.NULL;                   //If the string is missing, this is a null
        }
        //Profiler.EndSample();
    }