All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mattshoe.shoebox.kdux.gson.StoreDslExtensions.kt Maven / Gradle / Ivy

Go to download

Kdux-gson is an add-on to the Kdux library to add built-in support for Gson serialization.

There is a newer version: 1.0.10
Show newest version
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
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy