In this article, we will learn how to use the Kotlin extension functions to provide a simple and elegant type conversion mechanism.
Maybe you have used Sling Apache before. In this case, you probably know Use of adapter slings. We will be implementing a very similar approach in Kotlin.
Creating an extension function
With Kotlins extension functions, we can add methods to existing classes. The following declaration adds an adaptTo (..) method to all subtypes of Any.
1 2 3 |
|
The passed targetType parameter specifies the type of target that should be returned by the method. We are keeping the method body empty for now.
Converting an object of type A to another object of type B will look like this with our new method:
1 2 |
|
Provide conversion rules with adapters
In order to implement the adaptTo (..) method, we need a way to define conversion rules.
For this, we use a simple Adapter interface:
1 2 3 4 |
|
canAdapt (..) returns true when the implementing class is able to convert the object from to type to.
adaptTo (..) does the actual conversion and returns an object of type to.
Finding a suitable adapter
Our adaptTo (..) extension function needs a way to access the available adapters. So we create a simple list that stores our adapter implementations:
1 |
|
In the extension function, we can now search the list of adapters for a suitable adapter:
1 2 3 4 5 6 7 8 |
|
If an adapter is found that can be used for the requested conversion, we call adaptTo (..) from the adapter and return the result. If no suitable adapter is found, a NoSuitableAdapterFoundException exception is thrown.
Example of use
Suppose we want to convert JSON strings to Kotlin objects using the Jackson JSON Library. A simple adapter might look like this:
01 02 03 04 05 06 07 08 09 ten |
|
We can now use our new extension method to convert a JSON string to a Person object:
01 02 03 04 05 06 07 08 09 ten 11 12 13 14 15 16 17 18 |
|
You can find the source code of the examples on GitHub.
In adapters.kt you will find all the necessary parts in case you want to try it out for yourself. In example-usage.kt you will find adapter implementations and usage examples.