Created
January 21, 2016 19:00
-
-
Save bclymer/f569fffb41bab046c2d1 to your computer and use it in GitHub Desktop.
Kotlin Realm Primitive Arrays Workaround
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
val realmIntArrayType = object : TypeToken<RealmIntArray>() {}.type | |
val realmLongArrayType = object : TypeToken<RealmLongArray>() {}.type | |
val realmStringArrayType = object : TypeToken<RealmStringArray>() {}.type | |
val gson = GsonBuilder() | |
.setExclusionStrategies(object : ExclusionStrategy { | |
override fun shouldSkipClass(clazz: Class<*>?): Boolean { | |
return false | |
} | |
override fun shouldSkipField(f: FieldAttributes?): Boolean { | |
return f?.declaringClass?.equals(RealmObject::class.java) == true | |
} | |
}) | |
.registerTypeAdapter(realmIntArrayType, object : TypeAdapter<RealmIntArray>() { | |
override fun write(out: JsonWriter?, value: RealmIntArray?) { | |
// ignore | |
} | |
override fun read(inJson: JsonReader?): RealmIntArray? { | |
if (inJson != null) { | |
val realmIntArray = RealmIntArray() | |
val intList = arrayListOf<Int>() | |
inJson.beginArray() | |
while (inJson.hasNext()) { | |
intList.add(inJson.nextInt()) | |
} | |
inJson.endArray() | |
realmIntArray.joinedInts = intList.joinToString(",") | |
return realmIntArray | |
} | |
return null | |
} | |
}) | |
.registerTypeAdapter(realmLongArrayType, object : TypeAdapter<RealmLongArray>() { | |
override fun write(out: JsonWriter?, value: RealmLongArray?) { | |
// ignore | |
} | |
override fun read(inJson: JsonReader?): RealmLongArray? { | |
if (inJson != null) { | |
val realmLongArray = RealmLongArray() | |
val longList = arrayListOf<Long>() | |
inJson.beginArray() | |
while (inJson.hasNext()) { | |
longList.add(inJson.nextLong()) | |
} | |
inJson.endArray() | |
realmLongArray.joinedLongs = longList.joinToString(",") | |
return realmLongArray | |
} | |
return null | |
} | |
}) | |
.registerTypeAdapter(realmStringArrayType, object : TypeAdapter<RealmStringArray>() { | |
override fun write(out: JsonWriter?, value: RealmStringArray?) { | |
// ignore | |
} | |
override fun read(inJson: JsonReader?): RealmStringArray? { | |
if (inJson != null) { | |
val realmStringArray = RealmStringArray() | |
val StringList = arrayListOf<String>() | |
inJson.beginArray() | |
while (inJson.hasNext()) { | |
StringList.add(inJson.nextString()) | |
} | |
inJson.endArray() | |
// TODO think of a not horrible solution for this | |
realmStringArray.joinedStrings = StringList.joinToString(",,,,") | |
return realmStringArray | |
} | |
return null | |
} | |
}) | |
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS") | |
.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
import io.realm.RealmObject | |
public open class RealmIntArray : RealmObject() { | |
public open var joinedInts: String? = null | |
} | |
public fun RealmIntArray.toList(): List<Int> { | |
if (joinedInts != null) { | |
return joinedInts!!.split(",").map { it.toInt() } | |
} else { | |
return listOf() | |
} | |
} | |
public open class RealmLongArray : RealmObject() { | |
public open var joinedLongs: String? = null | |
} | |
public fun RealmLongArray.toList(): List<Long> { | |
if (joinedLongs != null) { | |
return joinedLongs!!.split(",").map { it.toLong() } | |
} else { | |
return listOf() | |
} | |
} | |
public open class RealmStringArray : RealmObject() { | |
public open var joinedStrings: String? = null | |
} | |
public fun RealmStringArray.toList(): List<String> { | |
// TODO think of a not horrible solution for this | |
if (joinedStrings != null) { | |
return joinedStrings!!.split(",,,,") | |
} else { | |
return listOf() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Workaround for Moshi