public static object DeJSONify(JSONObject j) { switch (j.type) { case JSONObject.Type.STRING: return j.str; case JSONObject.Type.NUMBER: return j.n; case JSONObject.Type.OBJECT: Dictionary<string, object> dict = new Dictionary<string, object>(); for (int i = 0; i < j.list.Count; i++) { string key = (string)j.keys[i]; dict.Add(key,DeJSONify((JSONObject)j.list[i])); } return dict; case JSONObject.Type.ARRAY: ArrayList retList = new ArrayList(); foreach (JSONObject o in j.list){ retList.Add(DeJSONify(o)); } return retList; case JSONObject.Type.BOOL: return j.b; case JSONObject.Type.NULL: return null; default: // TODO: Throw error here! return null; } }
public static JSONObject jsonObjectify(object o) { JSONObject j = null; Type ot = o.GetType(); if (ot == typeof(String)) { j = new JSONObject { type = JSONObject.Type.STRING, str = (string)o }; } else if (ot == typeof(ArrayList)) { j = new JSONObject { type = JSONObject.Type.ARRAY }; foreach (object element in (ArrayList)o) { j.Add(jsonObjectify(element)); } } else if (ot == typeof(Dictionary<string, object>)) { j = new JSONObject { type = JSONObject.Type.OBJECT }; foreach (KeyValuePair<string,object> entry in (Dictionary<string, object>)o) { j.AddField(entry.Key, jsonObjectify(entry.Value)); } } else if (ot.IsPrimitive) { if (typeof(bool) == ot) { j = new JSONObject { type = JSONObject.Type.BOOL, b = (bool)o}; } else { double d = Convert.ToDouble(o); j = new JSONObject { type = JSONObject.Type.NUMBER, n = d}; } } return j; }
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; } }
public OKLeaderboard(JSONObject leaderboardJSON) { this.Name = leaderboardJSON.GetField("name").str; this.LeaderboardID = (int)leaderboardJSON.GetField("id").n; this.IconUrl = leaderboardJSON.GetField("icon_url").str; this.PlayerCount = (int)leaderboardJSON.GetField("player_count").n; string sortString = leaderboardJSON.GetField("sort_type").str; if(sortString.Equals("HighValue",System.StringComparison.CurrentCultureIgnoreCase)) { this.SortType = LeaderboardSortType.HighValue; } else { this.SortType = LeaderboardSortType.LowValue; } }
public static JSONObject jsonObjectify(object o) { JSONObject j = null; Type ot = o.GetType(); if (ot == typeof(String)) { j = new JSONObject { type = JSONObject.Type.STRING, str = (string)o }; } else if (ot == typeof(ArrayList)) { j = new JSONObject { type = JSONObject.Type.ARRAY }; foreach (object element in (ArrayList)o) { j.Add(jsonObjectify(element)); } } else if (ot.IsGenericType && (ot.GetGenericTypeDefinition() == typeof(List<>))) { j = new JSONObject { type = JSONObject.Type.ARRAY }; // There must be a better way to do this at runtime. Right now we get // the type T in List<T> and cast 'o' to it. Type lt = ot.GetGenericArguments()[0]; if (lt == typeof(string)) { foreach (object element in (List<string>)o) { j.Add(jsonObjectify(element)); } } else { UnityEngine.Debug.LogError("Lists of that type are not supported by JSONObjectExt! Fix me."); } } else if (ot == typeof(Dictionary<string, object>)) { j = new JSONObject { type = JSONObject.Type.OBJECT }; foreach (KeyValuePair<string,object> entry in (Dictionary<string, object>)o) { j.AddField(entry.Key, jsonObjectify(entry.Value)); } } else if (ot.IsPrimitive) { if (typeof(bool) == ot) { j = new JSONObject { type = JSONObject.Type.BOOL, b = (bool)o}; } else { double d = Convert.ToDouble(o); j = new JSONObject { type = JSONObject.Type.NUMBER, n = d}; } } return j; }
public static JSONObject decode(string encodedStr) { JSONObject jsonObj = new JSONObject(encodedStr); return jsonObj; }
public string print(int depth) //Convert the JSONObject into a stiring { if (depth++ > MAX_DEPTH) { Debug.Log("reached max depth!"); return(""); } string str = ""; switch (type) { case Type.STRING: str = "\"" + this.str + "\""; break; case Type.NUMBER: str += n; break; case JSONObject.Type.OBJECT: if (list.Count > 0) { str = "{"; #if (READABLE) //for a bit more readability, comment the define above to save space str += "\n"; depth++; #endif for (int i = 0; i < list.Count; i++) { string key = (string)keys[i]; JSONObject obj = (JSONObject)list[i]; if (obj != null) { #if (READABLE) for (int j = 0; j < depth; j++) { str += "\t"; //for a bit more readability } #endif str += "\"" + key + "\":"; str += obj.print(depth) + ","; #if (READABLE) str += "\n"; #endif } } #if (READABLE) str = str.Substring(0, str.Length - 1); #endif str = str.Substring(0, str.Length - 1); str += "}"; } else { str += "null"; } break; case JSONObject.Type.ARRAY: if (list.Count > 0) { str = "["; #if (READABLE) str += "\n"; //for a bit more readability depth++; #endif foreach (JSONObject obj in list) { if (obj != null) { #if (READABLE) for (int j = 0; j < depth; j++) { str += "\t"; //for a bit more readability } #endif str += obj.print(depth) + ","; #if (READABLE) str += "\n"; //for a bit more readability #endif } } #if (READABLE) str = str.Substring(0, str.Length - 1); #endif str = str.Substring(0, str.Length - 1); str += "]"; } break; case Type.BOOL: if (b) { str += "true"; } else { str += "false"; } break; case Type.NULL: str = "null"; break; } return(str); }
/* * The Merge function is experimental. Use at your own risk. */ public void Merge(JSONObject obj) { MergeRecur(this, obj); }
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); }
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 Debug.LogWarning("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 != 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 Debug.LogWarning("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); } }