JSONObject

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Encoding)
(The JSONObject class)
Line 83: Line 83:
 
=The JSONObject class=
 
=The JSONObject class=
 
<csharp>
 
<csharp>
 +
#define READABLE
 +
 +
using UnityEngine;
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
 
/*
 
/*
 
  * http://www.opensource.org/licenses/lgpl-2.1.php
 
  * http://www.opensource.org/licenses/lgpl-2.1.php
 
  * JSONObject class
 
  * JSONObject class
 
  * for use with Unity
 
  * for use with Unity
  * Matt Schoen 2010
+
  * Copyright Matt Schoen 2010
 
  */
 
  */
//#define READABLE
 
 
using UnityEngine;
 
using System.Collections;
 
  
 
public class JSONObject : Nullable {
 
public class JSONObject : Nullable {
public ArrayList keys;
+
const int MAX_DEPTH = 1000;
 
public enum Type { NULL, STRING, NUMBER, OBJECT, ARRAY, BOOL }
 
public enum Type { NULL, STRING, NUMBER, OBJECT, ARRAY, BOOL }
 
public JSONObject parent;
 
public JSONObject parent;
public Type type;
+
public Type type = Type.NULL;
public ArrayList list;
+
public ArrayList list = new ArrayList();
 +
public ArrayList keys = new ArrayList();
 
public string str;
 
public string str;
 
public double n;
 
public double n;
 
public bool b;
 
public bool b;
 +
 +
public static JSONObject nullJO { get { return new JSONObject(JSONObject.Type.NULL); } }
 +
public static JSONObject obj { get { return new JSONObject(JSONObject.Type.OBJECT); } }
 +
public static JSONObject arr { get { return new JSONObject(JSONObject.Type.ARRAY); } }
  
 
public JSONObject(JSONObject.Type t) {
 
public JSONObject(JSONObject.Type t) {
 
type = t;
 
type = t;
 
switch(t) {
 
switch(t) {
case Type.ARRAY:
+
case Type.ARRAY:
list = new ArrayList();
+
list = new ArrayList();
break;
+
break;
case Type.OBJECT:
+
case Type.OBJECT:
list = new ArrayList();
+
list = new ArrayList();
keys = new ArrayList();
+
keys = new ArrayList();
break;
+
break;
 
}
 
}
 
}
 
}
public JSONObject(bool b){
+
public JSONObject(bool b) {
 
type = Type.BOOL;
 
type = Type.BOOL;
 
this.b = b;
 
this.b = b;
 
}
 
}
public JSONObject(float f){
+
public JSONObject(float f) {
 
type = Type.NUMBER;
 
type = Type.NUMBER;
 
this.n = f;
 
this.n = f;
 
}
 
}
public JSONObject() {}
+
public JSONObject() { }
 
public JSONObject(string str) { //create a new JSONObject from a string (this will also create any children, and parse the whole string)
 
public JSONObject(string str) { //create a new JSONObject from a string (this will also create any children, and parse the whole string)
 
//Debug.Log(str);
 
//Debug.Log(str);
if(str.Length > 0) {
+
if(str != null) {
if(str == "True") {
+
#if(READABLE)
type = Type.BOOL;
+
str = str.Replace("\\n", "");
b = true;
+
str = str.Replace("\\t", "");
} else if(str == "False") {
+
str = str.Replace("\\r", "");
type = Type.BOOL;
+
str = str.Replace("\t", "");
b = false;
+
str = str.Replace("\n", "");
} else if(str == "null") {
+
str = str.Replace("\\", "");
type = Type.NULL;
+
#endif
} else if(str[0] == '"') {
+
if(str.Length > 0) {
type = Type.STRING;
+
if(string.Compare(str, "true", true) == 0) {
this.str = str.Substring(1, str.Length - 2);
+
type = Type.BOOL;
} else {
+
b = true;
try {
+
} else if(string.Compare(str, "false", true) == 0) {
n = System.Convert.ToDouble(str);
+
type = Type.BOOL;
type = Type.NUMBER;
+
b = false;
} catch(System.FormatException) {
+
} else if(str == "null") {
int token_tmp = 0;
+
type = Type.NULL;
/*
+
} else if(str[0] == '"') {
* Checking for the following formatting (www.json.org)
+
type = Type.STRING;
* object - {"field1":value,"field2":value}
+
this.str = str.Substring(1, str.Length - 2);
* array - [value,value,value]
+
} else {
* value - string - "string"
+
try {
* - number - 0.0
+
n = System.Convert.ToDouble(str);
* - bool - true -or- false
+
type = Type.NUMBER;
* - null - null
+
} catch(System.FormatException) {
*/
+
int token_tmp = 0;
switch(str[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 '{':
 
case '{':
 
type = Type.OBJECT;
 
type = Type.OBJECT;
Line 166: Line 182:
 
default:
 
default:
 
type = Type.NULL;
 
type = Type.NULL;
Debug.Log("improper JSON formatting");
+
Debug.LogWarning("improper JSON formatting:" + str);
 
return;
 
return;
}
+
}
int depth = 0;
+
int depth = 0;
bool openquote = false;
+
bool openquote = false;
for(int i = 1; i < str.Length; i++) {
+
bool inProp = false;
if(str[i] == '"')
+
for(int i = 1; i < str.Length; i++) {
openquote = !openquote;
+
if(str[i] == '\\') {
if(str[i] == '[' || str[i] == '{')
+
i++;
depth++;
+
continue;
if(depth == 0 && !openquote) {
+
if(str[i] == ':') {
+
try{
+
keys.Add(str.Substring(token_tmp + 2, i - token_tmp - 3));
+
} catch {Debug.Log(i + " - " + str.Length + " - " + str);}
+
token_tmp = i;
+
 
}
 
}
if(str[i] == ',') {
+
if(str[i] == '"')
list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1)));
+
openquote = !openquote;
token_tmp = i;
+
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 { Debug.Log(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] == '}')
 +
list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1)));
 
}
 
}
 
if(str[i] == ']' || str[i] == '}')
 
if(str[i] == ']' || str[i] == '}')
list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1)));
+
depth--;
 
}
 
}
if(str[i] == ']' || str[i] == '}')
 
depth--;
 
 
}
 
}
 
}
 
}
Line 199: Line 223:
 
}
 
}
 
}
 
}
public void AddField(string name, bool val){ AddField(name, new JSONObject(val)); }
+
public void AddField(string name, bool val) { AddField(name, new JSONObject(val)); }
public void AddField(string name, float val){ AddField(name, new JSONObject(val)); }
+
public void AddField(string name, float val) { AddField(name, new JSONObject(val)); }
public void AddField(string name, int val){ AddField(name, new JSONObject(val)); }
+
public void AddField(string name, int val) { AddField(name, new JSONObject(val)); }
public void AddField(string name, string val){ AddField(name, new JSONObject{ type = JSONObject.Type.STRING, str = val} ); }
+
public void AddField(string name, string val) {
public void AddField(string name, JSONObject obj){
+
AddField(name, new JSONObject { type = JSONObject.Type.STRING, str = val });
keys.Add(name);
+
list.Add(obj);
+
 
}
 
}
public JSONObject GetField(string name){
+
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
 +
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 SetField(string name, JSONObject obj) {
 +
if(HasField(name)) {
 +
list.Remove(this[name]);
 +
keys.Remove(name);
 +
}
 +
AddField(name, obj);
 +
}
 +
public JSONObject GetField(string name) {
 
if(type == JSONObject.Type.OBJECT)
 
if(type == JSONObject.Type.OBJECT)
 
for(int i = 0; i < keys.Count; i++)
 
for(int i = 0; i < keys.Count; i++)
Line 214: Line 253:
 
return null;
 
return null;
 
}
 
}
public string print(){
+
public bool HasField(string name) {
 +
if(type == JSONObject.Type.OBJECT)
 +
for(int i = 0; i < keys.Count; i++)
 +
if((string)keys[i] == name)
 +
return true;
 +
return false;
 +
}
 +
public void Clear() {
 +
type = JSONObject.Type.NULL;
 +
list.Clear();
 +
keys.Clear();
 +
str = "";
 +
n = 0;
 +
b = false;
 +
}
 +
public JSONObject Copy() {
 +
return new JSONObject(print());
 +
}
 +
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 string print() {
 
return print(0);
 
return print(0);
 
}
 
}
 
public string print(int depth) { //Convert the JSONObject into a stiring
 
public string print(int depth) { //Convert the JSONObject into a stiring
if(depth++ > 1000){
+
if(depth++ > MAX_DEPTH) {
Debug.Log("infinte recursion!");
+
Debug.Log("reached max depth!");
 
return "";
 
return "";
 
}
 
}
 
string str = "";
 
string str = "";
 
switch(type) {
 
switch(type) {
case Type.STRING:
+
case Type.STRING:
str = "\"" + this.str + "\"";
+
str = "\"" + this.str + "\"";
break;
+
break;
case Type.NUMBER:
+
case Type.NUMBER:
str += n;
+
str += n;
break;
+
break;
case JSONObject.Type.OBJECT:
+
case JSONObject.Type.OBJECT:
if(list.Count > 0){
+
if(list.Count > 0) {
str = "{";
+
str = "{";
#if(READABLE)
+
#if(READABLE) //for a bit more readability, comment the define above to save space
str += "\n"; //for a bit more readability, comment back out for it to work :)
+
str += "\n";
depth++;
+
depth++;
#endif
+
#endif
for(int i = 0; i < list.Count; i++) {
+
for(int i = 0; i < list.Count; i++) {
string key = (string)keys[i];
+
string key = (string)keys[i];
JSONObject obj = (JSONObject)list[i];
+
JSONObject obj = (JSONObject)list[i];
if(obj){
+
if(obj) {
#if(READABLE)
+
#if(READABLE)
for(int j = 0; j < depth; j++)
+
for(int j = 0; j < depth; j++)
str += "\t"; //for a bit more readability, comment back out for it to work :)
+
str += "\t"; //for a bit more readability, comment back out for it to work :)
#endif
+
#endif
str += "\"" + key + "\":";  
+
str += "\"" + key + "\":";
str += obj.print(depth) +",";
+
str += obj.print(depth) + ",";
#if(READABLE)
+
#if(READABLE)
str += "\n";
+
str += "\n";
#endif
+
#endif
}
+
 
}
 
}
str = str.Substring(0, str.Length - 1);
 
str += "}";
 
 
}
 
}
break;
+
#if(READABLE)
case JSONObject.Type.ARRAY:
+
str = str.Substring(0, str.Length - 1);
if(list.Count > 0){
+
#endif
str = "[";
+
str = str.Substring(0, str.Length - 1);
#if(READABLE)
+
str += "}";
str += "\n"; //for a bit more readability, comment back out for it to work :)
+
} else str += "null";
depth++;
+
break;
#endif
+
case JSONObject.Type.ARRAY:
foreach(JSONObject obj in list) {
+
if(list.Count > 0) {
if(obj){
+
str = "[";
#if(READABLE)
+
#if(READABLE)
for(int j = 0; j < depth; j++)
+
str += "\n"; //for a bit more readability, comment back out for it to work :)
str += "\t"; //for a bit more readability, comment back out for it to work :)
+
depth++;
#endif
+
#endif
str += obj.print(depth) + ",";
+
foreach(JSONObject obj in list) {
#if(READABLE)
+
if(obj) {
str += "\n"; //for a bit more readability, comment back out for it to work :)
+
#if(READABLE)
#endif
+
for(int j = 0; j < depth; j++)
}
+
str += "\t"; //for a bit more readability, comment back out for it to work :)
 +
#endif
 +
str += obj.print(depth) + ",";
 +
#if(READABLE)
 +
str += "\n"; //for a bit more readability, comment back out for it to work :)
 +
#endif
 
}
 
}
str = str.Substring(0, str.Length - 1);
 
str += "]";
 
 
}
 
}
break;
+
#if(READABLE)
case Type.BOOL:
+
str = str.Substring(0, str.Length - 1);
str += b;
+
#endif
break;
+
str = str.Substring(0, str.Length - 1);
case Type.NULL:
+
str += "]";
str = "null";
+
}
break;
+
break;
 +
case Type.BOOL:
 +
if(b)
 +
str += "true";
 +
else
 +
str += "false";
 +
break;
 +
case Type.NULL:
 +
str = "null";
 +
break;
 
}
 
}
 
return str;
 
return str;
 +
}
 +
public JSONObject this[int index] {
 +
get { return (JSONObject)list[index]; }
 +
}
 +
public JSONObject this[string index] {
 +
get { return GetField(index); }
 +
}
 +
public override string ToString() {
 +
return base.ToString() + print();
 +
}
 +
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: Debug.LogWarning("Omitting object: " + (string)keys[i] + " in dictionary conversion"); break;
 +
}
 +
}
 +
return result;
 +
} else Debug.LogWarning("Tried to turn non-Object JSONObject into a dictionary");
 +
return null;
 
}
 
}
 
}
 
}
/csharp>
+
</csharp>

Revision as of 20:41, 4 April 2011

Contents

Author

Matt Schoen of Defective Studios

Download

Download JSONObject.zip

Intro

I came across the need to send structured data to and from a server on one of my projects, and figured it would be worth my while to use JSON. When I looked into the issue, I tried a few of the C# implementations listed on [json.org], but found them to be too complicated, or used part of the .NET framework not accessible from Unity scripts. So, I've written a very simple JSONObject class, which can be generically used to encode/decode data into a simple container. This page assumes that you know what JSON is, and how it works. It's rather simple, just go to json.org for a visual description of the encoding format.

Usage

Users should not have to modify the JSONObject class themselves, and must follow the very simple proceedures outlined below:

Sample data (in JSON format): {"field1":0.5,"field2":"sampletext","field3":[1,2,3]}

Features

It should be pretty obvious what this parser can and cannot do. If anyone reading this is a JSON buff (is there such a thing?) please feel free to expand and modify the parser to be more compliant. Currently I am using the .NET System.Convert namespace functions for parsing the data itself. It parses strings and numbers, which was all that I needed of it, but unless the formatting is supported by System.Convert, it may not incorporate all proper JSON strings. Also, having never written a JSON parser before, I don't doubt that I could improve the efficiency or correctness of the parser. It serves my purpose, and hopefully will help you with your project! Let me know if you make any improvements :)

Encoding

Encoding is something of a hard-coded process. This is because I have no idea what your data is! It would be great if this were some sort of interface for taking an entire class and encoding it's number/string fields, but it's not. I've come up with a few clever ways of using loops and/or recursive methods to cut down of the amount of code I have to write when I use this tool, but they're pretty project-specific.

<csharp> //Note: your data can only be numbers and strings. This is not a solution for object serialization or anything like that. JSONObject j = new JSONObject(JSONObject.Type.OBJECT); //number j.AddField("field1", 0.5); //string j.AddField("field2", "sampletext"); //array JSONObject arr = new JSONObject(JSONObject.Type.ARRAY); j.AddField("field3", arr);

arr.list.Add(1); arr.list.Add(2); arr.list.Add(3);

string encodedString = j.print(); </csharp>

Decoding

Decoding is much simpler on the input end, and again, what you do with the JSONObject will vary on a per-project basis. One of the more complicated way to extract the data is with a recursive function, as drafted below. Calling the constructor with a properly formatted JSON string will return the root object (or array) containing all of its children, in one neat reference! The data is in a public ArrayList called list, with a matching key list (called keys!) if the root is an Object. If that's confusing, take a glance over the following code and the print() method in the JSONOBject class. If there is an error in the JSON formatting (or if there's an error with my code!) the debug console will read "improper JSON formatting".


<csharp> string encodedString = "{\"field1\":0.5,\"field2\":\"sampletext\",\"field3\":[1,2,3]}"; JSONObject j = new JSONObject(encodedString); accessData(j); //access data (and print it) void accessData(JSONObject obj){ switch(obj.type){ case JSONObject.Type.OBJECT: for(int i = 0; i < obj.list.Count; i++){ string key = (string)obj.keys[i]; JSONObject j = (JSONObject)obj.list[i]; Debug.Log(key); accessData(j); } break; case JSONObject.Type.ARRAY: foreach(JSONObject j in obj.list){ accessData(j); } break; case JSONObject.Type.STRING: Debug.Log(obj.str); break; case JSONObject.Type.NUMBER: Debug.Log(obj.n); break; case JSONObject.Type.BOOL: Debug.Log(obj.b); break; case JSONObject.Type.NULL: Debug.Log("NULL"); break;

} } </csharp>


The JSONObject class

