Gson Deserialization woes using objects and delegates


#1

I have this construct, which I want to deserialize from Json (using Gson). Gson complains, rightfully so, that it doesn’t know how to instantiate the abstract class;

abstract class Filter<in T, E> {

    // ~ Abstract  ----------------------------------------------------

    abstract fun getData(obj: T): E

    // ~ Properties ---------------------------------------------------

    val selection = mutableSetOf<E>()

    // ~ Methods -----------------------------------------------------

    fun matches(obj: T) =
        getData(obj) in selection
}


class MyList() {

    // ~ Delegated Properties ---------------------------------------

    val filter = object : Filter<Any, String>() {
        override fun getData(obj: Any) =
            obj.toString()
    }
}

I guess it is because the object : Filter statement doesn’t create an actual type which Gson can reflect out of the MyList class and fill.

But actually all the info is there to unambiguously construct an instance from Json.

Is there a way to annotate the object: somehow?

Kind Regards
Fabian