# 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)

*** ** * ** ***

Common/AllJavaScript
- Cmn

  ```
  interface OkioSerializer<T : Any?>
  ```
Known direct subclasses [PreferencesSerializer](https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesSerializer), [StateStoreSerializer](https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStoreSerializer), [WebSerializer](https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/WebSerializer)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesSerializer` | Proto based serializer for Preferences. |
| `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStoreSerializer` | Serializer for `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStorePreferences` using `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` and Okio. |
| `https://developer.android.com/reference/kotlin/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 functions |
|---|---|---|
| `suspend T` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/OkioSerializer#readFrom(okio.BufferedSource)(source: https://lysine.dev/okio/3.x/okio/okio/-buffered-source/index.html)` Unmarshal object from source. | Cmn |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/OkioSerializer#writeTo(kotlin.Any,okio.BufferedSink)(t: T, sink: https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html)` Marshal object to a Sink. | Cmn |

| ### Public properties |
|---|---|---|
| `T` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/OkioSerializer#defaultValue()` Value to return if there is no data on disk. | Cmn |

## Public functions

### readFrom

Cmn

```
suspend fun readFrom(source: BufferedSource): T
```

Unmarshal object from source.

| Parameters |
|---|---|
| `source: https://lysine.dev/okio/3.x/okio/okio/-buffered-source/index.html` | the BufferedSource with the data to deserialize |

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

### writeTo

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

```
suspend fun writeTo(t: T, sink: BufferedSink): Unit
```

Marshal object to a Sink.

| Parameters |
|---|---|
| `t: T` | the data to write to output |
| `sink: https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html` | the BufferedSink to serialize data to |

## Public properties

### defaultValue

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

```
val defaultValue: T
```

Value to return if there is no data on disk.