# Kotlin JSON Benchmark on Android (2022): Moshi vs Kotlin Serialization

DevFeed: [Kotlin JSON Benchmark on Android (2022): Moshi vs Kotlin Serialization](<https://devfeed.tech/articles/kotlin-json-benchmark-on-android-2022-moshi-vs-kotlin-serialization-25881.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/kotlin-json-benchmark-on-android-2022-moshi-vs-kotlin-serialization-18436c0596c3?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2022-10-14T17:31:03Z

Content type: comparison

Language: en

Sources: [Stories by Christophe Beyls on Medium](<https://devfeed.tech/sources/stories-by-christophe-beyls-on-medium.md>)

Topics: [JSON](<https://devfeed.tech/topics/json.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [Gson](<https://devfeed.tech/topics/gson.md>), [build times](<https://devfeed.tech/topics/build-times.md>), [Code generation](<https://devfeed.tech/topics/code-generation.md>), [legacy](<https://devfeed.tech/topics/legacy.md>)

Tags: [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [annotation-processor](<https://devfeed.tech/tags/annotation-processor.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [gson](<https://devfeed.tech/tags/gson.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-serialization](<https://devfeed.tech/tags/kotlin-serialization.md>), [legacy](<https://devfeed.tech/tags/legacy.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [performance](<https://devfeed.tech/tags/performance.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [vs](<https://devfeed.tech/tags/vs.md>)

## AI overview

This article sets up a 2022 Android benchmark comparing Moshi and Kotlin Serialization for JSON serialization and deserialization of Kotlin classes. It discusses Kotlin metadata compatibility, generated adapters, reflection, streaming support, Okio integration, runtime dependencies, and build-time tradeoffs. The supplied text does not include the benchmark results or identify which library is fastest.

## Source excerpt

When it comes to automatic serialization and deserialization of Kotlin classes using the JSON format, the two main libraries compatible with Kotlin metadata are currently Moshi and Kotlin Serialization. This compatibility is especially important for non-null types and default values during deserialization, where lack of proper Kotlin support could result in unexpected values occurring at runtime, such as null values in non-null fields. If you're still using legacy libraries like Gson to parse Kotlin classes, it's time to reconsider. Moshi has been supporting Kotlin classes since version 1.5.0, released in 2017. One year later, the next major release 1.6.0 added an annotation processor to generate adapters for Kotlin classes at compile time. I was quite interested in the performance gains allowed by this solution and wrote an article detailing what the generated code does. JetBrains released version 1.0.0 of Kotlin Serialization in 2020 with built-in support for JSON. This library generates adapters at compile time similarly to Moshi's annotation processor while being compatible with more platforms and formats. The lack of initial support for streaming was disappointing, so I didn't even consider using it in production until streaming support was eventually added in version 1.3.0 in 2021. The JSON engine had also been rewritten in the meantime in order to improve performance. Recently, version 1.4.0 added integration with the Okio library, the same library used by Moshi under the hood. Now that Kotlin Serialization looks full-featured and well-optimized, I thought it would be a good time to compare its performance against Moshi on Android devices with some benchmarks. Which one is the fastest? Take your bets. The contendersMoshi-Kotlin Reflection The runtime Kotlin plugin of the Moshi library. The JSON adapters are generated at runtime using reflection. The main downside of this library is that it adds a runtime dependency to the big kotlin-reflect library (currently