Question by
Shmulzious · Dec 01, 2017 at 09:02 PM ·
serializationjson
List of serializable class objects results in empty json
Hey All,
Looked over a few examples and answers, and can't seem to understand why my code isn't working -
[Serializable]
public class Drink {
public enum DrinkType
{
SHOT, CHUG, SIP, FLAME
}
public string name;
public string imageName;
public DrinkType type;
public int courageGain;
public int intoxicationGain;
}
public class DrinkManager {
[SerializeField]
private List<Drink> _drinks;
public bool Save()
{
string json = EditorJsonUtility.ToJson(_drinks);
FileUtils.SaveStringToPath(json, DRINK_LIST_PATH);
return false;
}
public bool Load()
{
string json = FileUtils.ReadStringFromPath(DRINK_LIST_PATH);
return false;
}
}
in the save function all i get from ToJson is an empty json. Can anyone point me to what I'm missing? Thx :)
Comment
Best Answer
Answer by Shmulzious · Dec 02, 2017 at 12:28 AM
OK i figured it out - i needed to serialize the class instance i.e. call ToJson(this) instead of ToJson(_drinks). It makes sense since if i put the list directly, there is no key to assign the list as a value to.
Your answer
![](https://koobas.hobune.stream/wayback/20220612143753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Serialize gameobject children behaviours 0 Answers
UnityException: Failed to run serialization 0 Answers
[SOLVED] JSON Serialization of Derived Classes 2 Answers
Setting components variables with another component 0 Answers
Nested dictionary serialization 0 Answers