无法使用GSON lib

时间:2016-03-20 17:48:04

标签: java android json gson

当我尝试用Gson反序列化我的序列化对象时,我得到了下面的奇怪错误。

错误发生在我的代码的这一部分:

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

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

在日志中我可以看到我的JSON对象但不知何故我无法将其反序列化为Gladiator类的实例。

我的序列化对象是一个大字符串(12.000个字符)。 这可能是个问题吗?

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@framework@boot.oat (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)

1 个答案:

答案 0 :(得分:3)

如果尝试实例化抽象类或接口,则会发生此错误。在您的示例中,Gladiator类就是这种情况。 Gson只能反序列化具体类的实例。

如果您确实需要使用抽象类,则可能需要使用自定义TypeAdapterJsonDeserializer来反序列化您的类实例。