# moshi

Published articles for moshi.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

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

## Unconfined Enums Adapter for Moshi

DevFeed: [Unconfined Enums Adapter for Moshi](<https://devfeed.tech/articles/unconfined-enums-adapter-for-moshi-25914.md>)

Original publisher: [Read original article](<https://medium.com/@xxfast/unconfined-enums-adapter-for-moshi-5d84542c8bf0?source=rss-43bae76e8f81------2>)

Author: Isuru Rajapakse

Published: 2022-05-11T12:08:49Z

Content type: tutorial

Language: en

Sources: [Stories by Isuru Rajapakse on Medium](<https://devfeed.tech/sources/stories-by-isuru-rajapakse-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [LineageOS](<https://devfeed.tech/topics/lineageos.md>), [API](<https://devfeed.tech/topics/api.md>), [enum](<https://devfeed.tech/topics/enum.md>), [interfaces](<https://devfeed.tech/topics/interfaces.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [api](<https://devfeed.tech/tags/api.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [enum](<https://devfeed.tech/tags/enum.md>), [enum-class](<https://devfeed.tech/tags/enum-class.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [retrofit](<https://devfeed.tech/tags/retrofit.md>)

### AI overview

This tutorial presents a Kotlin pattern for handling previously unknown enum-like values from REST APIs with Moshi. It explains Moshi enum adapters, the unknown-value fallback, and an interface-based approach that preserves the server's raw value instead of mapping it only to a generic unknown value.

### Source excerpt

If you wonder what an Unconfined Enum is, wonder no more because it is a thing I just made up. Enums by definition are confined, or finite. Therefore "unconfined enum" is really an oxymoron. Nevertheless, say you have this Fruit enum and want to map each value to an Android string resource. Kotlin's enum properties make this convenient enum class Fruit(@StringRes val stringRes: Int) { Apple(R.string.fruit_apple), Oranges(R.string.fruit_orange) } When we want to consume this enum from a Restful API with a Moshi converter, Moshi automatically generates the enum adapter for you, but if you want to customise the behaviour for whatever reason, you can do object FruitsAdapter { @ToJson fun toJson(type: Fruit): String = type.name @FromJson fun fromJson(name: String): Fruit = Fruit.values().first { it.name == name } } Happy days. Everything is working as expected. When things go bananascom.squareup.moshi.JsonDataException: Expected one of [Apple, Oranges] but was Bananas at path $ Good APIs don't break contracts. Not all APIs are good APIs, so some can break contracts. What do we do now? push out an update with added enum value and its string resource? Perhaps we can make use of EnumJsonAdapter's .withUnknownFallBack() enum class Fruit(@StringRes val stringRes: Int) { Apple(R.string.fruit_apple), Oranges(R.string.fruit_orange), Unknown(R.string.fruit_unknown) } Moshi.Builder() .add(KotlinJsonAdapterFactory()) .add(Fruit::class.java, EnumJsonAdapter.create(Fruit::class.java) .withUnknownFallback(Fruit.Unknown)) .build() This certainly stops the app from crashing, but what if we actually want to show the "bananas" that the API sends? you know, as a fail safe so that users wouldn't end up seeing "unknowns". Let's open up the Enums Enums are by definition finite. Enums are final by design. Enums can't be subclassed but they still can inherit interfaces. We will exploit this to create our "unconfined" enum interface Fruit { val name: String data class Unknown(override val name:

## Going Fully Multiplatform #1: Migrating your Android Project to Kotlin Multiplatform

DevFeed: [Going Fully Multiplatform #1: Migrating your Android Project to Kotlin Multiplatform](<https://devfeed.tech/articles/going-fully-multiplatform-1-migrating-your-android-project-to-kotlin-multiplatform-25912.md>)

Original publisher: [Read original article](<https://medium.com/@xxfast/going-fully-multiplatform-1-migrating-your-android-project-to-kotlin-multiplatform-8fc38763307?source=rss-43bae76e8f81------2>)

Author: Isuru Rajapakse

Published: 2021-10-21T13:24:07Z

Content type: tutorial

Language: en

Sources: [Stories by Isuru Rajapakse on Medium](<https://devfeed.tech/sources/stories-by-isuru-rajapakse-on-medium.md>)

Topics: [Kotlin Multiplatform](<https://devfeed.tech/topics/kotlin-multiplatform.md>), [multiplatform](<https://devfeed.tech/topics/multiplatform.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android](<https://devfeed.tech/topics/android.md>), [iOS](<https://devfeed.tech/topics/ios.md>), [Ktor](<https://devfeed.tech/topics/ktor.md>), [Web](<https://devfeed.tech/topics/web.md>), [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [koin](<https://devfeed.tech/topics/koin.md>), [RxJava](<https://devfeed.tech/topics/rxjava.md>), [navigation](<https://devfeed.tech/topics/navigation.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [caching](<https://devfeed.tech/tags/caching.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [ktor](<https://devfeed.tech/tags/ktor.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [navigation](<https://devfeed.tech/tags/navigation.md>), [okhttp](<https://devfeed.tech/tags/okhttp.md>), [retrofit](<https://devfeed.tech/tags/retrofit.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>)

### AI overview

The article describes migrating an Android-only application to Kotlin Multiplatform. It compares the existing Android stack with a planned multiplatform stack targeting Android, JVM, iOS, and Web, including changes to networking, serialization, caching, dependency injection, asynchronous plumbing, navigation, and UI.

### Source excerpt

Earlier this year I started a mission to start migrating one of our long-lasting Android-only apps to Kotlin Multiplatform to go beyond just Android. This is the story of how that went. Migration from Only-Android to Kotlin Multiplatform!The Android stack This is our current "stack" our android app was set up as follows Targeting Android SDK API 23 or above, we had a few libraries to help us out. The Android-only stack OkHttp for Networking Retrofit for.. also Networking Moshi for serialisation Okio for caching Koin for Dependency Injection/ Service Locator Plumbing with RxJava Navigation with Conductor Imperative UI with Android Views written in XML The Multiplatform stack To go beyond just Android, I had to make some hard decisions to step outside of the comfort zone that we've been so fond of To target Android, JVM, iOS and Web -- we will be making use of The Multiplatform stack Ktor for Networking Kotlin Serialisation for.. serialisation Caching with SQLDelight Koin for Dependency Injection (that one can stay) Plumbing with Coroutine Flows Navigation with Decompose Declarative UI powered with Jetpack/Jetbrains Compose That's the plan! In the next few articles, we'll take a look at each step in more detail. Before jumping on to that -- I want to take a close look at how we can fully modularise a Kotlin Multiplatform project with Going Fully Multiplatform #2 Modularising your Kotlin Multiplatform Project!

## Custom Reactive JSON parsing

DevFeed: [Custom Reactive JSON parsing](<https://devfeed.tech/articles/custom-reactive-json-parsing-38647.md>)

Original publisher: [Read original article](<https://krossovochkin.com/posts/2021_06_08_custom_reactive_json_parsing/>)

Published: 2021-06-08T00:00:00Z

Content type: tutorial

Language: en

Sources: [Vasya Drobushkov](<https://devfeed.tech/sources/vasya-drobushkov.md>)

Topics: [JSON](<https://devfeed.tech/topics/json.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Library](<https://devfeed.tech/topics/library.md>), [Android](<https://devfeed.tech/topics/android.md>), [client](<https://devfeed.tech/topics/client.md>), [multiplatform](<https://devfeed.tech/topics/multiplatform.md>), [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [gson](<https://devfeed.tech/tags/gson.md>), [jackson](<https://devfeed.tech/tags/jackson.md>), [jetpack-compose](<https://devfeed.tech/tags/jetpack-compose.md>), [json](<https://devfeed.tech/tags/json.md>), [json-parsing](<https://devfeed.tech/tags/json-parsing.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [multiplatform](<https://devfeed.tech/tags/multiplatform.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [serialization](<https://devfeed.tech/tags/serialization.md>)

### AI overview

This tutorial introduces JSON parsing in Android applications, compares common libraries, and describes a pet-project weather app that bundles a JSON city list for offline selection. It notes that this approach involves workarounds and would not be suitable for production.

### Source excerpt

Introduction Parsing JSON strings is a must-have on most Android projects that have server-client communication (of course there are some other formats like XML or Protobuf, but I guess JSON is the most common one at least at the moment). Setting up this communication is relatively straightforward: choose a library, add it to a project, describe models, create mappers, and so on. There are a bunch of various libraries to parse JSON: good old Gson, Jackson, or more modern Moshi, kotlinx-serialization and there are more. Set up of these libraries is different, there are differences in the internal implementation, but the general idea is the same for most of them: you can convert JSON-string into a set of objects and vice versa.

## Manually parsing JSON with Moshi

DevFeed: [Manually parsing JSON with Moshi](<https://devfeed.tech/articles/manually-parsing-json-with-moshi-38544.md>)

Original publisher: [Read original article](<https://msfjarvis.dev/posts/manually-parsing-json-with-moshi/>)

Author: Harsh Shandilya

Published: 2020-12-21T06:30:00Z

Content type: tutorial

Language: en

Sources: [Posts on Harsh Shandilya](<https://devfeed.tech/sources/posts-on-harsh-shandilya.md>)

Topics: [JSON](<https://devfeed.tech/topics/json.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Library](<https://devfeed.tech/topics/library.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Java](<https://devfeed.tech/topics/java.md>), [Android](<https://devfeed.tech/topics/android.md>)

Tags: [adapter](<https://devfeed.tech/tags/adapter.md>), [annotation](<https://devfeed.tech/tags/annotation.md>), [codegen](<https://devfeed.tech/tags/codegen.md>), [crash](<https://devfeed.tech/tags/crash.md>), [database](<https://devfeed.tech/tags/database.md>), [json](<https://devfeed.tech/tags/json.md>), [json-parsing](<https://devfeed.tech/tags/json-parsing.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [library](<https://devfeed.tech/tags/library.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [moshi-read-json-from-file](<https://devfeed.tech/tags/moshi-read-json-from-file.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [performance](<https://devfeed.tech/tags/performance.md>), [schema](<https://devfeed.tech/tags/schema.md>)

### AI overview

A tutorial on manually parsing JSON with Moshi, covering adapters, reflection-based parsing, and kapt-backed code generation for Java and Kotlin classes on the JVM and Android.

### Source excerpt

Moshi is a fast and powerful JSON parsing library for the JVM and Android. Today we look into manually parsing JSON to and from Java/Kotlin classes

## Advanced JSON parsing techniques using Moshi and Kotlin

DevFeed: [Advanced JSON parsing techniques using Moshi and Kotlin](<https://devfeed.tech/articles/advanced-json-parsing-techniques-using-moshi-and-kotlin-25877.md>)

Original publisher: [Read original article](<https://bladecoder.medium.com/advanced-json-parsing-techniques-using-moshi-and-kotlin-daf56a7b963d?source=rss-54910f05af37------2>)

Author: Christophe Beyls

Published: 2018-07-30T23:12:56Z

Content type: tutorial

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>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Parser](<https://devfeed.tech/topics/parser.md>), [Android](<https://devfeed.tech/topics/android.md>), [Java](<https://devfeed.tech/topics/java.md>), [Library](<https://devfeed.tech/topics/library.md>), [Streaming](<https://devfeed.tech/topics/streaming.md>)

Tags: [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [code](<https://devfeed.tech/tags/code.md>), [gson](<https://devfeed.tech/tags/gson.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [parsing](<https://devfeed.tech/tags/parsing.md>), [performance](<https://devfeed.tech/tags/performance.md>), [streaming](<https://devfeed.tech/tags/streaming.md>)

### AI overview

A tutorial on parsing JSON with Moshi and Kotlin. It demonstrates manual streaming parsing, handling required and optional properties, skipping unknown values, and optimizing repeated key comparisons using Moshi and Okio.

### Source excerpt

A match made in parser heaven Moshi is a modern JSON library for Android and Java from Square. It can be considered as the successor to GSON, with a simpler and leaner API and an architecture enabling better performance through the use of the Okio library. It's also the most Kotlin-friendly library you can use to parse JSON files, as it comes with Kotlin-aware extensions. In this article I'm going to demonstrate how to take advantage of features from both the Moshi library and the Kotlin language itself in order to write efficient and robust JSON parsers. The example model and JSON file Consider the following model representing a person: class Person(val id: Long, val name: String, val age: Int = -1) id and name are mandatory properties, while the age is optional with a default value of -1. Our objective is to load a list of Person objects in our application from a JSON file or stream with the following contents: [ { "id": 1, "name": "John", "age": 38 }, { "id": 8, "name": "Lisa", "age": 23 }, { "id": 23, "name": "Karen" } ] In this simple example, the JSON object key names exactly match the Person property names and the "age" key is also optional. 1. Fully manual parsing The most basic way of parsing JSON using Moshi is to use the streaming API, which is similar to the streaming API of GSON and the one provided by the Android Framework. This gives you the most control over the parsing process, which is especially useful when the JSON source is dirty. Typical code would look like this: class ManualParser { fun parse(reader: JsonReader): List<Person> { val result = mutableListOf<Person>() reader.beginArray() while (reader.hasNext()) { var id: Long = -1L var name: String = "" var age: Int = -1 reader.beginObject() while (reader.hasNext()) { when (reader.nextName()) { "id" -> id = reader.nextLong() "name" -> name = reader.nextString() "age" -> age = reader.nextInt() else -> reader.skipValue() } } reader.endObject() if (id == -1L || name == "") { throw JsonDataException

## Kotlin's a great language for JSON

DevFeed: [Kotlin's a great language for JSON](<https://devfeed.tech/articles/kotlin-s-a-great-language-for-json-15738.md>)

Original publisher: [Read original article](<https://developer.squareup.com/blog/kotlins-a-great-language-for-json>)

Author: Jesse Wilson

Published: 2017-05-15T17:28:26Z

Content type: article

Language: en

Sources: [Square Corner Blog RSS Feed](<https://devfeed.tech/sources/square-corner-blog-rss-feed.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [JSON](<https://devfeed.tech/topics/json.md>), [Android](<https://devfeed.tech/topics/android.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [build](<https://devfeed.tech/tags/build.md>), [code](<https://devfeed.tech/tags/code.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [json](<https://devfeed.tech/tags/json.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [moshi](<https://devfeed.tech/tags/moshi.md>), [types](<https://devfeed.tech/tags/types.md>)

### AI overview

The article explains how Kotlin data classes, default values, nullable types, and raw strings can model JSON with less boilerplate. It also announces Moshi 1.5 support for Kotlin, including type adapters and annotations for binding JSON to idiomatic Kotlin models.

### Source excerpt

Why you should model your JSON documents with Kotlin

## Moshi, another JSON Processor

DevFeed: [Moshi, another JSON Processor](<https://devfeed.tech/articles/moshi-another-json-processor-15770.md>)

Original publisher: [Read original article](<https://developer.squareup.com/blog/moshi-another-json-processor>)

Author: Jesse Wilson

Published: 2017-03-27T19:50:19Z

Content type: opinion

Language: en

Sources: [Square Corner Blog RSS Feed](<https://devfeed.tech/sources/square-corner-blog-rss-feed.md>)

Topics: [JSON](<https://devfeed.tech/topics/json.md>), [Library](<https://devfeed.tech/topics/library.md>), [Gson](<https://devfeed.tech/topics/gson.md>), [Dagger](<https://devfeed.tech/topics/dagger.md>), [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [Mobile](<https://devfeed.tech/topics/mobile.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [dagger](<https://devfeed.tech/tags/dagger.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [development](<https://devfeed.tech/tags/development.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [gson](<https://devfeed.tech/tags/gson.md>), [json](<https://devfeed.tech/tags/json.md>), [library](<https://devfeed.tech/tags/library.md>), [moshi](<https://devfeed.tech/tags/moshi.md>)

### AI overview

The article introduces Moshi, a JSON library created to apply lessons from Gson. It describes problems with Gson's date handling and lenient parsing, and explains Moshi's goal of being small, efficient, and safe by default.

### Source excerpt

It's rare for a programmer to get to work on the same problem twice. Either the first is good enough and you're done, or it wasn't and...