Ejemplo n.º 1
0
        public Dictionary <string, string> ToDictionary()
        {
            if (type == Type.OBJECT)
            {
                Dictionary <string, string> result = new Dictionary <string, string>();
                for (int i = 0; i < list.Count; i++)
                {
                    JSONObject val = (JSONObject)list[i];
                    switch (val.type)
                    {
                    case Type.STRING:       result.Add((string)keys[i], val.str);           break;

                    case Type.NUMBER:       result.Add((string)keys[i], val.n + "");        break;

                    case Type.BOOL:         result.Add((string)keys[i], val.b + "");        break;

                    default: OKLog.Warn("Omitting object: " + (string)keys[i] + " in dictionary conversion"); break;
                    }
                }
                return(result);
            }
            else
            {
                OKLog.Warn("Tried to turn non-Object JSONObject into a dictionary");
            }
            return(null);
        }
Ejemplo n.º 2
0
 public void Add(JSONObject obj)
 {
     if (obj != null)                    //Don't do anything if the object is null
     {
         if (type != JSONObject.Type.ARRAY)
         {
             type = JSONObject.Type.ARRAY;                       //Congratulations, son, you're an ARRAY now
             OKLog.Warn("tried to add an object to a non-array JSONObject.  We'll do it for you, but you might be doing something wrong.");
         }
         list.Add(obj);
     }
 }
Ejemplo n.º 3
0
 public void AddField(string name, JSONObject obj)
 {
     if (obj != null)                    //Don't do anything if the object is null
     {
         if (type != JSONObject.Type.OBJECT)
         {
             type = JSONObject.Type.OBJECT;                      //Congratulations, son, you're an OBJECT now
             OKLog.Warn("tried to add a field to a non-object JSONObject.  We'll do it for you, but you might be doing something wrong.");
         }
         keys.Add(name);
         list.Add(obj);
     }
 }
Ejemplo n.º 4
0
        public JSONObject(string str)   //create a new JSONObject from a string (this will also create any children, and parse the whole string)
        //OKLog.Info(str);
        {
            if (str != null)
            {
#if (READABLE)
                str = str.Replace("\\n", "");
                str = str.Replace("\\t", "");
                str = str.Replace("\\r", "");
                str = str.Replace("\t", "");
                str = str.Replace("\n", "");
                str = str.Replace("\\", "");
#endif
                if (str.Length > 0)
                {
                    if (string.Compare(str, "true", true) == 0)
                    {
                        type = Type.BOOL;
                        b    = true;
                    }
                    else if (string.Compare(str, "false", true) == 0)
                    {
                        type = Type.BOOL;
                        b    = false;
                    }
                    else if (str == "null")
                    {
                        type = Type.NULL;
                    }
                    else if (str[0] == '"')
                    {
                        type     = Type.STRING;
                        this.str = str.Substring(1, str.Length - 2);
                    }
                    else
                    {
                        try {
                            n    = System.Convert.ToDouble(str);
                            type = Type.NUMBER;
                        } catch (System.FormatException) {
                            int token_tmp = 0;

                            /*
                             * 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
                             */
                            switch (str[0])
                            {
                            case '{':
                                type = Type.OBJECT;
                                keys = new ArrayList();
                                list = new ArrayList();
                                break;

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

                            default:
                                type = Type.NULL;
                                OKLog.Warn("improper JSON formatting:" + str);
                                return;
                            }
                            int  depth     = 0;
                            bool openquote = false;
                            bool inProp    = false;
                            for (int i = 1; i < str.Length; i++)
                            {
                                if (str[i] == '\\')
                                {
                                    i++;
                                    continue;
                                }
                                if (str[i] == '"')
                                {
                                    openquote = !openquote;
                                }
                                if (str[i] == '[' || str[i] == '{')
                                {
                                    depth++;
                                }
                                if (depth == 0 && !openquote)
                                {
                                    if (str[i] == ':' && !inProp)
                                    {
                                        inProp = true;
                                        try {
                                            keys.Add(str.Substring(token_tmp + 2, i - token_tmp - 3));
                                        } catch { OKLog.Info(i + " - " + str.Length + " - " + str); }
                                        token_tmp = i;
                                    }
                                    if (str[i] == ',')
                                    {
                                        inProp = false;
                                        list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1)));
                                        token_tmp = i;
                                    }
                                    if (str[i] == ']' || str[i] == '}')
                                    {
                                        if (i - token_tmp > 1)
                                        {
                                            list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1)));
                                        }
                                    }
                                }
                                if (str[i] == ']' || str[i] == '}')
                                {
                                    depth--;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                type = Type.NULL;               //If the string is missing, this is a null
            }
        }