0

I have been wondering if there is a possibility to automatically deserialize json to specific object properties, and serialize it with different names without implementing any custom deserializer. I am working on integrating my app with Github API and their naming convention is something I would like to remodel. Once requesting resources I get something like:

{
 full_name: "...",
 description: null,
 clone_url: "...",
 stargazers_count: 0,
 created_at: "2018-06-05"
}

but I would like to expose them on my side as:

{
  fullName: "...",
  description: null,
  cloneUrl: "...",
  stars: 0,
  createdAt: "2018-06-05"
}

Here is how my model looks like right now:

internal data class GithubRepositoryResponse(@JsonProperty("full_name") val fullName: String,
                                             val description: String?,
                                             @JsonProperty("clone_url") val cloneUrl: URL,
                                             @JsonProperty("stargazers_count") val stars: Int,
                                             @JsonProperty("created_at") val createdAt: LocalDate)

Using @JsonProperty with value is bidirectional so I get the same property names as declared in annotation which is something I would really like to avoid. Is there any convenient way of dealing with such a scenerio?

sh1nen
  • 199
  • 4
  • 18
  • Try this one: https://stackoverflow.com/a/40730352/1532460 – awesoon Jun 23 '18 at 14:19
  • @soon that way I won't be able to keep stargazers_count as stars, I got specification which I have to fulfill so it is not an option. – sh1nen Jun 23 '18 at 14:23

1 Answers1

1

Annotating data class properties with @JsonAlias did the trick.

sh1nen
  • 199
  • 4
  • 18