- Home /
How to serialize parameters from JSON?
I'm trying to figure out how I can deserialize some JSON and get it to be usable within Unity. I'm using JSONUtility for the JSON Serialization.
{"id":1, "nodes":[{"code":101,"indent":0,"parameters":["FacePic1",0,0,2]},{"code":401,"indent":0,"parameters":["This is a message test"]},{"code":0,"indent":0,"parameters":[]}],"name":"","switchId":1,"trigger":0},
I know I need to create a class for the object (Which will be my Event class) and then a 'nodes' object to hold the data in 'nodes'. I've already done this for another class... however, how would I tackle the 'parameters' of the nodes object? This varies depending on the code of 'nodes'? For example, code 101 has a string and 3 ints, but code 401 only has a string?
Answer by Bunny83 · Feb 14, 2021 at 12:40 AM
JsonUtility can not serialize / deserialize the object structure you have presented here. It's valid json (without the final comma) but not supported by Unity's JsonUtility.
So you have to pick an alternative json framework. Quite commonly used is the Newtonsoft Json.Net library. It can be used to deserialize your parameters array into an object array. Though that means you have to manually check for the types and cast to the right types.
An alternative may be my own SimpleJSON framework. It's not an object mapper like JsonUtility or the Json.Net library. Instead it just parses the json representation into a set of custom classes to represent the various json data types. There are implicit type convertion operators which allow you to essentially directly use a JSONNode as the actual primitive type you want.
So assuming the trailing comma does not exist, you can parse your json just like this:
JSONNode root = JSON.Parse(yourJsonText);
int id = root["id"];
string name = root["name"];
int switchId = root["switchId"];
int trigger = root["trigger"];
foreachJSONNode node in (root["nodes"])
{
int code = node["code"];
int indent = node["indent"];
foreach(JSONNode param in node["parameters"])
{
// do something with each parameter
}
}
Of course you don't have to parse out everything. You could keep the parameter array as a JSONNode if you like in case you need them later.
Note that even though SimpleJSON is just a single file, it's still modular. All classes are declared partial which makes it possible to directly extend almost every aspect with an extention file. There are some extention files in the repository. One that adds support for some common Unity primitive types like vectors, colors, quaternion, ... and another one for some additional common .NET types.
Of course under the hood json itself can only represent "numbers" (usually double precision), boolean values (true / false), strings, null and the two different collection types Array and Object. Those are represented by the types JSONNumber, JSONBool, JSONString, JSONNull, JSONArray and JSONObject which are all derived from the JSONNode base class. You can implement your own conversion operators for your own types if you really need them very often.
Of course it depends on your needs and how you're going to use this data in the end.
That's what I needed to know. Thanks :)
I'll look for an alternative way to handle the JSON.
Note, if the "code" represents some sort of "class type indicator" you can of course implement a proper polymorphic conversion. So imagine you have those classes:
public class Node
{
public int code;
public int indent;
}
public Node101 : Node
{
public string pString;
public int pInt0,
public int pInt1,
public int pInt2,
}
public Node401 : Node
{
public string pString;
}
public class $$anonymous$$yObject
{
public int id;
public string name;
public int switchId;
public int trigger;
public List<Node> nodes = new List<Node>();
}
You can add an extension like this:
This would allow you to implicitly "convert" a "$$anonymous$$yObject" to a JSONNode and vice versa.
Though often it might be better to implement the polymorphic handling of the different types inside the types themselfs by using two virtual methods. That way you only need some sort of factory to create the right C# type at deserialization and the actual deserialization would be done by the class itself.
This is the route I'm going to go for. I have my parent class EventNode, then I'm going to create $$anonymous$$essageNode, ChoiceNode... etc.
Then my Event class will be similar to your $$anonymous$$yObject, with a list of 'EventNode' :)