io.appwrite.json.PreciseNumberAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-for-kotlin Show documentation
Show all versions of sdk-for-kotlin Show documentation
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Kotlin SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
package io.appwrite.json
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken.*
import com.google.gson.stream.JsonWriter
import java.io.IOException
internal class PreciseNumberAdapter : TypeAdapter() {
private val delegate = Gson()
.getAdapter(Any::class.java)
@Throws(IOException::class)
override fun write(out: JsonWriter?, value: Any?) {
delegate.write(out, value)
}
@Throws(IOException::class)
override fun read(input: JsonReader): Any? {
return when (input.peek()) {
BEGIN_ARRAY -> {
val list = mutableListOf()
input.beginArray()
while (input.hasNext()) {
list.add(read(input))
}
input.endArray()
list
}
BEGIN_OBJECT -> {
val map = mutableMapOf()
input.beginObject()
while (input.hasNext()) {
map[input.nextName()] = read(input)
}
input.endObject()
map
}
STRING -> {
input.nextString()
}
NUMBER -> {
val numberString = input.nextString()
if (numberString.indexOf('.') != -1) {
numberString.toDouble()
} else {
numberString.toLong()
}
}
BOOLEAN -> {
input.nextBoolean()
}
NULL -> {
input.nextNull()
null
}
else -> {
throw IllegalStateException()
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy