Skip to content

Instantly share code, notes, and snippets.

@SpaceBison
Created September 24, 2018 06:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpaceBison/3cbf390116cc84d0851ea28aaaf740dd to your computer and use it in GitHub Desktop.
Save SpaceBison/3cbf390116cc84d0851ea28aaaf740dd to your computer and use it in GitHub Desktop.
Moshi adapter for org.json objects
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
}
@onemonster
Copy link

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()
    }
}

@iadcialim
Copy link

How to use this one?

@SpaceBison
Copy link
Author

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.

@SpaceBison
Copy link
Author

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