# kotlinx.serialization

Decode @Serializable classes directly from JSON on the JVM.


The `simdjson-kotlin-serialization` module plugs simdjson-kotlin into
[kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization): decode
JSON straight into your `@Serializable` classes, with simdjson doing the parsing.

<aside class="kt-callout kt-callout--warning">
  <svg class="kt-callout__icon" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10.3 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0Z"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>
  <div class="kt-callout__body"><strong class="kt-callout__title">JVM only</strong><div class="kt-callout__content">The serialization module is currently <strong>JVM-only</strong>. Add it to your <code>jvmMain</code>
source set (see <a href="/simdjson-kotlin/getting-started/installation/index.md">Installation</a>). The
<a href="../dom/">DOM</a> and <a href="../on-demand/">On-Demand</a> APIs in the core module work on
every supported target.</div>
  </div>
</aside>


## Decoding

Annotate your models with `@Serializable`, create a `SimdJson` instance, and
decode:

```kotlin
import io.github.devcrocod.simdjson.serialization.SimdJson
import kotlinx.serialization.Serializable

@Serializable
data class Twitter(val statuses: List<Status>)

@Serializable
data class Status(val user: User)

@Serializable
data class User(val default_profile: Boolean, val screen_name: String)

val json: String = loadTwitterJson()

val simdJson = SimdJson { ignoreUnknownKeys = true }
for (status in simdJson.decodeFromString<Twitter>(json).statuses) {
    if (status.user.default_profile) {
        println(status.user.screen_name)
    }
}
```

To decode from a UTF-8 `ByteArray` (skipping the `String` step), use
`decodeFromByteArray` with an explicit serializer:

```kotlin
import kotlinx.serialization.serializer

val twitter = simdJson.decodeFromByteArray(serializer<Twitter>(), bytes)
```

## Configuration

`SimdJson { }` builds an instance via a builder, mirroring kotlinx.serialization's
`Json { }`:

<table class="kt-params">
  <thead>
    <tr><th>Parameter</th><th>Description</th></tr>
  </thead>
  <tbody><tr>
          <td>
            <code class="kt-params__name">ignoreUnknownKeys</code><span class="kt-params__type">Boolean</span></td>
          <td>Skip JSON keys with no matching property instead of failing. Default <code>false</code>.</td>
        </tr><tr>
          <td>
            <code class="kt-params__name">isLenient</code><span class="kt-params__type">Boolean</span></td>
          <td>Relax the input grammar when reading values. Default <code>false</code>.</td>
        </tr><tr>
          <td>
            <code class="kt-params__name">coerceInputValues</code><span class="kt-params__type">Boolean</span></td>
          <td>Substitute the property default for a missing or <code>null</code>-for-non-null value. Default <code>false</code>.</td>
        </tr><tr>
          <td>
            <code class="kt-params__name">serializersModule</code><span class="kt-params__type">SerializersModule</span></td>
          <td>Register contextual or polymorphic serializers.</td>
        </tr></tbody>
</table>


## Decoding only

<aside class="kt-callout kt-callout--important">
  <svg class="kt-callout__icon" viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 2 4 5v6c0 5 3.5 9 8 11 4.5-2 8-6 8-11V5l-8-3z"/><path d="M12 8v4"/><path d="M12 16h.01"/></svg>
  <div class="kt-callout__body"><strong class="kt-callout__title">Parser, not a full format</strong><div class="kt-callout__content">simdjson-kotlin is a <strong>parser</strong>: <code>SimdJson</code> decodes only. <code>encodeToString</code> throws
<code>UnsupportedOperationException</code>. Use
<a href="https://github.com/Kotlin/kotlinx.serialization"><code>kotlinx.serialization.json</code></a>
for encoding. Decoding failures throw <code>SimdJsonDecodingException</code> (a
<code>kotlinx.serialization.SerializationException</code>).</div>
  </div>
</aside>

