# PreferencesFileSerializer

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/PreferencesFileSerializer.jvmAndAndroid.kt+class:androidx.datastore.preferences.core.PreferencesFileSerializer) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

*** ** * ** ***


```
public static class PreferencesFileSerializer implements Serializer<@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 |
|---|---|
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesFileSerializer` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesFileSerializer#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/PreferencesFileSerializer#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/PreferencesFileSerializer#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. |
| `void` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesFileSerializer#writeTo(androidx.datastore.preferences.core.Preferences,java.io.OutputStream)(@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://developer.android.com/reference/java/io/OutputStream.html output)` Marshal object to a stream. |

## Public fields

### INSTANCE

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

```
public static @NonNull PreferencesFileSerializer INSTANCE
```

## Public methods

### getDefaultValue

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

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

Value to return if there is no data on disk.

### readFrom

```
public @NonNull Preferences 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.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

```
public void writeTo(@NonNull Preferences 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 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://developer.android.com/reference/java/io/OutputStream.html output` | the OutputStream to serialize data to |