- Home /
Json wrapped array deserialization
I've been trying to deserialize the following Json file using the wrapper method from this answer: Serialize and Deserialize Json and Json Array in Unity. Deserializing the base objects and their normal variables into a list of objects works fine, but I can't figure out how to get the "tags" array into a normal string array within the deserialized game object, since it will have a variable number of indices.
{
"Items" : [
{
"ID": 0,
"title": "Mage Hand",
"description": "A simple spell used to manipulate objects through telekinesis.",
"texture": "/Textures/Cards/Spells/ArcaneSpell01.mat",
"type" : "SPELL",
"tags": [
{"primary" : "arcane"} ,
{"primary_strength" : "1"} ,
{"secondary" : "force"} ,
{"secondary_strength" : "1"}
]
},
{
"ID": 1,
"title": "A Colleague",
"description": "Another practicioner of the arcane arts. Perhaps they know something useful?",
"texture": "/Textures/Cards/Contacts/Wizard01.mat",
"type" : "CREATURE",
"tags": [
{"primary" : "arcane"} ,
{"primary_strength" : "1"} ,
{"secondary" : "relationship"} ,
{"secondary_strength" : "1"}
]
}
]
}
Any help is greatly appreciated :)
Answer by Bunny83 · Aug 23, 2019 at 10:31 PM
The objects in your tags array are not the same kind of objects since the variable / field names are different. This makes it difficult / impossible to actually map the json objects to C# objects. Since json does not contain any type information for objects, deserialization can not differentiate between different class types.
It might be easier to not map them to specific C# classes but to use a generic json library like my SimpleJSON library. It can handle any kind of json structure. It provides a simple hierachical structure to access any information stored in the json text.
If you don't want to use such a library you might want to rethink about the json structure. A tag object might look like this:
{
"name":"primary",
"value":"arcane"
}
instead of
{
"primary" : "arcane"
}
This way you can model any kind of "tags" and use the same C# class for all tags. If you only have those 4 tags you probably don't want to use an array at all and use a tags class. For those 4 tags I would probably do something like:
"tags": {
"primary": {
"type": "arcane",
"strength": "1"
},
"secondary": {
"type": "force",
"strength": "1"
}
}
Though since we don't know the exact usecase we can't decide for you.
Thank you very much for the clarification! The only thing I'm worried about with using a tag object is that I'm expecting to have a variable number of tags for each item. I can understand having a tag object that has the variables "type" and "strength", but I'm still unclear how to parse my individual items as having a variable number of tag objects within them without using an array. Is that something that is even possible without using a supplementary library such as the one you provided?
No, that's not possible without such a library since Unity's json serializer is only an object mapper. Objects in C# are statically defined and you can not add / remove variables / fields at runtime.
Apart from that Unity does not support inheritance / polymorphism for sub classes since there's no type information inside the json data so the deserializer does not know which object it should use to represent a certain object. Sub classes are always created based on the field type and therefore an array can only contain one type of objects.