org.mattshoe.shoebox.kdux.gson.StoreDslExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Kdux-gson Show documentation
Show all versions of Kdux-gson Show documentation
Kdux-gson is an add-on to the Kdux library to add built-in support for Gson serialization.
package org.mattshoe.shoebox.kdux.gson
import com.google.gson.Gson
import kdux.dsl.StoreDslMenu
val _gsonFallback by lazy { Gson() }
val _charSet = Charsets.UTF_8
inline fun StoreDslMenu.persistWithGson(
key: String,
gson: Gson = _gsonFallback,
noinline onError: (State?, Throwable) -> Unit = { _, _ -> }
) {
persist(
key,
serializer = { state, outputStream ->
outputStream.write(
gson.toJson(state).toByteArray(_charSet)
)
},
deserializer = {
gson.fromJson(it.reader(_charSet), State::class.java)
},
onError
)
}