Realm 对象内部有值,但是当我用 GSON 序列化它时,字符串包含 0 或空数组

Realm object has values inside, but when I serialise it with GSON, the string contains 0 or empty arrays

这是我的 TripStep 模型中的对象:

@SerializedName("travel_mode")
private String travelMode;
@SerializedName("moving_time")
private int movingTime;
@SerializedName("moving_distance")
private int movingDistance;
private Polyline polyline;
private String vehicle;
private Route route;
@SerializedName("trip_id")
private int tripId;
@SerializedName("departure_stop")
private TripStop departureStop;
@SerializedName("arrival_stop")
private TripStop arrivalStop;
@PrimaryKey
private int id;
private Transit_details transit_details;
private RealmList<StopInfo> filteredLocations = new RealmList<>();
private RealmList<StopInfo> rawLocations = new RealmList<>();

Android studio 已经为它生成了 setter 和 getter,我还有一个没有参数的构造函数,还有一个为每个对象都有一个参数的构造函数。

如果我设置如下:

 Log.i("","deviations tripstep orig:" + stepOrig.getId() + "");
        Log.i("", "deviations tripstep orig:" + stepOrig.getRoute());

它会说:

deviations tripstep orig:24930
deviations tripstep orig:Route = [{duration:Duration},{distance:Distance},{arrival_time:null},{end_location:CoordLocation},{start_address:Compagniestraat 49, 1018 HM Amsterdam, Netherlands},{end_address:Stationsplein 3-5, 1012 Amsterdam, Netherlands},{departure_time:null},{start_location:CoordLocation},{steps:RealmList<Step>[11]},{legs:RealmList<Route>[0]}]

如你所见,我有内在的价值观。 如果我连载它,会这样说:

  {"filteredLocations":[],"id":0,"moving_distance":0,"moving_time":0,"rawLocations":[],"trip_id":0}

所以所有我的,id,路线等都将被删除。为什么会这样? 这是我的序列化函数:

public class JsonUtil {

private static JsonUtil instance;

private Gson gson = null;

private JsonUtil() {
    gson = new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }

                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .create();
}

public static <T> T jsonToObject(String json, Class<T> toClass) throws Exception {
    if (instance == null) {
        instance = new JsonUtil();
    }
    return instance.gson.fromJson(json, toClass);
}

public static <T> T jsonToObject(InputStream databaseInputStream, Class<T> toClass) throws Exception {
    if (instance == null) {
        instance = new JsonUtil();
    }
    return instance.gson.fromJson(new InputStreamReader(databaseInputStream), toClass);
}

public static <T> String objectToJson(Object o, Class<T> toClass) throws Exception {
    if (instance == null) {
        instance = new JsonUtil();
    }
    return instance.gson.toJson(o, toClass);
}
}

PS:它适用于其他 RealmObjects,但这个尤其不起作用

使用 Jackson 而不是使用 GSON,现在 json 已按应有的方式序列化。 我是这样做的:

 ObjectMapper mapper = new ObjectMapper();
        TripStepNonRealm tripStepNonRealm = new TripStepNonRealm(stepOrig.getTravelMode(), stepOrig.getRoute(), stepOrig.getVehicle());
        final JSONObject juser = new JSONObject();
        JSONObject tripstep = new JSONObject(mapper.writeValueAsString(tripStepNonRealm));
        juser.put("step", tripstep);
        Log.i("", "deviations updateStep ======= tripstep is: " + tripstep);
        Log.i("","deviations updateStep ======= juser is: " + juser);