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?

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();