I have a data class called `data class Content( va...
# android
a
I have a data class called
data class Content( val aName: String, val bName: String)
This is the type how a library is sending the data to the app. I need to send this to our backend which requires the keys to be like “a_name” and “b_name” so I tagged the Json(name=“”) annotations on top of them like this,
data class Content( @Json(name="a_name") val aName: String,  @Json(name="b_name") val bName: String)
But now the library is shouting with the below error: com.squareup.moshi.JsonDataException: Required value ‘aName’ (JSON name ‘a_name’) missing at $[1] My Solution is to create
LibraryContent
data class and
BackendContent
data class with the respective names. Is there a better solution? Thanks in advance!
c
Unclear on the issue. Is it that the same data class ‘Content’ is used for both incoming data, and for delivering data to the back-end (in a slightly different format)?
a
yes
the name is different.
we cant ask our backend to change the contracts to accept the ones that the library is offering because of naming conventions.
c
the general pattern for this is as you indicated - separate the objects to match their respective contracts, and then map accordingly.
there’s possibly other shortcuts that are frought with fingerguns, as contracts have a habit of evolving independently.
c
I would personally not reuse a model across datasources like this. Declaring separate data classes that respect the contract of that datasource is a more scalable solution IMO.
ContentLibraryModel
ContentApiModel
✔️ 1
a
Thanks for your inputs folks.