Data conversion
The DataConversion plugin allows you to serialize and deserialize a list of values. By default, Ktor handles primitive types and enums through the DefaultConversionService. You can extend this service to handle additional types by installing and configuring the DataConversion
plugin.
If you are using the Locations plugin and want to support custom types in the Location
class parameters, you can utilize the DataConversion
plugin to handle those types.
Add dependencies
To use DataConversion
, you need to include the ktor-server-data-conversion
artifact in the build script:
Install DataConversion
To install the DataConversion
plugin to the application, pass it to the install
function in the specified module. The code snippets below show how to install DataConversion
...
... inside the
embeddedServer
function call.... inside the explicitly defined
module
, which is an extension function of theApplication
class.
Add converters
You can define type conversions within the DataConversion
configuration. Provide a convert<T>
method for the specified type and use the available functions to serialize and deserialize a list of values:
Use the
decode()
function to deserialize a list of values. It takes a list of strings, representing repeated values in the URL and returns the decoded value.decode { values -> // converter: (values: List<String>) -> Any? //deserialize values }Use the
encode()
function to serialize a value. The function takes an arbitrary value and returns a list of strings representing it.encode { value -> // converter: (value: Any?) -> List<String> //serialize value }
Access the service
You can access the DataConversion
service from the current context:
You can then use the converter service to call the callback functions:
The
fromValues(values: List<String>, type: TypeInfo)
callback function acceptsvalues
as a list of strings, and theTypeInfo
to convert the value to and returns the decoded value.The
toValues(value: Any?)
callback function accepts an arbitrary value and returns a list of strings representing it.
Example
In the following example, a converter for the type LocalDate
is defined and configured to serialize and deserialize values. When the encode
function is called, the service will convert the value using a SimpleDateFormat
and return a list containing the formatted value. When the decode
function is called, the service will format the date as a LocalDate
and return it.
The conversion service can then be called manually to retrieve the encoded and decoded values:
Customize enum serialization with Locations
Another potential use case is to customize how a specific enum
is serialized. By default, enums are serialized and deserialized using their name
attribute in a case-sensitive fashion. But you can, for example, serialize them as lower case and deserialize them as case-insensitive, as seen in the following example:
For the full example, see data-conversion