Kotlin Annotation

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade