Created
April 5, 2016 02:18
-
-
Save gnumilanix/8fc9051823c28b437a751df9efceda52 to your computer and use it in GitHub Desktop.
Realm Primitive Wrapper for GSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive.adapter; | |
import com.realm.primitive.RealmBoolean; | |
import com.realm.primitive.RealmDouble; | |
import com.realm.primitive.RealmInteger; | |
import com.realm.primitive.RealmLong; | |
import com.realm.primitive.RealmString; | |
import com.google.gson.TypeAdapter; | |
import com.google.gson.stream.JsonReader; | |
import com.google.gson.stream.JsonWriter; | |
import java.io.IOException; | |
import io.realm.RealmObject; | |
/** | |
* Abstract implementation of {@link TypeAdapter} to be used by primitive classes being used with realm | |
* | |
* @param <T> of the primitive | |
*/ | |
public abstract class AbstractRealmPrimitiveTypeAdapter<T extends RealmObject> extends TypeAdapter<T> { | |
@Override | |
public void write(JsonWriter out, T value) throws IOException { | |
if (value == null) { | |
out.nullValue(); | |
return; | |
} | |
writeAs(out, value); | |
} | |
/** | |
* Writes value as T | |
* | |
* @param out to write value to | |
* @param value to write | |
* @throws IOException if this writer is closed or another I/O error occurs. | |
*/ | |
protected abstract void writeAs(JsonWriter out, T value) throws IOException; | |
@Override | |
public T read(JsonReader in) throws IOException { | |
return readAs(in); | |
} | |
/** | |
* Reads value as T | |
* | |
* @param in to read value from | |
* @return value of type T | |
* @throws IOException on error | |
*/ | |
protected abstract T readAs(JsonReader in) throws IOException; | |
/** | |
* Type adapter for {@link RealmBoolean} {@link Boolean} wrapper | |
*/ | |
public static class RealmBooleanTypeAdapter extends AbstractRealmPrimitiveTypeAdapter<RealmBoolean> { | |
@Override | |
protected void writeAs(JsonWriter out, RealmBoolean value) throws IOException { | |
out.value(value.getVal()); | |
} | |
@Override | |
protected RealmBoolean readAs(JsonReader in) throws IOException { | |
return new RealmBoolean(in.nextBoolean()); | |
} | |
} | |
/** | |
* Type adapter for {@link RealmDouble} {@link Double} wrapper | |
*/ | |
public static class RealmDoubleTypeAdapter extends AbstractRealmPrimitiveTypeAdapter<RealmDouble> { | |
@Override | |
protected void writeAs(JsonWriter out, RealmDouble value) throws IOException { | |
out.value(value.getVal()); | |
} | |
@Override | |
protected RealmDouble readAs(JsonReader in) throws IOException { | |
return new RealmDouble(in.nextDouble()); | |
} | |
} | |
/** | |
* Type adapter for {@link RealmInteger} {@link Integer} wrapper | |
*/ | |
public static class RealmIntegerTypeAdapter extends AbstractRealmPrimitiveTypeAdapter<RealmInteger> { | |
@Override | |
protected void writeAs(JsonWriter out, RealmInteger value) throws IOException { | |
out.value(value.getVal()); | |
} | |
@Override | |
protected RealmInteger readAs(JsonReader in) throws IOException { | |
return new RealmInteger(in.nextInt()); | |
} | |
} | |
/** | |
* Type adapter for {@link RealmLong} {@link Long} wrapper | |
*/ | |
public static class RealmLongTypeAdapter extends AbstractRealmPrimitiveTypeAdapter<RealmLong> { | |
@Override | |
protected void writeAs(JsonWriter out, RealmLong value) throws IOException { | |
out.value(value.getVal()); | |
} | |
@Override | |
protected RealmLong readAs(JsonReader in) throws IOException { | |
return new RealmLong(in.nextLong()); | |
} | |
} | |
/** | |
* Type adapter for {@link RealmString} {@link String} wrapper | |
*/ | |
public static class RealmStringTypeAdapter extends AbstractRealmPrimitiveTypeAdapter<RealmString> { | |
@Override | |
protected void writeAs(JsonWriter out, RealmString value) throws IOException { | |
out.value(value.getVal()); | |
} | |
@Override | |
protected RealmString readAs(JsonReader in) throws IOException { | |
return new RealmString(in.nextString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive; | |
import io.realm.RealmObject; | |
/** | |
* {@link Boolean} wrapper for realm | |
*/ | |
public class RealmBoolean extends RealmObject { | |
private Boolean val; | |
public RealmBoolean() { | |
} | |
public RealmBoolean(Boolean val) { | |
this.val = val; | |
} | |
public Boolean getVal() { | |
return val; | |
} | |
public void setVal(Boolean val) { | |
this.val = val; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive; | |
import io.realm.RealmObject; | |
/** | |
* {@link Double} wrapper for realm | |
*/ | |
public class RealmDouble extends RealmObject { | |
private Double val; | |
public RealmDouble() { | |
} | |
public RealmDouble(Double val) { | |
this.val = val; | |
} | |
public Double getVal() { | |
return val; | |
} | |
public void setVal(Double val) { | |
this.val = val; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive; | |
import io.realm.RealmObject; | |
/** | |
* {@link Integer} wrapper for realm | |
*/ | |
public class RealmInteger extends RealmObject { | |
private Integer val; | |
public RealmInteger() { | |
} | |
public RealmInteger(Integer val) { | |
this.val = val; | |
} | |
public Integer getVal() { | |
return val; | |
} | |
public void setVal(Integer val) { | |
this.val = val; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive; | |
import io.realm.RealmObject; | |
/** | |
* {@link Long} wrapper for realm | |
*/ | |
public class RealmLong extends RealmObject { | |
private Long val; | |
public RealmLong() { | |
} | |
public RealmLong(Long val) { | |
this.val = val; | |
} | |
public Long getVal() { | |
return val; | |
} | |
public void setVal(Long val) { | |
this.val = val; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive.adapter; | |
import com.realm.primitive.RealmBoolean; | |
import com.realm.primitive.RealmDouble; | |
import com.realm.primitive.RealmInteger; | |
import com.realm.primitive.RealmLong; | |
import com.realm.primitive.RealmString; | |
import com.google.gson.Gson; | |
import com.google.gson.TypeAdapter; | |
import com.google.gson.TypeAdapterFactory; | |
import com.google.gson.reflect.TypeToken; | |
import static com.realm.primitive.adapter.AbstractRealmPrimitiveTypeAdapter.RealmBooleanTypeAdapter; | |
import static com.realm.primitive.adapter.AbstractRealmPrimitiveTypeAdapter.RealmDoubleTypeAdapter; | |
import static com.realm.primitive.adapter.AbstractRealmPrimitiveTypeAdapter.RealmIntegerTypeAdapter; | |
import static com.realm.primitive.adapter.AbstractRealmPrimitiveTypeAdapter.RealmLongTypeAdapter; | |
import static com.realm.primitive.adapter.AbstractRealmPrimitiveTypeAdapter.RealmStringTypeAdapter; | |
/** | |
* Implementation of {@link TypeAdapterFactory} to support realm primitive wrappers | |
* | |
* @author milan | |
*/ | |
public class RealmPrimitiveTypeAdapterFactory implements TypeAdapterFactory { | |
@SuppressWarnings("unchecked") | |
@Override | |
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { | |
final Class<? super T> rawType = typeToken.getRawType(); | |
if (rawType == RealmBoolean.class) | |
return (TypeAdapter<T>) new RealmBooleanTypeAdapter(); | |
else if (rawType == RealmDouble.class) | |
return (TypeAdapter<T>) new RealmDoubleTypeAdapter(); | |
else if (rawType == RealmInteger.class) | |
return (TypeAdapter<T>) new RealmIntegerTypeAdapter(); | |
else if (rawType == RealmLong.class) | |
return (TypeAdapter<T>) new RealmLongTypeAdapter(); | |
else if (rawType == RealmString.class) | |
return (TypeAdapter<T>) new RealmStringTypeAdapter(); | |
else | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.realm.primitive; | |
import io.realm.RealmObject; | |
/** | |
* {@link String} wrapper for realm | |
*/ | |
public class RealmString extends RealmObject { | |
private String val; | |
public RealmString() { | |
} | |
public RealmString(String val) { | |
this.val = val; | |
} | |
public String getVal() { | |
return val; | |
} | |
public void setVal(String val) { | |
this.val = val; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return new GsonBuilder().setExclusionStrategies(realmExclusionStrategy) | |
... | |
.registerTypeAdapterFactory(new RealmPrimitiveTypeAdapterFactory()) | |
.create(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.schema; | |
public class User extends RealmObject { | |
private RealmList<RealmInteger> subscriptions; | |
public User() { | |
} | |
public RealmList<RealmInteger> getSubscriptions() { | |
return subscriptions; | |
} | |
public void setSubscriptions(RealmList<RealmInteger> subscriptions) { | |
this.subscriptions = subscriptions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is invalid with newer Realm versions since it now supports primitive types.