SalomonBrys / Kotson

Kotlin bindings for JSON manipulation via Gson
MIT License
709 stars 37 forks source link

Strange crash #18

Closed fab1an closed 8 years ago

fab1an commented 8 years ago

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).

SalomonBrys commented 8 years ago

I have no idea.... Never encountered this. There's no JNI neither in kotson nor in Gson, so this is a JVM bug...

fab1an commented 8 years ago

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?

SalomonBrys commented 8 years ago

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."

fab1an commented 8 years ago

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: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x6f0095 in tid 29300 (fab1ans.app)

fab1an commented 8 years ago

I reposted this into kotlin discussion forums: https://discuss.kotlinlang.org/t/jvm-crash-on-android-when-using-kotlin-kotson-gson-library/1677

SalomonBrys commented 8 years ago

I've investigated based on the code you gave, and this is a Gson / Android issue. It is not related to Kotlin or Kotson (I've replicated the same crash with only java code).

It is because Gson tries to instanciate an interface, (in this case, TestInterface). While the JVM would throw an exception, Android juste crashes... It appears the issue has been fixed, and will be part of Gson's next release :)

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.

SalomonBrys commented 8 years ago

Great news ! Gson 2.7 is out with the aforementioned fix and can be used with Kotson 2.3.0 :)