Java 无法使用Gson将对象(使用Room DB)序列化为json字符串
我有一个类项(见下文),其中使用了一些roomdb注释。它还有一个名为ItemInfo的嵌套类。这两个类都有一个空构造函数 问题是,当我尝试序列化Item类的对象时,应用程序崩溃,出现以下错误:Java 无法使用Gson将对象(使用Room DB)序列化为json字符串,java,android,gson,android-room,Java,Android,Gson,Android Room,我有一个类项(见下文),其中使用了一些roomdb注释。它还有一个名为ItemInfo的嵌套类。这两个类都有一个空构造函数 问题是,当我尝试序列化Item类的对象时,应用程序崩溃,出现以下错误: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.android.carrymates, PID: 18526 java.lang.SecurityException: Can not mak
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注释正在添加至少一个Gson无法序列化的java.lang.reflect.Method
类型的对象
下面是我用来将Item对象序列化为json字符串的代码,其中Item
是类Item的对象,其类型为string
和ItemInfo
的字段值为非空
Gson gson = new Gson();
String result = gson.toJson(item); // crash begins from here
我如何解决这个问题?我希望至少有一个变通解决方案。通常,当我尝试此方法时,会得到输出
public class Car {
public String brand = null;
public int doors = 0;
}
Car car = new Car();
car.brand = "Rover";
car.doors = 5;
Gson gson = new Gson();
String json = gson.toJson(car);
签出此:
我可以建议您为不同的目标使用不同的对象(存储在
房间
db中,并序列化为json
)
您需要有一个项目的接口实体:
public interface Item {
int getId();
//other fields
}
然后,您需要为Room
db实体提供一个特定的实现。您实际上已经拥有的,但需要进行相同的重构:
@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));
是的,这种方法会让您编写更多的代码,但缺少像您这样的意外问题肯定会付出代价 请试试这个:这个被剪掉的代码工作正常
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}}
不要嵌套这些类,以便使用
@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() {
}
...
}
另请参见此,关于GSON
排除策略(这可能是itemInfo
所需)
或者简单地将这些字段直接添加到类项中
,以便一次将它们全部序列化-
为了避免增加超出要求的复杂性,这只会在将来引起问题。免责声明: 我不知道RoomDB如何处理
@Entity
类(尽管看起来RoomDB使用的是子类而不是您编写的类)
我还在JVM上运行测试
但我可以建议您使用:
基本部分是配置Gson
带有excludefieldswithout exposeannotation
选项:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
然后标记使用
@Expose
注释进行序列化和反序列化时应使用的所有字段。您可以发布序列化代码吗?@olmancarballetez请参阅?类型是什么类型的字段?它真的只是int
、boolean
和String
字段吗?或者其中也有任何类型的Class
?是的,其余字段仅为int
、boolean
和String
。String result=gson.toJson(项目);项中有什么?必须是java对象,例如Car Car=new Car();car.brand=“Rover”;轿厢门=5;Gson Gson=新的Gson();字符串json=gson.toJson(car);
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();