Skip to content

Persisting custom objects #42

@Miha-x64

Description

@Miha-x64

Hello again :)
What about supporting pref values, which could be presisted into String by custom converter?

object AppState { // client code
    val user by customPref<User?>(default = null, converter = UserConverter)
}

interface PrefConverter<T> { // library code
    fun fromPref(str: String): T
    fun toPref(t: T): String
}

object UserConverter : PrefConverter<User> { // client code
    private val gson = Gson()
    override fun fromPref(str: String) = gson.fromJson(str, User::class.java)
    override fun toPref(t: T) = gson.toJson(t)
}

Many ORMs (Hibernate, GreenDAO, ORMLite, ObjectBox, etc) do this for object's fields, so we can borrow some architectural solutions from them.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions