kr.jadekim.jext.gson.extensions.kt Maven / Gradle / Ivy
package kr.jadekim.jext.gson
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import java.io.Reader
inline fun Gson.fromJson(json: String): T = fromJson(json, object : TypeToken() {}.type)
inline fun Gson.fromJson(json: Reader): T = fromJson(json, object : TypeToken() {}.type)
inline fun Gson.fromJson(reader: JsonReader): T = fromJson(reader, object : TypeToken() {}.type)
inline fun Gson.fromJson(json: JsonElement): T = fromJson(json, object : TypeToken() {}.type)
fun JsonElement.sorted(): JsonElement = when (this) {
is JsonObject -> sorted()
is JsonArray -> sorted()
else -> this
}
fun JsonObject.sorted(): JsonObject {
val entries = entrySet().sortedBy { it.key }
val json = JsonObject()
for (entry in entries) {
json.add(entry.key, entry.value.sorted())
}
return json
}
fun JsonArray.sorted(): JsonArray {
val json = JsonArray()
for (item in this) {
val value = when (item) {
is JsonObject -> item.sorted()
is JsonArray -> item.sorted()
else -> item
}
json.add(value)
}
return json
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy