Skip to content Skip to sidebar Skip to footer

Realm Serializing Data From Map

I am working with Realm for Android, I had to work with storage of maps. However, Realm does not support maps and so I made a workaround for this as suggested here But when I am se

Solution 1:

Might be too late, but since I have tried to solve same problem, I came up with solution of writing custom JSON serializer/deserializer:

publicclassKeyValueSerializerimplementsJsonSerializer<RealmList<KeyValue>>,
                                           JsonDeserializer<RealmList<KeyValue>> {
    @OverridepublicJsonElementserialize(RealmList<KeyValue> src,
                                 Type typeOfSrc,
                                 JsonSerializationContext context) {

        JsonObject keyValueJson = newJsonObject();

        for(KeyValue keyValue : src) {
            keyValueJson.addProperty(keyValue.getKey(), keyValue.getValue());
        }

        return keyValueJson;
    }

    @OverridepublicRealmList<KeyValue> deserialize(JsonElement json,
                                           Type typeOfT,
                                           JsonDeserializationContext context) throws JsonParseException {

        Typetype = newTypeToken<Map<String, String>>() {}.getType();
        Map<String, String> data = newGson().fromJson(json, type);

        RealmList<KeyValue> keyValues = newRealmList<>();

        for (Map.Entry<String, String> entry : data.entrySet()) {
            KeyValue keyValue = newKeyValue();
            keyValue.setKey(entry.getKey());
            keyValue.setValue(entry.getValue());

            keyValues.add(keyValue);
        }

        return keyValues;
    }
}

Where the KeyValue class follows the same map representation as proposed here:

publicclassKeyValueextendsRealmObject {
    privateString key;
    privateString value;

    publicKeyValue() {

    }

    publicStringgetKey() {
        return key;
    }

    publicvoidsetKey(String key) {
        this.key = key;
    }

    publicStringgetValue() {
        return value;
    }

    publicvoidsetValue(String value) {
        this.value = value;
    }
}

RealmList is a generic type, so it can't be addressed with a .class. Use TypeToken to get a Type when registering a TypeAdapter and calling fromJson method:

TypekeyValueRealmListType=newTypeToken<RealmList<KeyValue>>() {}.getType();
Gsongson=newGsonBuilder().registerTypeAdapter(keyValueRealmListType, 
                                             newKeyValueSerializer())
           .create();

RealmList<KeyValue> keyValues = gson.fromJson(keyValuesJson,
                                              keyValueRealmListType);

As far as I have tested, a custom serializer/deserializer like this should read and write JSON in preferred format like it is in the question. I find it a bit hacky, but it does the job.

Solution 2:

If

{"key":"key1","value":"value1"}

This represents your entire data, then this should be your entire RealmModel.

publicclassMyObjectextendsRealmObject{
    @PrimaryKey
    privateString key;

    privateString value;

    // getters setters
}

Post a Comment for "Realm Serializing Data From Map"