-
Notifications
You must be signed in to change notification settings - Fork 37
Strange crash #18
Comments
I have no idea.... Never encountered this. |
I think it happens because the type of one of my members is an interface, so kotson/gson can't figure out the time. Maybe java tries to instantiate the kotlin interface and crashes? |
I just tried this scenario, and it gives a proper exception RuntimeException : "Unable to invoke no-args constructor for interface com.github.salomonbrys.kotson.Itf. Register an InstanceCreator with Gson for this type may fix this problem." |
Oh, I should have mentioned that I'm running on android. Big sorry. O.o. This crashes: /* MainActivity.java */
@Override
protected void onResume() {
super.onResume();
TestKt.restore(this);
} /* Test.kt */
interface TestInterface {}
class TestClass() : TestInterface {}
data class Test(
val member: TestInterface = TestClass()
)
fun restore(activity: Activity) {
val test = Gson().fromJson<Test>("{\"member\":{}}");
} Gives me: |
I reposted this into kotlin discussion forums: https://discuss.kotlinlang.org/t/jvm-crash-on-android-when-using-kotlin-kotson-gson-library/1677 |
I've investigated based on the code you gave, and this is a Gson / Android issue. It is because Gson tries to instanciate an interface, (in this case, TestInterface). While the JVM would throw an exception, Android juste crashes... However, even once the crash is fixed, it will still throw an issue: if you wish to deserialize an interface, you must register a deserializer! Gson has no way of knowing which concrete type it should create in place of the interface. |
Great news ! Gson |
I'm trying to deserialize a custom data-class with various members.
I just tried
gson.fromJson<MyType>(...)
without any custom deserializers, but I keep getting a strange segfault (libc, not an exception that I can catch).When I call
gson.fromJson<MyType>('{}')
i get the correct type (with defaults set).The text was updated successfully, but these errors were encountered: