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

*** ** * ** ***


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

<br />

Known direct subclasses [AeadSerializer](https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer), [PreferencesFileSerializer](https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesFileSerializer)

|---|---|
| `https://developer.android.com/reference/androidx/datastore/tink/AeadSerializer` | A `https://developer.android.com/reference/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/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/androidx/datastore/core/Serializer` 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/Serializer#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/Serializer#readFrom(java.io.InputStream)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/InputStream.html input)` Unmarshal object from stream. |
| `abstract void` | `https://developer.android.com/reference/androidx/datastore/core/Serializer#writeTo(kotlin.Any,java.io.OutputStream)(@https://developer.android.com/reference/androidx/annotation/NonNull T t, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/OutputStream.html output)` Marshal object to a stream. |

## Public methods

### getDefaultValue

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

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

Value to return if there is no data on disk.

### readFrom

```
abstract @NonNull T readFrom(@NonNull InputStream input)
```

Unmarshal object from stream.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/InputStream.html input` | the InputStream 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/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/androidx/datastore/core/Serializer` 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.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

```
abstract void writeTo(@NonNull T t, @NonNull OutputStream output)
```

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

| 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://developer.android.com/reference/java/io/OutputStream.html output` | the OutputStream to serialize data to |