java 使用GSON将Realm对象序列化为JSON

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用GSON将Realm对象序列化为JSON相关的知识,希望对你有一定的参考价值。

package io.realm.example;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

public class PersonSerializer implements JsonSerializer<Person> {

    @Override
    public JsonElement serialize(Person src, Type typeOfSrc, JsonSerializationContext context) {
        final JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", src.getName());
        jsonObject.addProperty("age", src.getAge());
        jsonObject.add("favoriteDog", context.serialize(src.getFavoriteDog()));
        jsonObject.add("dogs", context.serialize(src.getDogs()));
        return jsonObject;
    }
}
package io.realm.example;

import io.realm.RealmList;
import io.realm.RealmObject;

public class Person extends RealmObject {

    private String name;
    private int age;
    private Dog favoriteDog;
    private RealmList<Dog> dogs;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Dog getFavoriteDog() {
        return favoriteDog;
    }

    public void setFavoriteDog(Dog favoriteDog) {
        this.favoriteDog = favoriteDog;
    }

    public RealmList<Dog> getDogs() {
        return dogs;
    }

    public void setDogs(RealmList<Dog> dogs) {
        this.dogs = dogs;
    }
}
package io.realm.examples;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;

import io.realm.examples.realmgridview.Dog;

public class DogSerializer implements JsonSerializer<Dog> {

    @Override
    public JsonElement serialize(Dog src, Type typeOfSrc, JsonSerializationContext context) {
        final JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("name", src.getName());
        return jsonObject;
    }
}
package io.realm.example;

import io.realm.RealmObject;

public class Dog extends RealmObject {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
// GSON can parse the data.
//
// Deserialization:
// Note there is a bug in GSON 2.3.1 that can cause it to StackOverflow when working with RealmObjects.
// To work around this, use the ExclusionStrategy below or downgrade to 1.7.1
// See more here: https://code.google.com/p/google-gson/issues/detail?id=440
//
// Serialization:
// <Type>RealmProxy objects are created by the Realm annotation processor. They are used to control
// access to the actual data instead of storing them in fields and it is therefore them we need to register a
// TypeAdapter for.
Gson 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;
            }
        })
        .registerTypeAdapter(Class.forName("io.realm.PersonRealmProxy"), new PersonSerializer())
        .registerTypeAdapter(Class.forName("io.realm.DogRealmProxy"), new DogSerializer())
        .create();

// Serialize a Realm object to a JSON string
String json = gson.toJson(realm.where(Person.class).findFirst());

以上是关于java 使用GSON将Realm对象序列化为JSON的主要内容,如果未能解决你的问题,请参考以下文章

如何将java对象序列化/反序列化为javax.JSON

Java GSON - 将int序列化为json文件的字符串

如何使用 Java 和 Gson 中的构建器模式将选择类字段序列化为 JSON 字符串?

使用 Gson 序列化具有瞬态字段的对象

让我们畅通无阻:在 Scala 中使用 GSON 读取 Json

如何使用GSON配置哪些POJO字段序列化为JSON?