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

commonMain.com.eygraber.json.sugar.JsonArray.kt Maven / Gradle / Ivy

The newest version!
package com.eygraber.json.sugar

import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

public inline fun  JsonArray.toListOf(mapper: (JsonElement) -> T): List = map(mapper)

public fun JsonArray.toListOfString(): List = map { it.asString }

public fun JsonArray.toListOfInt(): List = map { it.asInt }

public fun JsonArray.toListOfLong(): List = map { it.asLong }

public fun JsonArray.toSetOfString(): Set = mapTo(LinkedHashSet(size)) { it.asString }

public fun JsonArray.toListOfObject(): List = map { it.jsonObject }

public fun JsonArray?.toListOfObjectOrEmpty(): List = this?.map { it.jsonObject }.orEmpty()

public fun JsonArray.asSequenceOfObject(): Sequence = asSequence().map { it.jsonObject }

public fun JsonArray.toListOfPrimitive(): List = map { it.jsonPrimitive }

public fun List.toJsonArray(): JsonArray = JsonArray(map(::JsonPrimitive))

public fun Set.toJsonArray(): JsonArray = JsonArray(map(::JsonPrimitive))

public fun Collection.toJsonArray(): JsonArray = JsonArray(map(::JsonPrimitive))

public fun emptyJsonArray(): JsonArray = JsonArray(emptyList())




© 2015 - 2024 Weber Informatics LLC | Privacy Policy