Last active
March 21, 2017 10:51
-
-
Save markus2610/8dbbf02b85975a44f62b45dd71b13231 to your computer and use it in GitHub Desktop.
Kotlin Realm Primitive Arrays Workaround for Moshi
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 de.wdv.apps.yoursingapore.core.moshi | |
import com.squareup.moshi.JsonAdapter | |
import com.squareup.moshi.JsonReader | |
import com.squareup.moshi.JsonWriter | |
import com.squareup.moshi.Moshi | |
import de.wdv.apps.yoursingapore.core.realm.RealmIntArray | |
import de.wdv.apps.yoursingapore.core.realm.RealmLongArray | |
import de.wdv.apps.yoursingapore.core.realm.RealmStringArray | |
import java.lang.reflect.Type | |
/* | |
* Copyright 2017 Markus Wedler. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
class RealmTypeAdapter { | |
companion object { | |
val FACTORY: JsonAdapter.Factory = JsonAdapter.Factory { type: Type, annotations: Set<Annotation>, moshi: Moshi -> | |
if (type == RealmStringArray::class.java) { | |
return@Factory RealmStringArrayAdapter() | |
} else if(type == RealmIntArray::class.java) { | |
return@Factory RealmIntArrayAdapter() | |
} else if(type == RealmLongArray::class.java) { | |
return@Factory RealmLongArrayAdapter() | |
} | |
null | |
} | |
} | |
class RealmStringArrayAdapter : JsonAdapter<RealmStringArray>() { | |
override fun fromJson(reader: JsonReader?): RealmStringArray? { | |
if (reader != null) { | |
val realmStringArray = RealmStringArray() | |
val StringList = arrayListOf<String>() | |
reader.beginArray() | |
while (reader.hasNext()) { | |
StringList.add(reader.nextString()) | |
} | |
reader.endArray() | |
realmStringArray.joinedStrings = StringList.joinToString(",,,,") | |
return realmStringArray | |
} | |
return null | |
} | |
override fun toJson(writer: JsonWriter, value: RealmStringArray) { | |
// ignore | |
} | |
} | |
class RealmIntArrayAdapter : JsonAdapter<RealmIntArray>() { | |
override fun fromJson(reader: JsonReader?): RealmIntArray? { | |
if (reader != null) { | |
val realmIntArray = RealmIntArray() | |
val intList = arrayListOf<Int>() | |
reader.beginArray() | |
while (reader.hasNext()) { | |
intList.add(reader.nextInt()) | |
} | |
reader.endArray() | |
realmIntArray.joinedInts = intList.joinToString(",") | |
return realmIntArray | |
} | |
return null | |
} | |
override fun toJson(writer: JsonWriter, value: RealmIntArray) { | |
// ignore | |
} | |
} | |
class RealmLongArrayAdapter : JsonAdapter<RealmLongArray>() { | |
override fun fromJson(reader: JsonReader?): RealmLongArray? { | |
if (reader != null) { | |
val realmLongArray = RealmLongArray() | |
val longList = arrayListOf<Long>() | |
reader.beginArray() | |
while (reader.hasNext()) { | |
longList.add(reader.nextLong()) | |
} | |
reader.endArray() | |
realmLongArray.joinedLongs = longList.joinToString(",") | |
return realmLongArray | |
} | |
return null | |
} | |
override fun toJson(writer: JsonWriter, value: RealmLongArray) { | |
// ignore | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment