# WebSerializer

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/WebSerializer.web.kt+class:androidx.datastore.core.okio.WebSerializer) Common/AllJavaScript
- JS

  ```
  class WebSerializer<T : Any?> : OkioSerializer<T>
  ```

*** ** * ** ***

A Serializer that uses kotlinx.serialization.json to convert any @Serializable data class T to and from a byte stream using Okio.

| Parameters |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | The data type to serialize. Must be annotated with @Serializable. |

## Summary

| ### Public constructors |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/WebSerializer#WebSerializer(kotlinx.serialization.KSerializer,kotlin.Any)(kSerializer: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-k-serializer/index.html<T>, defaultValue: T)` | JS |

| ### Public functions |
|---|---|---|
| `open suspend T` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/WebSerializer#readFrom(okio.BufferedSource)(source: https://lysine.dev/okio/3.x/okio/okio/-buffered-source/index.html)` Reads a UTF-8 string from the source, and then parses that string back into an object of type T using JSON. | JS |
| `open suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/WebSerializer#writeTo(kotlin.Any,okio.BufferedSink)(t: T, sink: https://lysine.dev/okio/3.x/okio/okio/-buffered-sink/index.html)` Encodes the object of type T into a JSON string, and writes that UTF-8 string to the sink. | JS |

| ### Public properties |
|---|---|---|
| `open T` | `https://developer.android.com/reference/kotlin/androidx/datastore/core/okio/WebSerializer#defaultValue()` The default instance of T to return if data is corrupted or doesn't exist. | JS |

## Public constructors

### WebSerializer

JS

```
<T : Any?> WebSerializer(kSerializer: KSerializer<T>, defaultValue: T)
```

| Parameters |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?>` | The data type to serialize. Must be annotated with @Serializable. |
| `kSerializer: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-k-serializer/index.html<T>` | The KSerializer for type T, usually accessed via `T.serializer()`. |
| `defaultValue: T` | The default instance of T to return if data is corrupted or doesn't exist. |

## Public functions

### readFrom

JS

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

Reads a UTF-8 string from the source, and then parses that string back into an object of type T using JSON.

### writeTo

JS

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

Encodes the object of type T into a JSON string, and writes that UTF-8 string to the sink.

## Public properties

### defaultValue

JS

```
open val defaultValue: T
```

The default instance of T to return if data is corrupted or doesn't exist.