All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.vaadinonkotlin.restclient.GsonUtils.kt Maven / Gradle / Ivy

package eu.vaadinonkotlin.restclient

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.Reader
import java.io.StringReader
import java.lang.reflect.Type

/**
 * Parses [json] as a list of items with class [itemClass] and returns that.
 */
public fun  Gson.fromJsonArray(json: String, itemClass: Class): List =
    fromJsonArray(StringReader(json), itemClass)

/**
 * Parses JSON from a [reader] as a list of items with class [itemClass] and returns that.
 */
public fun  Gson.fromJsonArray(reader: Reader, itemClass: Class): List {
    val type: Type = TypeToken.getParameterized(List::class.java, itemClass).type
    return fromJson>(reader, type)
}

/**
 * Parses JSON from a [reader] as a map of items with class [valueClass] and returns that.
 */
public fun  Gson.fromJsonMap(reader: Reader, valueClass: Class): Map {
    val type: Type = TypeToken.getParameterized(
        Map::class.java,
        String::class.java,
        valueClass
    ).type
    return fromJson>(reader, type)
}

/**
 * Parses [json] as a map of items with class [valueClass] and returns that.
 */
public fun  Gson.fromJsonMap(json: String, valueClass: Class): Map =
    fromJsonMap(StringReader(json), valueClass)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy