Kotlin Annotation
Jul 10, 2017 · 1 min read
For person who want to write your own library in Java / Kotlin
note: In java might be little different from example.
Chapter 1
Data class from `JSON` (Example using Github-API)
First of all start create Data class (in my case using Owner User)
and annotation by name JsonKey with 1 parameter is String which is key in json
Use your favorite library to manage with json object
In my case I choose `com.beust:klaxon:0.30`
since in this case might have to create more than 1 class so I use builder pattern to build the object to me.
Starting Create Builder Class
Code Explanation
// unsafe cast null object
tClass.primaryConstructor!!// return single element if not (have more than 1) return null
List<T>.singleOrNull();// as keyword mean `cast`
obj as JsonKey// normally the return keyword will return nearest function which is `build` so @ (Label) will resolve this problem
return@filter
Thank you for reading
Kamontat Chantrachirathumrong
