杰克逊无法序列化我的领域对象

时间:2019-02-28 11:23:34

标签: json serialization jackson realm realm-mobile-platform

我有一个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中遇到没有主键的问题原因? 好的,该类没有主键,因为它不需要主键。但是此类与我尝试序列化的对象没有任何关系。 我该如何解决?

我正在使用:

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"

1 个答案:

答案 0 :(得分:1)

看看异常消息:

  

io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy [“ 领域”]
  -> io.realm.Realm [“ 模式”]
  -> io.realm.ImmutableRealmSchema [“ 全部”]
  -> java.util.LinkedHashSet [0]
  -> io.realm.ImmutableRealmObjectSchema [“ primaryKey”]

您的类扩展了RealmObject,并且在运行时有一个代理io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy,它可以具有许多不同的功能。尝试使用以下命令忽略内部Realm属性:

@JsonIgnoreProperties({"realm", "schema"})