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

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

本文介绍了杰克逊无法序列化我的领域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Route对象,但看到无法序列化.所以我说我将调试并尝试分别从中序列化对象.

I had a Route object and I saw that I can't serialize it.So I said I'll debug and try to serialize the objects from it, separately.

这是我的功能:

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:

This is the variables from my object which has @RealmClass annotation and extends realmObject and implements 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对象:

This is my Duration object:

@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上遇到相同的问题:

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

Now can someone please explain to me, why I get an issue cause of no primary key in StatsUserVehicle?Ok, this class has no primary key, cause it doesn't need one. But this class has no connection, with my object that I try to serialize.How can I fix this?

我正在使用:

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"

推荐答案

看看异常消息:

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

Your class extends RealmObject and in runtime there is a proxy io.realm.nl_hgrams_passenger_model_tracking_DurationRealmProxy which could have many different things. Try to ingnore internal Realm properties with:

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

这篇关于杰克逊无法序列化我的领域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:14