JKid is a simple JSON serialization/deserialization library for Kotlin data classes. To serialize or deserialize an object use the 'serialize' and 'deserialize' functions.
This project accompanies the Chapter 10, "Introspecting Kotlin Code" of the Kotlin in Action book.
The section "Declaring and Applying Annotations" of the Chapter 10 describes annotations in Kotlin, and also shows how to use the library and how to tune it with annotations.
You can find examples for this section in the folder test/kotlin/examples
.
Five examples correspond to five subsections accordingly.
The file kotlin/Annotations.kt
contains the declarations of discussed annotations.
The section "Reflection: Introspecting Kotlin Objects at Runtime" of the book describes the implementation of serializer and deserializer.
The files main/kotlin/serialization/Serializer.kt
and main/kotlin/deserialization/Deserializer.kt
contain the source code.
We highly encourage you to do the following exercises after reading the text. The first exercise can be started after reading the description of the serializer, to be exact after reading the section "Customizing Serialization with Annotations". The second exercise is intended to be started after reading the whole chapter. Solving these exercises will help you to understand the concepts better and lets you practice right away.
-
Support the annotation
DateFormat
, that allows to annotate the date property with@DateFormat("dd-MM-yyyy")
specifying the date format as an argument. The testing example showing its usage is in the filetest/kotlin/exercise/DateFormatAnnotation.kt
. Remove@Ignore
from the testDateFormatTest
and make it pass. The declaration of the annotation is in the filemain/kotlin/exercise/DateFormat.kt
. The solution to this exercise can be found in the branchsolution-date-format
. You can use the action "compare with Branch..." to compare your solution with the suggested one. -
Make JKid support serialization and deserialization of maps as property values. For now it supports only objects and collections. The example is in the file
test/kotlin/exercise/Map.kt
. Remove@Ignore
from the testMapTest
and make it pass. To support deserialization of maps, create a classMapSeed
similar toObjectSeed
and collection seeds. The functioncreateSeedForType
should now return an instance ofMapSeed
if a map is expected. The example solution can be found in the branchsolution-map
.