public JSONObject(JSONObject.Type t)
 {
     type = t;
     switch (t)
     {
         case Type.ARRAY:
             list = new ArrayList();
             break;
         case Type.OBJECT:
             list = new ArrayList();
             keys = new ArrayList();
             break;
     }
 }
        /// <summary>
        /// Build a JSON string from the event data
        /// </summary>
        /// <returns></returns>
        public String ToJSON()
        {
            // Create a json object from this event and then stringigy it
            JSONObject json = new JSONObject();
            json.AddField("jsonrpc", "2.0");

            // We always use id 1, no async tasks are required
            json.AddField("id", 1);

            // Add metod name
            json.AddField("method", GetMethodName());
            json.AddField("params", GetParams());

            return json.ToString();
        }
 static void MergeRecur(JSONObject left, JSONObject right)
 {
     if (right.type == JSONObject.Type.OBJECT)
     {
         for (int i = 0; i < right.list.Count; i++)
         {
             if (right.keys[i] != null)
             {
                 string key = (string)right.keys[i];
                 JSONObject val = (JSONObject)right.list[i];
                 if (val.type == JSONObject.Type.ARRAY || val.type == JSONObject.Type.OBJECT)
                 {
                     if (left.HasField(key))
                         MergeRecur(left[key], val);
                     else
                         left.AddField(key, val);
                 }
                 else
                 {
                     if (left.HasField(key))
                         left.SetField(key, val);
                     else
                         left.AddField(key, val);
                 }
             }
         }
     }// else left.list.Add(right.list);
 }
 public void SetField(string name, JSONObject obj)
 {
     if (HasField(name))
     {
         list.Remove(this[name]);
         keys.Remove(name);
     }
     AddField(name, obj);
 }
 /*
  * The Merge function is experimental. Use at your own risk.
  */
 public void Merge(JSONObject obj)
 {
     MergeRecur(this, obj);
 }
 public void AddField(string name, JSONObject obj)
 {
     if (obj)
     {		//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
             Console.WriteLine("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);
     }
 }
 public void Add(JSONObject obj)
 {
     if (obj)
     {		//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
             Console.WriteLine("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);
     }
 }