- Home /
JSON missing arrays
I have this class:
[System.Serializable]
public class TerrainSaveInfo
{
public string name;
public float[][] vertices;
public int[] triangles;
public float[][] uvs;
}
which I am trying to convert to JSON and save to file. Unfortunately, the data that is visible is name and vertices - the last 2 arrays are not converted to JSON at all.
Why, what am I doing wrong?
Are the other 2 fields missing or what, how do they appear in your file / json string?
Answer by Bunny83 · Sep 26, 2019 at 03:49 PM
I think you should only see the "name" and the "triangles" array since the other two arrays can not be serialized with Unity's JsonUtility. Nested / jagged arrays or multidimensional arrays are not suppored by Unity's serializer and the JsonUtility has the same limitations as it's build on top of it. What you can do is use this:
[System.Serializable]
public class TerrainSaveInfo
{
public string name;
public Vector3[] vertices;
public int[] triangles;
public Vector2[] uvs;
}
Of course this will store each vertex / uv as an object with x,y,z components instead of a nested array. If you want to have this specific format, you could use my SimpleJSON framework and in addition the Unity extension. It does not directly serialize the data into your own custom class, but it provides easy access and easy creation of json text.
By default the "VectorContainerType" is set to "array", so it will create a JSONArray when you store a Vector3. With SimpleJSON you can do:
JSONNode data = new JSONObject();
data["name"] = yourName;
var triangles = data["triangles"].AsArray();
foreach(Vector3 v in yourVertices)
triangles.Add(v);
// the same for the other array
// finally
string text = data.ToString();
Thanks it helped! I don't have to play swapping float[][] for Vector3 now.
Now I have to think about how to reduce the file size because 1 file with my terrain (chunk) takes almost 300kb - it's a bit too much.
If you want to reduce the file size, using a text format isn't the best solution ^^. Json usually uses double precision floating point numbers. Those expressed as text easily take up 4-6 times the space a float in binary form would take. If you're just looking for a compact way to store a mesh, you might want to have a look at my generic $$anonymous$$eshSerializer. It stores the mesh data in a compact binary format. It only stores things that are present in the mesh.
If you want to keep the text format for some reason, you might just want to compress it. Though it would require some compression library.
Looking at your code, I have the impression that I'm just a beginner in program$$anonymous$$g. I will stay with the second option with compression.
After all, many thanks, you helped a lot :)
Your answer
Follow this Question
Related Questions
Cannot edit values of Class when created using .JSON file 1 Answer
Creating a class wide array (c#) 1 Answer
Adding scripted variables to an array 1 Answer
Losing array in class after Array.ToBuiltin? 1 Answer
Prepopulate Class Array 0 Answers