Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Cannot deserialize object with GSON lib

When I try to deserialize my serialized object with Gson, I get the weird error below.

The error happened in this part of my code:

Log.i("read_json:", jsonSerializedString);

Gladiator gladiator = gson.fromJson(jsonSerializedString, Gladiator.class);

In the log I can see my JSON Object but somehow I cannot deserialize it to an instance of the Gladiator class.

My serialized object is a large String (12.000 characters). Could that be a problem?

JNI DETECTED ERROR IN APPLICATION: can't make objects of type com.adamvarhegyi.duelsofcodrer.model.clazzes.base.Gladiator: 0x12db3d30
    in call to AllocObject
    from java.lang.Object sun.misc.Unsafe.allocateInstance(java.lang.Class) "main" prio=5 tid=1 Runnable
    | group="main" sCount=0 dsCount=0 obj=0x738392a0 self=0xb4d36500
    | sysTid=2014 nice=0 cgrp=default sched=0/0 handle=0xb6f0db34
    | state=R schedstat=( 401798791 103148234 510 ) utm=32 stm=8 core=0 HZ=100
    | stack=0xbe4dd000-0xbe4df000 stackSize=8MB
    | held mutexes= "mutator lock"(shared held)
    native: #00 pc 00370e01  /system/lib/libart.so (art::DumpNativeStack(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, int, char const*, art::ArtMethod*, void*)+160)
    native: #01 pc 0035046f  /system/lib/libart.so (art::Thread::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const+150)
    native: #02 pc 0025a725  /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+740)
    native: #03 pc 0025adfd  /system/lib/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, std::__va_list)+64)
    native: #04 pc 000fd1d1  /system/lib/libart.so (art::ScopedCheck::AbortF(char const*, ...)+32)
    native: #05 pc 00108349  /system/lib/libart.so (art::CheckJNI::AllocObject(_JNIEnv*,    _jclass*)+584)
    native: #06 pc 00254cb5  /data/dalvik-cache/arm/system@[email protected] (Java_sun_misc_Unsafe_allocateInstance__Ljava_lang_Class_2+96)
  at sun.misc.Unsafe.allocateInstance(Native method)
  at java.lang.reflect.Method.invoke!(Native method)
  at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
  at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:223) 
  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:206)
  at com.google.gson.Gson.fromJson(Gson.java:879)
  at com.google.gson.Gson.fromJson(Gson.java:844)
  at com.google.gson.Gson.fromJson(Gson.java:793)
  at com.google.gson.Gson.fromJson(Gson.java:765)
  at com.adamvarhegyi.duelsofcodrer.storage.InternalStorage.newReadMethod(InternalStorage.java:40)
  at com.adamvarhegyi.duelsofcodrer.storage.InternalStorage.readObject(InternalStorage.java:87)
  at com.adamvarhegyi.duelsofcodrer.storage.GladiatorsDAO.getPlayer(GladiatorsDAO.java:34)
  at com.adamvarhegyi.duelsofcodrer.fragment.etc.character.CharacterSheetFragment.onCreateView(CharacterSheetFragment.java:44)

Upvotes: 2

Views: 1294

Answers (1)

Floern
Floern

Reputation: 33904

This error can occur if you try to instantiate an abstract class or an interface. In your example that would be the case with the Gladiator class. Gson can only deserialize instances of a concrete class.

If you really need to use an abstract class, you may want to use a custom TypeAdapter or JsonDeserializer to deserialize your class instances.

Upvotes: 3

Related Questions