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

run.smt.ktest.json.mapping.kt Maven / Gradle / Ivy

package run.smt.ktest.json

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.jayway.jsonpath.*
import run.smt.ktest.util.reflection.canBeAssignedTo
import run.smt.ktest.util.resource.load
import java.io.InputStream
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.KClass

val mapper = jacksonObjectMapper()

inline fun  String.loadAsJson(): T = load().deserialize()
fun  String.loadAsJson(type: JavaType): T = load() deserialize type
fun  String.loadAsJson(type: TypeReference) = load() deserialize type
fun  String.loadAsJson(type: TypeDSL) = load().deserialize(type)
fun String.loadAsJsonTree() = load().deserialize()

fun Any?.serialize(): ByteArray = mapper.writeValueAsBytes(this)
fun Any?.dump(): String = mapper.writer().withDefaultPrettyPrinter().writeValueAsString(this)

inline fun  String.deserialize() = this deserialize R::class.java
inline fun  ByteArray.deserialize() = this deserialize R::class.java
inline fun  InputStream.deserialize() = this deserialize R::class.java

infix fun  String.deserialize(clazz: KClass) = mapper.readTree(this) mapTo clazz.java
infix fun  ByteArray.deserialize(clazz: KClass) = mapper.readTree(this) mapTo clazz.java
infix fun  InputStream.deserialize(clazz: KClass) = mapper.readTree(this) mapTo clazz.java

infix fun  String.deserialize(typeDSL: TypeDSL): R = this deserialize type(typeDSL)
infix fun  ByteArray.deserialize(typeDSL: TypeDSL): R = this deserialize type(typeDSL)
infix fun  InputStream.deserialize(typeDSL: TypeDSL): R = this deserialize type(typeDSL)

infix fun  String.deserialize(clazz: Class) = mapper.readTree(this) mapTo clazz
infix fun  ByteArray.deserialize(clazz: Class) = mapper.readTree(this) mapTo clazz
infix fun  InputStream.deserialize(clazz: Class) = mapper.readTree(this) mapTo clazz

infix fun  String.deserialize(type: JavaType) = mapper.readValue(this, type)
infix fun  ByteArray.deserialize(type: JavaType) = mapper.readValue(this, type)
infix fun  InputStream.deserialize(type: JavaType) = mapper.readValue(this, type)

infix fun  String.deserialize(type: TypeReference) = mapper.readValue(this, type)
infix fun  ByteArray.deserialize(type: TypeReference) = mapper.readValue(this, type)
infix fun  InputStream.deserialize(type: TypeReference) = mapper.readValue(this, type)

inline fun  JsonNode.mapTo(): R = mapTo(R::class)
infix fun  JsonNode.mapTo(type: TypeReference): R = mapTo(mapper.constructType(type.type))
infix fun  JsonNode.mapTo(resultClass: Class): R = mapTo { simple(resultClass) }
infix fun  JsonNode.mapTo(resultClass: KClass): R = mapTo { simple(resultClass) }
infix fun  JsonNode.mapTo(type: TypeDSL): R = mapTo(type(type))

infix fun  JsonNode.mapTo(type: JavaType): R {
    @Suppress("UNCHECKED_CAST")
    return when {
        // TODO: not an optimal way... need to find better one
        DocumentContext::class canBeAssignedTo type.rawClass -> JsonPath.parse(this.toString()) as R
        JsonNode::class canBeAssignedTo type.rawClass -> this as R
        else -> mapper.convertValue(this, type)
    }
}

fun createJsonNode(value: Any?): JsonNode {
    val factory = mapper.nodeFactory
    return when (value) {
        null -> factory.nullNode()
        is Long -> factory.numberNode(value)
        is Short -> factory.numberNode(value)
        is Int -> factory.numberNode(value)
        is Float -> factory.numberNode(value)
        is Double -> factory.numberNode(value)
        is BigDecimal -> factory.numberNode(value)
        is BigInteger -> factory.numberNode(value)
        is Byte -> factory.numberNode(value)
        is String -> factory.textNode(value)
        is ByteArray -> factory.binaryNode(value)
        is Boolean -> factory.booleanNode(value)
        is Iterable<*> -> factory.arrayNode().addAll(value.map { createJsonNode(it) })
        is Map<*, *> -> factory.objectNode().setAll(value.mapKeys { it.key?.toString() }.mapValues { createJsonNode(it.value) })
        else -> value.serialize() deserialize JsonNode::class
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy