JsonUtility.FromJson() return null.

Hi,
I need to deserialize an array, but JsonUtility.FromJson return null while Json.Net can actually do this job. Is that a bug?

Snapshot
2533782--175896--results compare.jpg

Wrapper

using UnityEngine;
using System;
using Newtonsoft.Json;

public static class JsonWrapper
{
    public static T[] FromJson<T>(string json)
    {     
        if (json.StartsWith("[")) //top level array, surround it with {"Items": }
        {
            json = json.Insert(0, "{\"Items\":");
            json = json.Insert(json.Length, "}");
        }     

        Wrapper<T> jsonDotNet= JsonConvert.DeserializeObject<Wrapper<T>>(json);
        Wrapper<T> unityJson = JsonUtility.FromJson<Wrapper<T>>(json);

        return jsonDotNet.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return JsonUtility.ToJson(wrapper);
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }
}

JSON data

[
  {
    "_id": "56c6dfed2622525499abe1e4",
    "user_id": "56c593385a007650c4f1c6d0",
    "description": "Super heroes",
    "download": 8,
    "upvote": 0,
    "downvote": 1,
    "updated_at": "2016-02-19T09:27:09.039Z",
    "__v": 0,
    "names": [
      ""
    ],
    "tags": [
      "ironman",
      "superman",
      "batman",
      "hulk"
    ]
  }
]

Interesting. Are you sure it’s the array that JsonUtility is having trouble with or is it the generic object? You’re actually deserializing a generic object with an array of T. Maybe the built in JsonUtility doesn’t support generic objects. I’m not very familiar with it but I know it’s very limited in its serialization and deserialization capabilities. I don’t see any issues with your json string itself.

Edit, as an aside, instead of wrapping your Json string in JsonDotNet, you could also do:

var wrapper = new Wrapper<ArrayType>();
wrapper.Items = JsonConvert.DeserializeObject<ArrayType>(json);

You could do the same with the JsonUtility. You could actually even expand your Wrapper class with a static method like so:

public static Wrapper<T> FromJson(json)
{
     return new Wrapper<T> { Items = JsonConvert.DeserializeObject<T>(json) };
}

Then you wouldn’t lose your dynamic utility method approach. Of course here, T would be an array type rather than a base item type.

1 Like

JsonUtility supports generic object as long as it’s [System.Serializable]. The issue may relate to top-level array, since I found this How to load an array with JsonUtility? - Unity Engine - Unity Discussions .
Your asset works great, I’ve been used it for years, and now it seems will be even longer :slight_smile:

2 Likes

Thanks! I wasn’t trying to persuade you otherwise. The JsonUtility is native and certainly the fastest and most gc friendly solution when it works! It’s just far more limited.

I know this is super late but here’s a workaround to serialize generic classes. What you can do is wrap a class around your generic as in:

SomeOtherClass : SomeClass<int>

SomeClass does not serialize but SomeOtherClass does.

Make your wrapper class a concrete class