Because I am working on a CouchDB class ( to be released at a later stage in the Asset Store | Free ) to manage data streams to and from CouchDB databases and Services like Cloudant, I have been searching for a good (and simple) JSON parser and formatter.
Most .NET libs I encountered were a bit buggy, fatty or complex to use.
I encountered and really liked MiniJSON by Darktable ( Calvin Rien | Unity3D: MiniJSON Decodes and encodes simple JSON strings. Not intended for use with massive JSON strings, probably < 32k preferred. Handy for parsing JSON from inside Unity3d. · GitHub ). Its simple and you have total control.
However …
I found that i lacked some functionality, hence the JSON class I present to you …
( and licensed under the open source MIT License )
Download the WyrmTale JSON class unit here : 1214378–49535–$JSON.cs (22.7 KB)
Based on MiniJSON, its got :
- access to JSON as an object instead of a static class
- using class indexers to set data
- supporting some unity types like Vector2, Vector3, Rect, Color, Color and Color32
- simple way to support/extend to you own custom classes
Some usage example code
Formatting JSON from basic and unity types.
JSON js = new JSON();
// ----------------------------------------------
// base types
// string
// ----------------------------------------------
js["myString"] = "This is a string ";
js["myStringArray"] = new string[] {"string value 1","string value 2", "string value 3"};
// int
js["myInt"] = 1234;
js["myIntArray"] = new int[] {1,2,3};
// float
js["myFloat"] = 0.5f;
js["myFloatArray"] = new float[] {1.5f,2.5f,3.5f};
// boolean
js["myBoolean"] = true;
js["myBooleanArray"] = new bool[] {true, false, true};
// ----------------------------------------------
// Supported Unity Types
// ----------------------------------------------
// Vector2
js["myVector2"] = (JSON)(new Vector2(100.5f,123.45f));
js["myVector2Array"] = new JSON[] {
(JSON)(new Vector2(100.5f,123.45f)),
(JSON)(new Vector2(200.5f,234.45f)),
(JSON)(new Vector2(300.5f,345.45f))
};
// Other supported Unity classes
js["myVector3"] = (JSON)Vector3.one;
js["myRect"] = (JSON)(new Rect(10,10,100,200));
js["myColor"] = (JSON)Color.cyan;
js["myColor32"] = (JSON)(new Color32(245,210,155,128));
js["myQuaternion"] = (JSON)Quaternion.identity;
// Get serialized JSON string
string jsonString = js.serialized;
// {
// "myString":"This is a string ",
// "myStringArray":["string value 1","string value 2","string value 3"],
// "myInt":1234,"myIntArray":[1,2,3],
// "myFloat":0.5,
// "myFloatArray":[1.5,2.5,3.5],
// "myBoolean":true,
// "myBooleanArray":[true,false,true],
// "myVector2":{"x":100.5,"y":123.45},
// "myVector2Array":[{"x":100.5,"y":123.45},{"x":200.5,"y":234.45},{"x":300.5,"y":345.45}],
// "myVector3":{"x":1,"y":1,"z":1},
// "myRect":{"left":10,"top":210,"width":100,"height":200},
// "myColor":{"r":0,"g":1,"b":1,"a":1},
// "myColor32":{"r":245,"g":210,"b":155,"a":128},
// "myQuaternion":{"x":0,"y":0,"z":0,"w":1}
// }
Parsing JSON and getting it to your variables
js = new JSON();
// Parse JSON string
js.serialized = jsonString;
// ----------------------------------------------
// base types
// ----------------------------------------------
// get string and string[]
string myString = js.ToString("myString");
string[] myStringArray = js.ToArray<string>("myStringArray");
// get int and int[]
int myInt = js.ToInt("myInt");
int[] myIntArray = js.ToArray<int>("myIntArray");
// get float and float[]
float myFloat = js.ToFloat("myFloat");
float[] myFloatArray = js.ToArray<float>("myFloatArray");
// get bool and bool[]
bool myBoolean = js.ToBoolean("myBoolean");
bool[] myBooleanArray = js.ToArray<bool>("myBooleanArray");
// ----------------------------------------------
// Supported Unity Types
// ----------------------------------------------
// get Vector2 and Vector2[]
Vector2 myVector2 = (Vector2)js.ToJSON("myVector2");
Vector2[] myVector2Array = js.ToArray<Vector2>("myVector2Array");
// get other Unity types
Vector2 myVector3 = (Vector3)js.ToJSON("myVector3");
Rect myRect = (Rect)js.ToJSON("myRect");
Color myColor = (Color)js.ToJSON("myColor");
Color32 myColor32 = (Color32)js.ToJSON("myColor32");
Quaternion myQuaternion = (Quaternion)js.ToJSON("myQuaternion");
By adding 3 methods to your custom classes you can simply support formatting to and parsing from JSON
- static implicit operator to format to JSON
- static explicit operator to parse JSON to you custom class
- static Array() method to convert an JSON[ ] object array to an array with your class elements
The Example class
public class MyClass
{
public GameObject gameObject;
// constructor will create an 'empty' game object
public MyClass(string name, Vector3 position, Vector3 localScale, Quaternion rotation)
{
this.gameObject = new GameObject(name);
this.gameObject.transform.position = position;
this.gameObject.transform.localScale = localScale;
this.gameObject.transform.rotation = rotation;
}
// serialize this class to JSON
public static implicit operator JSON(MyClass value)
{
GameObject g = value.gameObject;
JSON js = new JSON();
if (g!=null)
{
JSON jsTransform = new JSON();
js["name"] = g.name;
js["transform"] = jsTransform;
jsTransform["position"] = (JSON)g.transform.position;
jsTransform["localScale"] = (JSON)g.transform.localScale;
jsTransform["rotation"] = (JSON)g.transform.rotation;
}
return js;
}
// JSON to class conversion
public static explicit operator MyClass(JSON value)
{
checked
{
JSON jsTransform = value.ToJSON("transform");
return new MyClass(
value.ToString("name"),
(Vector3)jsTransform.ToJSON("position"),
(Vector3)jsTransform.ToJSON("localScale"),
(Quaternion)jsTransform.ToJSON("rotation"));
}
}
// convert a JSON array to a MyClass Array
public static MyClass[] Array(JSON[] array)
{
List<MyClass> tc = new List<MyClass>();
for (int i=0; i<array.Length; i++)
tc.Add((MyClass)array[i]);
return tc.ToArray();
}
}
To format this custom class and an array with classes to JSON
js = new JSON();
// ----------------------------------------------
// custom type | MyClass to store gameobject name + transform
// ----------------------------------------------
js["mainObject"] = (JSON)(new MyClass("main",
new Vector3(10,10,10), new Vector3(1,1,1), Quaternion.identity));
js["ObjectArray"] = new JSON[] {
(JSON)(new MyClass("object.1",
new Vector3(20,20,20), new Vector3(2,2,2), Quaternion.identity)),
(JSON)(new MyClass("object.2",
new Vector3(30,30,30), new Vector3(3,3,3), Quaternion.identity)),
(JSON)(new MyClass("object.3",
new Vector3(40,40,40), new Vector3(4,4,4), Quaternion.identity))};
jsonString = js.serialized;
// {
// "mainObject": {
// "name":"main",
// "transform":{
// "position":{"x":10,"y":10,"z":10},
// "localScale":{"x":1,"y":1,"z":1},
// "rotation":{"x":0,"y":0,"z":0,"w":1}
// }
// },
// "ObjectArray":[
// { "name":"object.1",
// "transform":{
// "position":{"x":20,"y":20,"z":20},
// "localScale":{"x":2,"y":2,"z":2},
// "rotation":{"x":0,"y":0,"z":0,"w":1}
// }
// },{ "name":"object.2",
// "transform":{
// "position":{"x":30,"y":30,"z":30},
// "localScale":{"x":3,"y":3,"z":3},
// "rotation":{"x":0,"y":0,"z":0,"w":1}
// }
// },{ "name":"object.3",
// "transform":{
// "position":{"x":40,"y":40,"z":40},
// "localScale":{"x":4,"y":4,"z":4},
// "rotation":{"x":0,"y":0,"z":0,"w":1}
// }
// }]
// }
To parse the JSON string, created above , back to ‘new’ objects ( because MyClass is coded that way )
js = new JSON();
js.serialized = jsonString;
MyClass mc = (MyClass)js.ToJSON("mainObject");
MyClass[] mcArray = MyClass.Array(js.ToArray<JSON>("ObjectArray"));
Download the WyrmTale JSON class unit here : 1214378–49535–$JSON.cs (22.7 KB)
( and licensed under the open source MIT License )
Cheers!