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

com.mercateo.jsonschema.mapper.SchemaPropertyMapper.kt Maven / Gradle / Ivy

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

import com.fasterxml.jackson.databind.node.ObjectNode
import com.mercateo.jsonschema.property.Property
import com.mercateo.jsonschema.property.PropertyType
import com.mercateo.jsonschema.schema.ObjectContext
import com.mercateo.jsonschema.schema.mapper.*

class SchemaPropertyMapper(private val referencedElements: Set) {

    internal var nodeFactory = ObjectNodeFactory.nodeFactory

    private val primitivePropertyMappers = mapOf(Pair(PropertyType.STRING, StringJsonPropertyMapper(nodeFactory)),
            Pair(PropertyType.INTEGER, IntegerJsonPropertyMapper(nodeFactory)),
            Pair(PropertyType.NUMBER, NumberJsonPropertyMapper(nodeFactory)),
            Pair(PropertyType.BOOLEAN, BooleanJsonPropertyMapper(nodeFactory)),
            Pair(PropertyType.ARRAY, ArrayJsonPropertyMapper(this, nodeFactory)),
            Pair(PropertyType.OBJECT, ObjectJsonPropertyMapper(this, nodeFactory))
    )

    fun  toJson(context: ObjectContext): ObjectNode {

        val propertyDescriptor = context.propertyDescriptor

        if (context.reference != null) {
            val result = nodeFactory.objectNode()
            result.put("\$ref", context.reference)
            return result
        } else {
            val propertyNode = primitivePropertyMappers.get(propertyDescriptor.propertyType)!!.toJson(context)
            val name = context.property.path

            if (referencedElements.contains(name)) {
                propertyNode.put("id", name)
            }

            return propertyNode
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy