# PreferencesSerializer

Artifact: [androidx.datastore:datastore-preferences-core](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/preferences/core/PreferencesSerializer.+class:androidx.datastore.preferences.core.PreferencesSerializer) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

*** ** * ** ***


```
public static class PreferencesSerializer implements OkioSerializer<@NonNull Preferences>
```

<br />

*** ** * ** ***

Proto based serializer for Preferences. Can be used to manually create `https://developer.android.com/reference/androidx/datastore/core/DataStore` using the `https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory#create(androidx.datastore.core.Storage,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)` function.

## Summary

| ### Public fields |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer#INSTANCE()` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer#getDefaultValue()()` Value to return if there is no data on disk. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer#readFrom(okio.BufferedSource)(@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-source/index.html source)` Unmarshal object from source. |
| `void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer#writeTo(androidx.datastore.preferences.core.Preferences,okio.BufferedSink)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences t, @https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html sink)` Marshal object to a Sink. |

## Public fields

### INSTANCE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

```
public @NonNull PreferencesSerializer INSTANCE
```

## Public methods

### getDefaultValue

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

```
public @NonNull Preferences getDefaultValue()
```

Value to return if there is no data on disk.

### readFrom

```
public @NonNull Preferences readFrom(@NonNull BufferedSource source)
```

Unmarshal object from source.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-source/index.html source` | the BufferedSource with the data to deserialize |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/datastore/core/CorruptionException` | if the data from `https://developer.android.com/reference/androidx/datastore/core/okio/OkioSerializer#readFrom(okio.BufferedSource)` is corrupted and/or unparseable, e.g. `InvalidProtocolBufferException` when the type `https://developer.android.com/reference/androidx/datastore/core/okio/OkioSerializer` is a protobuf message and it is corrupted. Other unrecoverable `https://developer.android.com/reference/androidx/datastore/core/IOException` from the file system should not be thrown as `https://developer.android.com/reference/androidx/datastore/core/CorruptionException`. |

### writeTo

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

```
public void writeTo(@NonNull Preferences t, @NonNull BufferedSink sink)
```

Marshal object to a Sink.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences t` | the data to write to output |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html sink` | the BufferedSink to serialize data to |