Roger
Roger

Reputation: 573

Why does moshi codegen require @JsonClass to be only on concrete classes?

It seems perfectly possible for an annotation processor to generate json adapters for interfaces or abstract classes, at least in kotlin. Why doesn't moshi do this? There must be some use case that I haven't thought of...

[edit - adding examples below]

We're deserializing json recipe data from REST endpoints that, annoyingly, each return slightly different representations of a recipe - one endpoint will return recipes with authors, another with ingredients, etc. So we have it all represented in the client as something like:

interface Recipe {
  val id: Int
  val name: String
}

interface HasIngredients {
  val ingredients: List<String>
}

interface HasAuthor {
  val author: String
}

interface DetailedRecipe: Recipe, HasAuthor, HasIngredients
interface SearchResultRecipe: Recipe, HasAuthor
etc.

(this is a simplified example but you get the idea)

I'd rather not create concrete classes for all of these variations because that would result in duplicate field definitions.

Upvotes: 0

Views: 32

Answers (0)

Related Questions