Doesn't JsonUtility support arrays with abstract type?

The “new” class JsonUtility provides easy-to-use JSON (de-)serialization. In this “early” state, it doesn’t support arrays as type to directly serialize. However, there is a workaround using wrapper classes (as seen in this post on StackOverflow).
In my project, I have an array of objects that extend an abstract class, but JsonUtility doesn’t seem to do anything with it.

using System;
using UnityEngine;

public class JsonTest : MonoBehaviour {
	void Start () {
        // The array that contains elements based on AbstractBase
        AbstractBase[] array = new AbstractBase[]
            {
                new SubOne(),
                new SubTwo()
            };

        // Print one element
        string json = JsonUtility.ToJson(array[0]);
        Debug.Log(json);

        // Print array
        json = JsonUtility.ToJson(array);
        Debug.Log(json);

        // Print random array (that does not have an abstract class as it's type) using wrapper
        ArrayWrapper<string> stringWrapper = new ArrayWrapper<string>();
        stringWrapper.items = new string[] { "one", "two"};
        json = JsonUtility.ToJson(stringWrapper);
        Debug.Log(json);

        // Print abstract array using wrapper
        ArrayWrapper<AbstractBase> abstractWrapper = new ArrayWrapper<AbstractBase>();
        abstractWrapper.items = array;
        json = JsonUtility.ToJson(abstractWrapper);
        Debug.Log(json);
    }
}

public class ArrayWrapper<T>
{
    public T[] items;
}

[Serializable]
public abstract class AbstractBase
{
    public string name;
}

[Serializable]
public class SubOne : AbstractBase
{
    public int someImportantInt = 2;
}

[Serializable]
public class SubTwo : AbstractBase
{
    public string someImportantString = "I'm important!";
}

The log prints the following json-strings:

{"name":"","someImportantInt":2}
{}
{"items":["one","two"]}
{}

This points out that JsonUtility doesn’t serialize bare arrays (as seen in line 2) and arrays with an abstract type.

So, is this a bug or does anyone know a workaround for this?

The JsonUtility class has the same restrictions as Unity’s serialization system. That means no support for polymorphism. Objects are deserialized based on the reference type. The JSON representation doesn’t contain any type information.

Imagine this example:

[Serializable]
public class Base
{
    public int test;
}

[Serializable]
public class ClassA : Base
{
    public string additionalData;
}

[Serializable]
public class ClassB : Base
{
    public string additionalData;
}

[Serializable]
public class Wrapper
{
    public Base[] instances;
}

var data = new Wrapper();
data.instances = new Base[3];
data.instances[0] = new ClassA(){test = 42, additionalData = "foobar"};
data.instances[1] = new ClassB(){test = 0, additionalData = "some data"};
data.instances[2] = new Base(){test = 2};

An example JSON representation would be:

{
    "instances" : [
        { "test" : 42, "additionalData" : "foobar" },
        { "test" : 0, "additionalData" : "some data" },
        { "test" : 2 },
    ]
}

As you can see the deserializer can’t determine the concrete type of the instances inside the array. Unity’s serialization system (as well as the JsonUtility class) deserializes the objects based on the variable type. That means after deserialization the Wrapper class would contain 3 Base class instances.

In order to correctly deserialize polymorphic object graphs, the JSON representation would need additional meta data about the actual types involved which is not supported.