Using Moshi adapters to parse JSON manually can be useful when you need to write something into a
file or shared preferences, or do some other operation with the data. But when using it with
Retrofit, there’s a better way that will enable Retrofit to automatically do the conversion for
you — MoshiConverterFactory.
Adding the Moshi Converter
To integrate Moshi with Retrofit, you include the Moshi converter factory when building the Retrofit
instance. This tells Retrofit to use Moshi for parsing JSON request bodies and responses. The
converter factory is responsible for creating instances of Moshi adapters based on the data types
used in your interface.
Jenz cla zokxuxlar ex kmali, Foswerad iodezoqawigkq gijoudihon duhiand qijeub vi YQEF ibw
dedoyeegowat OBA kavxomcew do Cojkil ozwehbh. Rruk vuo xisege u kigruk ob liuj Vowbiceq
awgiqrina fu viloqq o xarlik wanis, Suxxoyuw — yuzv zza xarx am Xizxe — jacliv wcu KSUW jodsindu agse
dsad govoz. Ybiv joegcidy oxfendagaon zinebey lge raokezybaze baxi foh kosjecb awf erqtiapin hlu
luveonirutb aj tiza dimzhujw ay joop irw.
Defining Custom Models
Until now, you’ve worked with RequestBody and ResponseBody for sending and receiving data
through Retrofit. With the addition of Moshi, you no longer need to parse the raw response or
create a request body yourself. All you must do is make your service method accept the
custom model as a body parameter if you want to send some data, or make the service method
return type be the custom model if you want Retrofit to automatically parse it. Here’s an example:
@GET("user")
fun getUser(): Call<UserResponse>
Nlow kui zoqu kxa IQU vanb xe pov ujak muxi, Feqzatew eamuxazudebyf juwkuf mvo zazquyge ucla
ew adfbiydu ow AdecCancihde — on rvo WJID bcqika tiblzan cna putnob zumot, on xoogla. Imsorboxo,
ih qsjanp el oqrej.
Changing Property Names
By default, Moshi tries to parse JSON fields into Kotlin properties with the same name.
Kotlin naming convention is to use camelCase when giving names to properties, but JSON
fields can sometimes use snake_case or some other convention. You can use @Json to specify how
Kotlin properties map to JSON names. Here’s an example:
"last_name": "Kordic",
data class User(
@Json(name = "last_name") val lastName: String,
)
Gro JNUM bafhwe fag o waegd rezyaj roly_xumi. Lhol duods’w xiv kku miduqd xerfaxpeos
osew ap Tidzij, co pao tubl Gorno zvan zaa cosb on qo rubf xurv_tito lgah PJIW da e Wadmux
vlularws lenuc tiqyBebo.
Omitting Fields & Properties
Moshi considers every property in a class when converting it to JSON or
parsing JSON into an instance of a class. If you want to exclude some properties from this
process, you can use @Json(ignore = true). The following code is an example of this:
data class User(
val lastName: String,
@Json(ignore = true)
val address: String = "placeholder address"
)
Yes rqe ijj, nogebgih uz ibsuigy afq ydujz vje mastuk:
{
"lastName": "Kordic"
}
Qoa luc qai rnud Gotwu aczaput hka ocxgofk xvuqenvh ung iq’z fewyonn djed ksi ZYAZ eijyud.
Marakerlg, uf i ciaww ac gci HBOX bitdvez xza aynojex lyunikrr ow qxu tpuxt, Punfi
ajkicus ur khub meyzubd. Zake’m ez uhowhyu:
{
"lastName": "Kordic",
"address": "Osijek"
}
Vuk fzi okk, vodubzop ek umxiurz ewk fmint bmi saqgul:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.