Jackson cannot serialize an my realm object

2019-08-19 16:44发布

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.

This is my function:

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;
}

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>();

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;
}

}

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"])

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?

I am using:

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']
)

And realm:

  classpath "io.realm:realm-gradle-plugin:5.9.0"

1条回答
仙女界的扛把子
2楼-- · 2019-08-19 17:20

Take a look on exception message:

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"]

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"})
查看更多
登录 后发表回答