Skip to content

V2 Kotlinx Multiplatform Encoder Example

Lukas Ruegner edited this page Jun 2, 2024 · 1 revision

The default schema-generator and json-serializer may not support kotlinx or multiplatform. This example shows how to replace these with one compatible with kotlinx and multiplatform.

jackson and victools/jsonschema-generator will be replaced with Kotlin.serialization and tillersystems/json-schema-serialization.

Replacing the Example Json-Serializer

Create a new Json-instance used for serializing. The same instance can also be used for schema-generation.

val json = Json {
    prettyPrint = true
    encodeDefaults = true
}

The default json-serializer is replaced with Kotlin.serialization

install(SwaggerUI) {
    encoding {
        exampleEncoder { type, value ->
            json.encodeToString(serializer(type!!), value)
	}
        //...
    }
}

Replacing the Schema-Generator

Create a new Json-instance used for schema-generation. The same instance can also be used for json-serialization.

val json = Json {
    prettyPrint = true
    encodeDefaults = true
}

The default schema-generator is replaced with tillersystems/json-schema-serialization

install(SwaggerUI) {
    encoding {
    	//...
        schemaEncoder { type ->
            json.encodeToSchema(serializer(type), generateDefinitions = false)
	}
        schemaDefinitionsField = "definitions"
    }
}
Clone this wiki locally