'Unity C# JsonUtility is not serializing a list

I've got some data I need to serialize/deserialize, but JsonUtility is just not doing what it's supposed to. Here's the objects I'm working with:

public class SpriteData {
    public string sprite_name;
    public Vector2 sprite_size;
    public List<Vector2> subimage;
}

public class SpriteDataCollection
{
    public SpriteData[] sprites;
}

If I create a SpriteDataCollection, and attempt to serialize it with JsonUtility, I just get an empty object {}. Here's how it's being built:

            SpriteData data = new SpriteData();
            data.sprite_name = "idle";
            data.sprite_size = new Vector2(64.0f, 64.0f);
            data.subimage = new List<Vector2> { new Vector2(0.0f, 0.0f) };

            SpriteDataCollection col = new SpriteDataCollection();
            col.sprites = new SpriteData[] { data };

            Debug.Log(JsonUtility.ToJson(col));

The debug log only prints "{}". Why isn't it serializing anything? I've tested it out, and serializing a single SpriteData does exactly what it's supposed to do, but it won't work in the SpriteDataCollection.



Solution 1:[1]

One other cause Programmer didn't mention but is a big stickler:

[Serializable]
public struct MyObject
{
    public string someField;
}

[Serializable]
public class MyCollection
{
    public readonly List<MyObject> myObjects = new List<MyObject>();
    //     ^-- BAD
}

A list won't serialize if it is using readonly. E.g.

MyCollection collection = new MyCollection();
collection.myObjects.Add(new MyObject {someField = "Test"});
string json = JsonUtility.ToJson(collection);
// {}

Solution 2:[2]

I fixed that by using Newtonsoft instead of JsonUtility as a converter.

here is an example

//here we will serialise or deserialize the data

public void SaveData
{

        List<Model> models = new List<Model>();
        //just adding some data
        models.Add(new Model { playerHealth = 10, saveTime = DateTime.Now, strength = 50 });

        //here pasing the data that are stoed on that variable into a json format
        string jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(models);
        Debug.Log(jsonText);
}

//this is the model

public class Model
{
    public DateTime saveTime;

    public int playerHealth;

    public int strength;

}

here are result enter image description here

Solution 3:[3]

Unity3D has an old-fashioned Json library. You can use the Newtonsoft.Json, a standard JSON library in .NET ecosystem. However, it doesn't support Unity3D and you need to get the specialized package for Unity3D. You can get it from its Github or from the Unity Asset Store.

using Newtonsoft.Json;
    ...
    ...

The_Object the_object = new The_Object(...);

....

string jsonString = JsonConvert.SerializeObject(the_object);

The_Oobject deserialized_object = JsonConvert.DeserializeObject<The_Object>(jsonString);

Please note that SerializableAttribute should be applied on The_Object class and any used object type inside it. Also, you have to indicate variables that you want to parse by json as public. For example:

[Serializable]
class first_class
{
    public second_class sceond = new second_class();
    public System.Collections.Generic.List<List<third_class>> third_nested_list;
    //....
}
[Serializable]
class second_class
{
    public third_class third = new third_class();
    //....
}
[Serializable]
class third_class
{
    public string name;

    //....
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yes Barry
Solution 2 Indrit Vaka
Solution 3