I am attempting to write a method that takes in an object and will use the Unity JsonUtility or the JsonHelper and returns the serialized version of that object. When I run the below debugging code everything works correctly.
object t = new List<Player>
{
new Player
{
Name = "asdf",
ID = Guid.NewGuid().ToString()
}
};
if (t is IEnumerable<Player> list)
Debug.Log(JsonHelper.ToJson(list.ToArray()));
//Result {"Items":[{"ID":"eff6ca08-b464-4e57-9f2e-f49a0cb869ff","Name":"asdf"}]}
However, my actual method does not return the same thing.
public void SeriailizeObject<T>(T obj)
{
if (obj is IEnumerable<T> list)
{
Debug.Log(JsonHelper.ToJson(list.ToArray()));
//Result {}
}
...
Does anyone know if you can serialize a generic list with Unity's JsonUtility?