Last active
December 28, 2019 07:41
-
-
Save michpohl/83795b9880cb847b1c78d81ea922e10f to your computer and use it in GitHub Desktop.
JsonDataClass, enabling all sub (data) classes to be converted to Json and from Json unsing Moshi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * All sub classes must be data classes! | |
| */ | |
| open class JsonDataClass { | |
| fun toJsonString(): String { | |
| return Moshi.Builder().build().adapter(this.javaClass).toJson(this) | |
| } | |
| companion object { | |
| inline fun <reified T : JsonDataClass> fromJsonString(string: String): T? { | |
| return Moshi.Builder().build().adapter(T::class.java).fromJson(string) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment