# Serializer

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- android

  ```
  interface Serializer<T : Any?>
  ```
Known direct subclasses [AeadSerializer](https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer), [PreferencesFileSerializer](https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesFileSerializer)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/tink/AeadSerializer` | A `https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer` implementation that provides transparent encryption and decryption using Tink's `https://javadoc.io/doc/com.google.crypto.tink/tink/latest/com/google/crypto/tink/Aead.html` (Authenticated Encryption with Associated Data). |
| `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesFileSerializer` | Proto based serializer for Preferences. |

*** ** * ** ***

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

The type `https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer` 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/Serializer#readFrom(java.io.InputStream)(input: https://developer.android.com/reference/java/io/InputStream.html)` Unmarshal object from stream. | android |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer#writeTo(kotlin.Any,java.io.OutputStream)(t: T, output: https://developer.android.com/reference/java/io/OutputStream.html)` Marshal object to a stream. | android |

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

## Public functions

### readFrom

android

```
suspend fun readFrom(input: InputStream): T
```

Unmarshal object from stream.

| Parameters |
|---|---|
| `input: https://developer.android.com/reference/java/io/InputStream.html` | the InputStream 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/Serializer#readFrom(java.io.InputStream)` is corrupted and/or unparseable, e.g. `https://protobuf.dev/reference/java/api-docs/com/google/protobuf/InvalidProtocolBufferException.html` when the type `https://developer.android.com/reference/kotlin/androidx/datastore/core/Serializer` 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

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

```
suspend fun writeTo(t: T, output: OutputStream): Unit
```

Marshal object to a stream. Closing the provided OutputStream is a no-op.

| Parameters |
|---|---|
| `t: T` | the data to write to output |
| `output: https://developer.android.com/reference/java/io/OutputStream.html` | the OutputStream to serialize data to |

## Public properties

### defaultValue

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

```
val defaultValue: T
```

Value to return if there is no data on disk.