Skip to content

Instantly share code, notes, and snippets.

@Haoxiqiang
Created March 21, 2022 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Haoxiqiang/c2dc95da25d975b3f9aa0071443149f7 to your computer and use it in GitHub Desktop.
Save Haoxiqiang/c2dc95da25d975b3f9aa0071443149f7 to your computer and use it in GitHub Desktop.
Moshi Support JSONObject
package cn.ixiaochuan.frodo.api.moshi
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonAdapter.Factory
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
import java.lang.reflect.Type
class JSONAdapter(moshi: Moshi, keyType: Type, valueType: Type) :
JsonAdapter<JSONObject?>() {
private val keyAdapter: JsonAdapter<String> = moshi.adapter(keyType)
private val valueAdapter: JsonAdapter<Any> = moshi.adapter(valueType)
companion object {
val FACTORY: Factory =
Factory { type, annotations, moshi ->
val rawType = Types.getRawType(type)
if (annotations.isNotEmpty()) {
return@Factory null
}
if (rawType != JSONObject::class.java) {
return@Factory null
}
val keyAndValue = if (type === java.util.Properties::class.java) {
arrayOf<Type>(
String::class.java,
String::class.java
)
} else {
arrayOf<Type>(Any::class.java, Any::class.java)
}
JSONAdapter(
moshi,
keyAndValue[0],
keyAndValue[1]
).nullSafe()
}
}
@Throws(IOException::class)
override fun toJson(writer: JsonWriter, value: JSONObject?) {
writer.beginObject()
value?.keys()?.forEach { key ->
val v = value[key]
writer.promoteValueToName()
keyAdapter.toJson(writer, key)
if (v is JSONArray) {
writer.beginArray()
for (index in 0 until v.length()) {
valueAdapter.toJson(writer, v.get(index))
}
writer.endArray()
} else {
valueAdapter.toJson(writer, v)
}
}
writer.endObject()
}
@Throws(IOException::class)
override fun fromJson(reader: JsonReader): JSONObject {
val result = JSONObject()
reader.beginObject()
while (reader.hasNext()) {
reader.promoteNameToValue()
val name = keyAdapter.fromJson(reader)
if (name == null) {
throw JsonDataException(
"JSONObject key:$name is null."
)
}
val value = valueAdapter.fromJson(reader)
result.put(name, value)
}
reader.endObject()
return result
}
override fun toString(): String {
return "JsonAdapter($keyAdapter=$valueAdapter)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment