Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package vn.fstyle.realmandroiddemo.realm;
import io.realm.RealmObject;
/**
* RealmString.
*
* @author DaoLQ
*/
public class RealmString extends RealmObject {
String stringValue;
/**
* constructor
*/
public RealmString() {
}
/**
* @param stringValue value
*/
public RealmString(String stringValue) {
this.stringValue = stringValue;
}
/**
* @return value
*/
public String getStringValue() {
return stringValue;
}
/**
* @param stringValue input
*/
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}
}
package vn.fstyle.realmandroiddemo.realm;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import io.realm.RealmList;
/**
* RealmStringDeserializer.
*
* @author DaoLQ
*/
public class RealmStringDeserializer implements
JsonDeserializer<RealmList<RealmString>> {
@Override
public RealmList<RealmString> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
RealmList<RealmString> realmStrings = new RealmList<>();
JsonArray stringList = json.getAsJsonArray();
for (JsonElement stringElement : stringList) {
realmStrings.add(new RealmString(stringElement.getAsString()));
}
return realmStrings;
}
}
@daolq

This comment has been minimized.

Copy link
Owner Author

daolq commented Jul 12, 2016

Config Gson when Create RestAdapter.Builder of retrofit:

Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .setExclusionStrategies(new ExclusionStrategy() {
                @Override
                public boolean shouldSkipField(FieldAttributes f) {
                    return f.getDeclaringClass().equals(RealmObject.class);
                }
                @Override
                public boolean shouldSkipClass(Class<?> clazz) {
                    return false;
                }
            })
            .registerTypeAdapter(new TypeToken<RealmList<RealmString>>() {
            }.getType(), new RealmStringDeserializer())
            .create();
@daolq

This comment has been minimized.

Copy link
Owner Author

daolq commented Jul 12, 2016

In Object

private RealmList<RealmString> mStrings;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.