<csharp>

  1. define READABLE

using UnityEngine; using System.Collections; using System.Collections.Generic;

/*

* http://www.opensource.org/licenses/lgpl-2.1.php
* JSONObject class
* for use with Unity
* Copyright Matt Schoen 2010
*/

public class JSONObject : Nullable { const int MAX_DEPTH = 1000; public enum Type { NULL, STRING, NUMBER, OBJECT, ARRAY, BOOL } public JSONObject parent; public Type type = Type.NULL; public ArrayList list = new ArrayList(); public ArrayList keys = new ArrayList(); public string str; public double n; public bool b;

public static JSONObject nullJO { get { return new JSONObject(JSONObject.Type.NULL); } } public static JSONObject obj { get { return new JSONObject(JSONObject.Type.OBJECT); } } public static JSONObject arr { get { return new JSONObject(JSONObject.Type.ARRAY); } }

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 JSONObject(bool b) { type = Type.BOOL; this.b = b; } public JSONObject(float f) { type = Type.NUMBER; this.n = f; } public JSONObject() { } public JSONObject(string str) { //create a new JSONObject from a string (this will also create any children, and parse the whole string) //Debug.Log(str); if(str != null) {

  1. 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("\\", "");

  1. 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; Debug.LogWarning("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 { Debug.Log(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] == '}') 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 } } public void AddField(string name, bool val) { AddField(name, new JSONObject(val)); } public void AddField(string name, float val) { AddField(name, new JSONObject(val)); } public void AddField(string name, int val) { AddField(name, new JSONObject(val)); } public void AddField(string name, string val) { AddField(name, new JSONObject { type = JSONObject.Type.STRING, str = val }); } 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 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 SetField(string name, JSONObject obj) { if(HasField(name)) { list.Remove(this[name]); keys.Remove(name); } AddField(name, obj); } public JSONObject GetField(string name) { if(type == JSONObject.Type.OBJECT) for(int i = 0; i < keys.Count; i++) if((string)keys[i] == name) return (JSONObject)list[i]; return null; } public bool HasField(string name) { if(type == JSONObject.Type.OBJECT) for(int i = 0; i < keys.Count; i++) if((string)keys[i] == name) return true; return false; } public void Clear() { type = JSONObject.Type.NULL; list.Clear(); keys.Clear(); str = ""; n = 0; b = false; } public JSONObject Copy() { return new JSONObject(print()); } 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 string print() { return print(0); } 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 = "{";

  1. if(READABLE) //for a bit more readability, comment the define above to save space

str += "\n"; depth++;

  1. endif

for(int i = 0; i < list.Count; i++) { string key = (string)keys[i]; JSONObject obj = (JSONObject)list[i]; if(obj) {

  1. if(READABLE)

for(int j = 0; j < depth; j++) str += "\t"; //for a bit more readability, comment back out for it to work :)

  1. endif

str += "\"" + key + "\":"; str += obj.print(depth) + ",";

  1. if(READABLE)

str += "\n";

  1. endif

} }

  1. if(READABLE)

str = str.Substring(0, str.Length - 1);

  1. endif

str = str.Substring(0, str.Length - 1); str += "}"; } else str += "null"; break; case JSONObject.Type.ARRAY: if(list.Count > 0) { str = "[";

  1. if(READABLE)

str += "\n"; //for a bit more readability, comment back out for it to work :) depth++;

  1. endif

foreach(JSONObject obj in list) { if(obj) {

  1. if(READABLE)

for(int j = 0; j < depth; j++) str += "\t"; //for a bit more readability, comment back out for it to work :)

  1. endif

str += obj.print(depth) + ",";

  1. if(READABLE)

str += "\n"; //for a bit more readability, comment back out for it to work :)

  1. endif

} }

  1. if(READABLE)

str = str.Substring(0, str.Length - 1);

  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; } public JSONObject this[int index] { get { return (JSONObject)list[index]; } } public JSONObject this[string index] { get { return GetField(index); } } public override string ToString() { return base.ToString() + print(); } 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: Debug.LogWarning("Omitting object: " + (string)keys[i] + " in dictionary conversion"); break; } } return result; } else Debug.LogWarning("Tried to turn non-Object JSONObject into a dictionary"); return null; } } </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox