jvmMain.com.pubnub.api.JsonElement.jvm.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin-api-jvm Show documentation
Show all versions of pubnub-kotlin-api-jvm Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!
The newest version!
package com.pubnub.api
import com.google.gson.GsonBuilder
actual typealias JsonElement = com.google.gson.JsonElement
actual fun JsonElement.asString(): String? {
return if (this.isJsonPrimitive && this.asJsonPrimitive.isString) {
this.asString
} else {
null
}
}
actual fun JsonElement.asMap(): Map? {
return if (this.isJsonObject) {
this.asJsonObject.asMap()
} else {
null
}
}
actual fun JsonElement.isNull(): Boolean {
return this.isJsonNull
}
actual fun JsonElement.asList(): List? {
return if (this.isJsonArray) {
this.asJsonArray.asList()
} else {
null
}
}
actual fun JsonElement.asLong(): Long? {
return if (this.isJsonPrimitive && this.asJsonPrimitive.isNumber) {
this.asLong
} else {
null
}
}
actual fun JsonElement.asBoolean(): Boolean? {
return if (this.isJsonPrimitive && this.asJsonPrimitive.isBoolean) {
this.asBoolean
} else {
null
}
}
actual fun JsonElement.asDouble(): Double? {
return if (this.isJsonPrimitive && this.asJsonPrimitive.isNumber) {
this.asDouble
} else {
null
}
}
actual fun JsonElement.asNumber(): Number? {
return if (this.isJsonPrimitive && this.asJsonPrimitive.isNumber) {
this.asNumber
} else {
null
}
}
actual fun createJsonElement(any: Any?): JsonElement {
return GsonBuilder().serializeNulls().create().toJsonTree(any)
}