Raymond Gao
04/18/2020, 5:44 PMval foo : Foo = Foo()
var jobj : JSONObject = serialize(foo)
var foo2 : Foo = construct(jobj, Foo::class)
The methods serialize and construct will inspect the object using reflection,
find any fields that match a set of constraints and add their values to a json object
or send them into the appropriate constructor of Foo
to create a new object
It works until I run into Generics
class Foo(val map : Map<String, Int>)
{
...
}
I'm having some difficulty getting the type parameters passed into Map
at runtime.
My friend has told me about the concept of type erasure. It seems that kotlin throws away
these arguments at runtime. I have resorted to annotating the field with the appropriate types
for now, but it causes some ugly duplication of information already typed down.
class Foo(@param:GenericsHint([String::class, Int::class]) val map : Map<String, Int>)
Does anyone have any ideas on how I can get around this problem?
Thanks 🙂Adam Powell
04/18/2020, 6:15 PMRaymond Gao
04/18/2020, 6:15 PMJakub Pi
04/18/2020, 8:23 PM