【发布时间】:2019-02-28 11:23:34
【问题描述】:
我有一个 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 并实现了可序列化:
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 中没有主键的问题? 好的,这个类没有主键,因为它不需要主键。但是这个类与我尝试序列化的对象没有任何联系。 我该如何解决这个问题?
我正在使用:
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"
【问题讨论】:
-
尽量忽略question中的内部属性
-
@MichałZiober 抱歉,但我不明白你的回答。你想让我忽略什么?我的对象模型中没有 StatsUserVehicle,甚至没有导入。如你看到的。我的 Duration 类只有值和文本。
-
好的,谢谢,现在试试
-
请问mu建议的结果是什么,有效吗?
-
我已将我的评论添加为答案。
标签: json serialization jackson realm realm-mobile-platform