Last active
October 10, 2016 08:16
-
-
Save peterbetos/fb42ac87c31accd2efc43e33ddaaecc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class RealmListConverter<T extends RealmObject> implements | |
TypeConverter<RealmList<T>> { | |
@FunctionalInterface | |
public interface SingleFieldSerialization<T> { | |
Object getValue(T object); | |
} | |
Class<T> clazz; | |
TypeConverter<T> typeConverter; | |
JsonMapper<T> mapper; | |
public final String mLogTag = getClass().getSimpleName(); | |
private SingleFieldSerialization<T> singleFieldSerialization; | |
public void setSingleFieldSerialization(SingleFieldSerialization<T> | |
singleFieldSerialization) { | |
this.singleFieldSerialization = singleFieldSerialization; | |
} | |
protected void init() { | |
if (typeConverter == null && mapper == null) { | |
try { | |
typeConverter = LoganSquare.typeConverterFor(getPersistentClass()); | |
} catch (NoSuchTypeConverterException e) { | |
Log.w(mLogTag, "TypeConverter failed. Using mapper instead"); | |
mapper = LoganSquare.mapperFor(getPersistentClass()); | |
} | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public Class<T> getPersistentClass() { | |
if (clazz == null) { | |
this.clazz = (Class<T>) ((ParameterizedType) getClass() | |
.getGenericSuperclass()).getActualTypeArguments()[0]; | |
} | |
return clazz; | |
} | |
@Override | |
public RealmList<T> parse(JsonParser jsonParser) throws IOException { | |
init(); | |
RealmList<T> list = new RealmList<>(); | |
if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) { | |
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { | |
T object = typeConverter != null ? typeConverter.parse | |
(jsonParser) : mapper.parse(jsonParser); | |
if (object != null) { | |
list.add(object); | |
} | |
} | |
} | |
return list; | |
} | |
@Override | |
public void serialize(RealmList<T> list, String fieldName, boolean | |
writeFieldNameForObject, JsonGenerator jsonGenerator) throws | |
IOException { | |
if (writeFieldNameForObject) { | |
if (list == null || list.isEmpty()) { | |
return; | |
} | |
jsonGenerator.writeFieldName(fieldName); | |
} | |
if (list == null || list.isEmpty()) { | |
jsonGenerator.writeNull(); | |
return; | |
} | |
jsonGenerator.writeStartArray(); | |
if (singleFieldSerialization != null) { | |
secondConditionSerialization(list, jsonGenerator); | |
} else { | |
init(); | |
for (T object : list) { | |
serializingTypeConverterMapper(object, fieldName, | |
jsonGenerator); | |
} | |
} | |
jsonGenerator.writeEndArray(); | |
} | |
public void secondConditionSerialization(RealmList<T> list, JsonGenerator | |
jsonGenerator) { | |
try { | |
JsonMapper<Object> objectMapper = LoganSquare.mapperFor(Object | |
.class); | |
for (T object : list) { | |
if (object != null) { | |
objectMapper.serialize(singleFieldSerialization.getValue | |
(object), jsonGenerator, true); | |
} else { | |
jsonGenerator.writeNull(); | |
} | |
} | |
} catch (IOException e) { | |
Log.w(mLogTag, Log.getStackTraceString(e)); | |
} | |
} | |
private void serializingTypeConverterMapper(T object, String fieldName, | |
JsonGenerator jsonGenerator) { | |
try { | |
if (object != null) { | |
if (typeConverter != null) { | |
typeConverter.serialize(object, fieldName, false, | |
jsonGenerator); | |
} else { | |
mapper.serialize(object, jsonGenerator, true); | |
} | |
} else { | |
jsonGenerator.writeNull(); | |
} | |
} catch (IOException e) { | |
Log.w(mLogTag, Log.getStackTraceString(e)); | |
} | |
} |
This file contains hidden or 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
public class RealmListCategoryItemConverter extends RealmListConverter<Sample> { | |
//Assume that Sample is an extension of RealmObject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment