杰克逊无法序列化我的领域对象
Jackson cannot serialize an my realm object
我有一个 Route 对象,但发现无法序列化它。
所以我说我会调试并尝试分别从中序列化对象。
这是我的功能:
public JSONObject getRouteJson(Next_Step step) {
JSONObject route = new JSONObject();
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
RouteNonRealm r = new RouteNonRealm(step.getRoute());
String string = mapper.writeValueAsString(r.getDuration());
route.put("duration", string);
string = mapper.writeValueAsString(r.getDistance());
route.put("distance", string);
string = mapper.writeValueAsString(r.getArrival_time());
route.put("arrival_time", string);
string = mapper.writeValueAsString(r.getEnd_location());
route.put("end_location", string);
string = mapper.writeValueAsString(r.getStart_address());
route.put("start_address", string);
string = mapper.writeValueAsString(r.getEnd_address());
route.put("end_address", string);
string = mapper.writeValueAsString(r.getDeparture_time());
route.put("departure_time", string);
string = mapper.writeValueAsString(r.getStart_location());
route.put("start_location", string);
string = mapper.writeValueAsString(r.getSteps());
route.put("steps", string);
string = mapper.writeValueAsString(r.getLegs());
route.put("legs", string);
} catch (Exception e) {
Log.e("route", "Error getting route: " + e.getMessage());
}
return route;
}
这是来自我的对象的变量,它具有 @RealmClass 注释并扩展了 realmObject 并实现了 Serializable:
private Duration duration;
private Distance distance;
private RouteTime arrival_time;
private CoordLocation end_location;
private String start_address;
private String end_address;
private RouteTime departure_time;
private CoordLocation start_location;
private RealmList<Step> steps = new RealmList<Step>();
private RealmList<Leg> legs = new RealmList<Leg>();
这是我的 Duration 对象:
@RealmClass
public class Duration extends RealmObject implements Serializable {
private int value;
private String text;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
现在我在第一个问题上遇到了同样的问题mapper.writeValueAsString:
StatsUserVehicle doesn't have a primary key. (through reference chain: io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy["realm"]->io.realm.Realm["schema"]->io.realm.ImmutableRealmSchema["all"]->java.util.LinkedHashSet[0]->io.realm.ImmutableRealmObjectSchema["primaryKey"])
现在有人可以向我解释一下,为什么我会遇到 StatsUserVehicle 中没有主键的问题?
好的,这个 class 没有主键,因为它不需要主键。但是这个 class 与我尝试序列化的对象没有任何联系。
我该如何解决这个问题?
我正在使用:
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.2.7'
implementation(
[group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.8'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.8'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8']
)
和领域:
classpath "io.realm:realm-gradle-plugin:5.9.0"
查看异常消息:
io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy["realm"]
->io.realm.Realm["schema"]
->io.realm.ImmutableRealmSchema["all"]
->java.util.LinkedHashSet[0]
->io.realm.ImmutableRealmObjectSchema["primaryKey"]
您的 class 扩展了 RealmObject
并且在运行时有一个代理 io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy
可以有很多不同的东西。尝试忽略内部 Realm
属性:
@JsonIgnoreProperties({"realm", "schema"})
我有一个 Route 对象,但发现无法序列化它。 所以我说我会调试并尝试分别从中序列化对象。
这是我的功能:
public JSONObject getRouteJson(Next_Step step) {
JSONObject route = new JSONObject();
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
RouteNonRealm r = new RouteNonRealm(step.getRoute());
String string = mapper.writeValueAsString(r.getDuration());
route.put("duration", string);
string = mapper.writeValueAsString(r.getDistance());
route.put("distance", string);
string = mapper.writeValueAsString(r.getArrival_time());
route.put("arrival_time", string);
string = mapper.writeValueAsString(r.getEnd_location());
route.put("end_location", string);
string = mapper.writeValueAsString(r.getStart_address());
route.put("start_address", string);
string = mapper.writeValueAsString(r.getEnd_address());
route.put("end_address", string);
string = mapper.writeValueAsString(r.getDeparture_time());
route.put("departure_time", string);
string = mapper.writeValueAsString(r.getStart_location());
route.put("start_location", string);
string = mapper.writeValueAsString(r.getSteps());
route.put("steps", string);
string = mapper.writeValueAsString(r.getLegs());
route.put("legs", string);
} catch (Exception e) {
Log.e("route", "Error getting route: " + e.getMessage());
}
return route;
}
这是来自我的对象的变量,它具有 @RealmClass 注释并扩展了 realmObject 并实现了 Serializable:
private Duration duration;
private Distance distance;
private RouteTime arrival_time;
private CoordLocation end_location;
private String start_address;
private String end_address;
private RouteTime departure_time;
private CoordLocation start_location;
private RealmList<Step> steps = new RealmList<Step>();
private RealmList<Leg> legs = new RealmList<Leg>();
这是我的 Duration 对象:
@RealmClass
public class Duration extends RealmObject implements Serializable {
private int value;
private String text;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
现在我在第一个问题上遇到了同样的问题mapper.writeValueAsString:
StatsUserVehicle doesn't have a primary key. (through reference chain: io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy["realm"]->io.realm.Realm["schema"]->io.realm.ImmutableRealmSchema["all"]->java.util.LinkedHashSet[0]->io.realm.ImmutableRealmObjectSchema["primaryKey"])
现在有人可以向我解释一下,为什么我会遇到 StatsUserVehicle 中没有主键的问题? 好的,这个 class 没有主键,因为它不需要主键。但是这个 class 与我尝试序列化的对象没有任何联系。 我该如何解决这个问题?
我正在使用:
implementation group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.2.7'
implementation(
[group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.8'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.8'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8']
)
和领域:
classpath "io.realm:realm-gradle-plugin:5.9.0"
查看异常消息:
io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy["realm"]
->io.realm.Realm["schema"]
->io.realm.ImmutableRealmSchema["all"]
->java.util.LinkedHashSet[0]
->io.realm.ImmutableRealmObjectSchema["primaryKey"]
您的 class 扩展了 RealmObject
并且在运行时有一个代理 io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy
可以有很多不同的东西。尝试忽略内部 Realm
属性:
@JsonIgnoreProperties({"realm", "schema"})