# OkioSerializer

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

*** ** * ** ***


```
public interface OkioSerializer<T extends Object>
```

<br />

Known direct subclasses [PreferencesSerializer](https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer), [StateStoreSerializer](https://developer.android.com/reference/androidx/appstate/datastore/StateStoreSerializer), [WebSerializer](https://developer.android.com/reference/androidx/datastore/core/okio/WebSerializer)

|---|---|
| `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesSerializer` | Proto based serializer for Preferences. |
| `https://developer.android.com/reference/androidx/appstate/datastore/StateStoreSerializer` | Serializer for `https://developer.android.com/reference/androidx/appstate/datastore/StateStorePreferences` using `https://developer.android.com/reference/androidx/datastore/core/DataStore` and Okio. |
| `https://developer.android.com/reference/androidx/datastore/core/okio/WebSerializer` | A Serializer that uses kotlinx.serialization.json to convert any @Serializable data class T to and from a byte stream using Okio. |

*** ** * ** ***

The OkioSerializer determines the on-disk format and API for accessing it.

The type T MUST be immutable. Mutable types will result in broken DataStore functionality.

## Summary

| ### Public methods |
|---|---|
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/datastore/core/okio/OkioSerializer#getDefaultValue()()` Value to return if there is no data on disk. |
| `abstract @https://developer.android.com/reference/androidx/annotation/NonNull T` | `https://developer.android.com/reference/androidx/datastore/core/okio/OkioSerializer#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. |
| `abstract void` | `https://developer.android.com/reference/androidx/datastore/core/okio/OkioSerializer#writeTo(kotlin.Any,okio.BufferedSink)(@https://developer.android.com/reference/androidx/annotation/NonNull T 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 methods

### getDefaultValue

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

```
abstract @NonNull T getDefaultValue()
```

Value to return if there is no data on disk.

### readFrom

```
abstract @NonNull T 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)

```
abstract void writeTo(@NonNull T t, @NonNull BufferedSink sink)
```

Marshal object to a Sink.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull T 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 |