com.ancientlightstudios.quarkus.kotlin.openapi.JsonDeserializationExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-kotlin-openapi Show documentation
Show all versions of quarkus-kotlin-openapi Show documentation
A support library providing shared classes that the generated code will use.
The newest version!
package com.ancientlightstudios.quarkus.kotlin.openapi
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.NullNode
import com.fasterxml.jackson.databind.node.ObjectNode
fun Maybe.asJson(objectMapper: ObjectMapper): Maybe = onNotNull {
try {
if (value.isEmpty()) {
success(null)
} else {
success(objectMapper.readValue(value, JsonNode::class.java))
}
} catch (_: Exception) {
failure(ValidationError("is not valid json", context, ErrorKind.Incompatible))
}
}
@Suppress("unused")
fun Maybe.asObject(): Maybe = onNotNull {
when (this.value) {
is NullNode -> success(null)
is ObjectNode -> this@asObject as Maybe
else -> Maybe.Failure(context, ValidationError("is not a valid json object", context, ErrorKind.Incompatible))
}
}
@Suppress("unused")
fun JsonNode?.findProperty(name: String, context: String): Maybe = Maybe.Success(context, this?.get(name))
// returns the amount of properties in the json node. only properties from the known list will be considered.
// if a property has a default value but is not declared in the node it will still affect the counter
fun JsonNode.countKnownProperties(knownProperties: List, propertiesWithDefault: List): Int {
// fieldNames() just return an iterator which requires an extra step to convert it into a set or list
val declaredPropertyNames = properties().map { it.key }.toMutableSet()
declaredPropertyNames.addAll(propertiesWithDefault)
return knownProperties.count { it in declaredPropertyNames }
}
// returns the amount of properties in the json node. if a property has a default value but is not declared in the
// node it will still affect the counter
fun JsonNode.countAllProperties(propertiesWithDefault: List): Int {
val declaredPropertyNames = properties().map { it.key }.toMutableSet()
declaredPropertyNames.addAll(propertiesWithDefault)
return declaredPropertyNames.size
}
@Suppress("unused")
fun Maybe.asList(): Maybe?> = onNotNull {
when (this.value) {
is NullNode -> success(null)
is ArrayNode -> success(this.value.toList())
else -> Maybe.Failure(context, ValidationError("is not a valid json array", context, ErrorKind.Incompatible))
}
}
@Suppress("unused")
@JvmName("asStringFromJson")
fun Maybe.asString() = onNotNull {
when (value) {
is NullNode -> Maybe.Success(context, null)
is ObjectNode, is ArrayNode -> Maybe.Failure(context, ValidationError("is not a string", context, ErrorKind.Incompatible))
else -> Maybe.Success(context, value.asText())
}
}
@Suppress("unused")
@JvmName("asIntFromJson")
fun Maybe.asInt() = asString().asInt()
@Suppress("unused")
@JvmName("asUIntFromJson")
fun Maybe.asUInt() = asString().asUInt()
@Suppress("unused")
@JvmName("asLongFromJson")
fun Maybe.asLong() = asString().asLong()
@Suppress("unused")
@JvmName("asULongFromJson")
fun Maybe.asULong() = asString().asULong()
@Suppress("unused")
@JvmName("asBigIntegerFromJson")
fun Maybe.asBigInteger() = asString().asBigInteger()
@Suppress("unused")
@JvmName("asFloatFromJson")
fun Maybe.asFloat() = asString().asFloat()
@Suppress("unused")
@JvmName("asDoubleFromJson")
fun Maybe.asDouble() = asString().asDouble()
@Suppress("unused")
@JvmName("asBigDecimalFromJson")
fun Maybe.asBigDecimal() = asString().asBigDecimal()
@Suppress("unused")
@JvmName("asBooleanFromJson")
fun Maybe.asBoolean() = asString().asBoolean()
@Suppress("unused")
@JvmName("asByteArrayFromJson")
fun Maybe.asByteArray() = asString().asByteArray()
© 2015 - 2025 Weber Informatics LLC | Privacy Policy