I'm new to the Unity3d and C#. I want to store some data in JSON files, so they are easy to edit in text editor.
So far I used JsonUtility because its build in Unity, and it worked great. Possibility to use it as easy as:
string json = JsonUtility.ToJson(foo);
Foo foo = JsonUtility.FromJson<Foo>(json);
was great for me.
But now, I added some Dictionaries. They are later converted for more afficient data structures, but I want to serialize them to JSON as named objects.
Here is sample of my code. The Objects structure in my case is set and known. Single Dictionary keeps one type of objects, and Dictionary is set deeper in hierarchy of serializable objects.
[System.Serializable]
public struct Bar{
public int x;
public int y;
}
[System.Serializable]
public class Foo{
public int otherData = 43;
public Dictionary<string,Bar> bar = new Dictionary<string,Bar>(){
{"spaceship", new Bar{x = 7, y = 11}},
{"pirates", new Bar{x = 5, y = -3}},
};
}
What I want to achieve:
{
"otherData": 43,
"bar": {
"spaceship": {"x": 7, "y": 11},
"pirates": {"x": 5, "y": -3}
}
}
Is it any way to make that work?
Maybe is a way that I can write my on serialization/deserialization methods for Dictionary?