使用Gson无法将对象(使用Room DB)序列化为json字符串

时间:2018-09-05 19:01:01

标签: java android gson android-room

我有一个Item类(见下文),其中使用了一些Room db注释。它还有一个名为ItemInfo的嵌套类。这两个类都有一个空的构造函数。

问题是当我尝试序列化Item类的对象时,应用程序崩溃并出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.android.carrymates, PID: 18526
              java.lang.SecurityException: Can not make a java.lang.reflect.Method constructor accessible
                  at java.lang.reflect.AccessibleObject.setAccessible0(AccessibleObject.java:133)
                  at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:119)
                  at com.google.gson.internal.reflect.PreJava9ReflectionAccessor.makeAccessible(PreJava9ReflectionAccessor.java:31)
                  at com.google.gson.internal.ConstructorConstructor.newDefaultConstructor(ConstructorConstructor.java:103)
                  at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:85)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:101)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
                  at com.google.gson.Gson.getAdapter(Gson.java:458)
                  at com.google.gson.Gson.toJson(Gson.java:696)
                  at com.google.gson.Gson.toJson(Gson.java:683)
                  at com.google.gson.Gson.toJson(Gson.java:638)
                  at com.google.gson.Gson.toJson(Gson.java:618)
                  ... more log (irrelevant to question asked)

Item.java

@Entity(tableName = "items", indices = {@Index(value = {"id"}, unique = true), @Index(value = {"owner", "type"})})
public class Item {

    @PrimaryKey
    @NonNull
    String id="";

   //...rest fields are int, boolean and String only

    @Embedded
    ItemInfo itemInfo; // see ItemInfo


    public Item() {

   }


    // ...getters and setters

    @IgnoreExtraProperties
    public static class ItemInfo {

        //...fields are int, boolean and String only

        public ItemInfo() {

        }

        //...getters and setters
    }
}

我的猜测是,Room DB批注将添加至少一个类型为java.lang.reflect.Method的对象,而Gson无法序列化该对象。

下面是我用来将Item对象序列化为json字符串的代码,其中item是Item类的对象,其字段类型为StringItemInfo的字段为非空

Gson gson = new Gson();
String result = gson.toJson(item); // crash begins from here

我该如何解决这个问题?我希望至少有一种解决方法。

4 个答案:

答案 0 :(得分:0)

我建议您将不同的对象用于不同的目标(存储在Room数据库中并序列化为json)。

您需要具有 Item 实体的界面:

public interface Item {

    int getId();

    //other fields
}

然后,您需要一个Room数据库实体的特定实现。您实际上已经拥有的东西,但是需要进行相同的重构:

@Entity(tableName = "items", indices = {@Index(value = {"id"}, unique = true), @Index(value = {"owner", "type"})})
public class RoomItem implements Item {

    @PrimaryKey
    @NonNull
    private int id;

    //other fields

    public RoomItem() {
    }

    public RoomItem(Item item) {
        id = item.getId();
    }

    @Override
    public int getId() {
        return 0;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    //other getters and setters
}

此外,您需要摆脱内部静态类 ItemInfo ,并将其放在单独的.java文件中。

最后,您需要针对Gson实体的特定实现:

public class GsonItem implements Item {

    private final int id;

    public GsonItem(Item origin) {
        id = origin.getId();
    }

    @Override
    public int getId() {
        return id;
    }
}

在这种情况下,您可以像这样使用它而不会出现任何问题:

Gson gson = new Gson();
String result = gson.toJson(new GsonItem(item));

是的,这种方法会使您编写更多代码,但是缺少像您这样的意外问题肯定会花费很多精力!

答案 1 :(得分:0)

请尝试以下操作:这段代码已正确插入

import android.arch.persistence.room.Embedded;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;

import com.google.firebase.database.IgnoreExtraProperties;

@Entity(tableName = "items", indices = {@Index(value = {"id"}, unique = true), @Index(value = {"owner", "type"})})
public class Item {

    @PrimaryKey
    @NonNull
    String id="";

    //...rest fields are int, boolean and String only

    @Embedded
    ItemInfo itemInfo; // see ItemInfo


    public Item() {

    }


    // ...getters and setters

    @IgnoreExtraProperties
    public static class ItemInfo {

        //...fields are int, boolean and String only

        public ItemInfo() {

        }

        int prop1;
        String id="";
        //...getters and setters
    }
}

请注意也要删除依赖项

implementation 'com.google.firebase:firebase-core:16.0.3'
implementation "com.google.firebase:firebase-database:16.0.1"

// Arch
implementation "android.arch.core:runtime:1.1.1"
implementation "android.arch.core:common:1.1.1"

implementation 'android.arch.persistence.room:runtime:1.1.1';
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1';

和实现:

        Item item = new Item();

        item.id = "Rover";
        item.itemInfo = new Item.ItemInfo();
        item.itemInfo.id = "asd";
        item.itemInfo.prop1 = 1;

        Gson gson = new Gson();

        String json = gson.toJson(item); // here json ={"id":"Rover","itemInfo":{"id":"asd","prop1":1}}

答案 2 :(得分:0)

请勿嵌套这些类,以使用@Embedded批注:

@Entity(
    tableName = "items",
    indices = {
        @Index(value = {"id"}, unique = true), 
        @Index(value = {"owner", "type"})
    }
)
public class Item {

    @PrimaryKey
    @ColumnInfo(name = "id")
    String id = null;

    @Embedded
    ItemInfo itemInfo;

    public Item() {

    }
}

@Entity(tableName = "item_info")
public class ItemInfo {

    public ItemInfo() {

    }

    ...
}

另请参阅此answer,涉及GSON的排除策略(itemInfo可能需要)

或直接将这些字段直接添加到类Item中,以便一次全部序列化它们-

为了不增加不必要的复杂性,只会造成问题。

答案 3 :(得分:0)

免责声明:

我不知道 RoomDB @Entity类有什么作用(尽管看起来RoomDB使用子类而不是您编写的类)。

我也在JVM上运行测试

但是我建议您使用@Expose

public class GsonTest {

  private static class SampleModel {
    @Expose
    private int i;

    private Method method;

    @Expose
    private Nested nested = new Nested();
  }

  private static class Nested {
    @Expose
    private String a = "my string";
  }

  @Test
  public void failsWithMethodField() throws Exception {
    assertThrows(Exception.class, () -> {
      SampleModel sampleModel = new SampleModel();
      sampleModel.i = 10;
      sampleModel.method = Object.class.getDeclaredMethod("equals", Object.class);
      Gson gson = new Gson();
      gson.toJson(sampleModel);
    });
  }

  @Test
  public void withExposedDoesNotFail() {
    assertDoesNotThrow(() -> {
      SampleModel sampleModel = new SampleModel();
      sampleModel.method = Object.class.getDeclaredMethod("equals", Object.class);
      Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
      String json = gson.toJson(sampleModel);
      System.out.println(json); // {"i":0,"nested":{"a":"my string"}}
    });
  }
}

必不可少的部分是使用Gson选项配置excludeFieldsWithoutExposeAnnotation

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

然后用@Expose注释标记序列化和反序列化时应使用的所有字段。