-
- All Implemented Interfaces:
-
kotlinx.serialization.SerialFormat
public class Preferences implements SerialFormat
Serializes and deserializes class properties into SharedPreferences consisting of string keys and primitive type values.
@Serializable data class Person(val name: String, val age: Int) val preferences = Preferences(sharedPreferences) val abby = Person("Abby", 20) preferences.encode("person", abby) assertEquals("Abby", sharedPreferences.getString("person.name", null)) assertEquals(20, sharedPreferences.getInt("person.age", 0))
-
-
Field Summary
Fields Modifier and Type Field Description private final SerializersModuleserializersModule
-
Method Summary
Modifier and Type Method Description SerializersModulegetSerializersModule()final <T extends Any> Unitencode(SerializationStrategy<T> serializer, String tag, T value)Serializes and encodes the given value into the SharedPreferences at the specified tag using the given serializer. final <T extends Any> Tdecode(DeserializationStrategy<T> deserializer, String tag)Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using the given deserializer -
-
Method Detail
-
getSerializersModule
SerializersModule getSerializersModule()
-
encode
final <T extends Any> Unit encode(SerializationStrategy<T> serializer, String tag, T value)
Serializes and encodes the given value into the SharedPreferences at the specified tag using the given serializer.
- Parameters:
serializer- strategy used to encode the datatag- key to encode data tovalue- value to encode
-
decode
final <T extends Any> T decode(DeserializationStrategy<T> deserializer, String tag)
Decodes and deserializes from the SharedPreferences at the specified tag to the value of type T using the given deserializer
- Parameters:
deserializer- strategy used to decode the datatag- key to decode data from
-
-
-
-