Created
September 24, 2018 06:01
-
-
Save SpaceBison/3cbf390116cc84d0851ea28aaaf740dd to your computer and use it in GitHub Desktop.
Moshi adapter for org.json objects
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
package com.n7mobile.loterity.network.moshi | |
import com.squareup.moshi.JsonAdapter | |
import com.squareup.moshi.JsonReader | |
import com.squareup.moshi.JsonReader.Token.END_ARRAY | |
import com.squareup.moshi.JsonReader.Token.END_OBJECT | |
import com.squareup.moshi.JsonWriter | |
import org.json.JSONArray | |
import org.json.JSONObject | |
class SimpleJsonAdapter : JsonAdapter<JSONObject>() { | |
override fun fromJson(reader: JsonReader): JSONObject? = | |
reader.readObject() | |
override fun toJson(writer: JsonWriter, value: JSONObject?) { | |
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | |
} | |
} | |
private fun JsonReader.readValue(): Any? = | |
peek().let { token -> | |
when (token) { | |
JsonReader.Token.BEGIN_ARRAY -> readArray() | |
JsonReader.Token.BEGIN_OBJECT -> readObject() | |
JsonReader.Token.STRING -> "\"${nextString()}\"" | |
JsonReader.Token.NUMBER -> nextNumber() | |
JsonReader.Token.BOOLEAN -> nextBoolean() | |
JsonReader.Token.NULL -> nextNull() ?: null // elvis+null necessary for type inference | |
else -> throw com.squareup.moshi.JsonEncodingException("Unexpected token: $token") | |
} | |
} | |
private fun JsonReader.nextNumber(): Number { | |
try { | |
return nextInt() | |
} catch (ignored: Throwable) { | |
} | |
try { | |
return nextLong() | |
} catch (ignored: Throwable) { | |
} | |
return nextDouble() | |
} | |
private fun JsonReader.readObject(): JSONObject { | |
val jsonObject = JSONObject() | |
beginObject() | |
while (peek() != END_OBJECT) { | |
jsonObject.put(nextName(), readValue()) | |
} | |
endObject() | |
return jsonObject | |
} | |
private fun JsonReader.readArray(): JSONArray { | |
val jsonArray = JSONArray() | |
beginArray() | |
while (peek() != END_ARRAY) { | |
jsonArray.put(readJsonValue()) | |
} | |
endArray() | |
return jsonArray | |
} |
How to use this one?
The
JsonReader.nextNumber()
can be shortened to the following. Just a suggestion, thanks for your contribution!private fun JsonReader.nextNumber(): Number { return try { nextInt() } catch (ignored: Throwable) { nextLong() } catch (ignored: Throwable) { nextDouble() } }
This won't work though, because if nextLong()
throws an exception, it won't be caught in the second catch block, so nextDouble()
will never be called.
How to use this one?
I've written this one a while ago so it's highly probable that it won't work with the current Moshi release. Nevertheless, you can find the docs about how to register a custom type adapter in the README on the Moshi repo: https://github.com/square/moshi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
JsonReader.nextNumber()
can be shortened to the following. Just a suggestion, thanks for your contribution!