question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[REQ][Kotlin] Use Moshi codegen instead of reflection for generating JSON adapters

See original GitHub issue

Is your feature request related to a problem? Please describe.

Moshi can generate JSON adapters two different ways. The old way used reflection. There is a newer way that avoids reflection and uses code generation instead. Typically, code generation performs better than reflection.

OpenAPI uses the reflection approach when generating Kotlin Moshi code.

Describe the solution you’d like

Ideally, OpenAPI Generator would use the code generation approach over reflection.

Remove com.squareup.moshi:moshi-kotlin dependency. Remove Moshi.Builder().add(KotlinJsonAdapterFactory()) Add com.squareup.moshi:moshi-kotlin-codegen dependency. Annotate each generated model with @JsonClass(generateAdapter = true)

Describe alternatives you’ve considered

The alternative is leaving it as is, but that may affect performance.

Additional context

See: https://github.com/square/moshi#kotlin

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alexfacciorussocommented, Jan 19, 2021

Hi all!

I think the moshiCodeGen : true option is not listed anywhere, and it should be added to the doc!

1reaction
stephen-mojocommented, Oct 21, 2020

And closing again. Sorry about the back and forth! Once again, I spoke before thinking hard enough.

My issue was that I had the following dependency… implementation "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

…when I should have had this: kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

Thanks again for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's the use of Moshi's Kotlin codegen? - Stack Overflow
That adapter makes use of reflection to find all the fields and create the JSON string (you can see that class imports stuff...
Read more >
Moshi JSON Library for Kotlin Applications - Baeldung
The standard way of Moshi is to use reflection with user types to generate adapters. With Kotlin, it means an extra dependency and...
Read more >
Exploring Moshi's Kotlin Code Gen - Zac Sweers
Moshi 1.6 was recently released with a brand new Kotlin code gen artifact. It's an alternative to the existing reflection-based artifact, ...
Read more >
Getting started using Moshi for JSON parsing with Kotlin
This article demonstrates how this is achieved from setting up Moshi, using it to parse JSON into a simple model object and implementing...
Read more >
Advanced JSON parsing techniques using Moshi and Kotlin
Moshi has two ways to generate these converters which are called JsonAdapter s: Using reflection, via the moshi-kotlin artifact. Adapters will be generated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found