Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@wilinz
Created November 27, 2023 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilinz/4bb26bf5b23e0caa80ce0cbbd7d1312d to your computer and use it in GitHub Desktop.
Save wilinz/4bb26bf5b23e0caa80ce0cbbd7d1312d to your computer and use it in GitHub Desktop.
MoshiExt.kt
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.Moshi
import okio.BufferedSink
import okio.BufferedSource
inline fun <reified T> Moshi.adapter(): JsonAdapter<T> = adapter(T::class.java)
inline fun <reified T> Moshi.toJson(v: T): String = adapter<T>().toJson(v)
inline fun <reified T> Moshi.toJson(sink: BufferedSink, v: T) = adapter<T>().toJson(sink, v)
inline fun <reified T> Moshi.toJson(writer: JsonWriter, v: T) = adapter<T>().toJson(writer, v)
inline fun <reified T> Moshi.fromJson(json: String) = adapter<T>().fromJson(json)
inline fun <reified T> Moshi.fromJson(json: BufferedSource) = adapter<T>().fromJson(json)
inline fun <reified T> Moshi.fromJson(json: JsonReader) = adapter<T>().fromJson(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment