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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- android

  ```
  object PreferencesFileSerializer : Serializer<Preferences>
  ```

*** ** * ** ***

Proto based serializer for Preferences. Can be used to manually create `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` using the `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStoreFactory#create(androidx.datastore.core.Storage,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)` function.

## Summary

| ### Public functions |
|---|---|---|
| `open suspend https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesFileSerializer#readFrom(java.io.InputStream)(input: https://developer.android.com/reference/java/io/InputStream.html)` Unmarshal object from stream. | android |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferencesFileSerializer#writeTo(androidx.datastore.preferences.core.Preferences,java.io.OutputStream)(t: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences, output: https://developer.android.com/reference/java/io/OutputStream.html)` Marshal object to a stream. | android |

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

## Public functions

### readFrom

android

```
open suspend fun readFrom(input: InputStream): Preferences
```

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

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

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

| Parameters |
|---|---|
| `t: https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences` | 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.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

```
open val defaultValue: Preferences
```

Value to return if there is no data on disk.