V2 Example Serialization - SMILEY4/ktor-openapi-tools GitHub Wiki
json-serialization (for examples) is the conversion from any value (of a given type kotlin.reflect.KType
) to a json-string. This behavior can be customized, overwritten or completely replaced in the plugin-config encoding
-section.
This project uses jackson as the default json-serializer. The configuration of the object-mapper can be customized freely via the "EncodingData".
EncodingData.DEFAULT_EXAMPLE_OBJECT_MAPPER = jacksonObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
The default json-serializer can be completely replaced by another one in the plugin-config encoding
-section.
install(SwaggerUI) {
encoding {
exampleEncoder { type, value ->
// convert 'value' to json and return as string
}
}
}
-
exampleEncoder
- converts the given value (of the specified typetype
) to a valid json-string
It is also possible to only replace the serializer for specific types.
exampleEncoder { type, value ->
when (type) {
getSchemaType<String>() -> value.toString() // custom "serializer" for strings
else -> EncodingData.encodeExample(value) // use default serializer for everything else
}
}
An example replacing jackson with kotlinx can be found here.