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

de.lancom.openapi.jackson.Util.kt Maven / Gradle / Ivy

Go to download

This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes

There is a newer version: 2.1.1
Show newest version
package de.lancom.openapi.jackson

import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator

fun JsonNode.booleanValueOrErrorOpt(): Boolean? {
    if (isNull) {
        return null
    }
    return booleanValueOrError()
}

fun JsonNode.booleanValueOrError(): Boolean {
    require(isBoolean) {
        "a boolean node is required"
    }
    return booleanValue()
}

fun JsonNode.numberValueOrError(): Number {
    require(isNumber) {
        "a number node is required"
    }
    return numberValue()
}

fun JsonNode.textValueOrError(): String {
    require(isTextual) {
        "a textural node is required"
    }
    return textValue()
}

fun JsonNode.textValueOrErrorOpt(): String? {
    if (isNull) {
        return null
    }
    return textValueOrError()
}

fun > C?.takeUnlessEmpty(): C? {
    return if (isNullOrEmpty()) {
        null
    } else {
        this
    }
}

fun  Map?.takeUnlessEmpty(): Map? {
    return if (isNullOrEmpty()) {
        null
    } else {
        this
    }
}

fun createObjectMapper(yaml: Boolean): ObjectMapper {
    return ObjectMapper(
        if (yaml) {
            YAMLFactory()
                .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
                .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
                .enable(YAMLGenerator.Feature.SPLIT_LINES)
                .enable(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS)
        } else {
            JsonFactory()
        }
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy