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

com.mercateo.jsonschema.schema.mapper.NumberJsonPropertyMapper.kt Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package com.mercateo.jsonschema.schema.mapper

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.DoubleNode
import com.fasterxml.jackson.databind.node.FloatNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import com.mercateo.jsonschema.schema.ObjectContext

internal class NumberJsonPropertyMapper(nodeFactory: JsonNodeFactory) : JsonPropertyMapper {

    private val primitiveJsonPropertyBuilder: PrimitiveJsonPropertyBuilder

    init {
        primitiveJsonPropertyBuilder = PrimitiveJsonPropertyBuilder(nodeFactory)
    }

    override fun toJson(jsonProperty: ObjectContext<*>): ObjectNode {
        return primitiveJsonPropertyBuilder.forProperty(jsonProperty as ObjectContext) //
                .withType("number").withDefaultAndAllowedValues(this::createNode).build()
    }

    private fun createNode(value: Number): JsonNode {
        return when (value) {
            is Float -> FloatNode(value)
            is Double -> DoubleNode(value)
            else -> throw IllegalStateException("cannot create Number node for unknown type")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy