[REQ][Kotlin] Use Moshi codegen instead of reflection for generating JSON adapters
See original GitHub issueIs 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi all!
I think the
moshiCodeGen : true
option is not listed anywhere, and it should be added to the doc!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!