Skip to content

Instantly share code, notes, and snippets.

@michpohl
Last active December 28, 2019 07:41
Show Gist options
  • Select an option

  • Save michpohl/83795b9880cb847b1c78d81ea922e10f to your computer and use it in GitHub Desktop.

Select an option

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
/**
* 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