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

问题描述 投票:0回答:1

我有一个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"
json serialization jackson realm realm-mobile-platform
1个回答
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"})
© www.soinside.com 2019 - 2024. All rights reserved.