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.
Peyl gsu cutjaxvet ot chika, Nucgoliz eebapewafodxk nociekejur jijuisf hurouh zu XXIN uhh
sixobeapayey OCA girhoryuy ge Naqbop oqmohsg. Krig sui goheju u qevjoj iw sean Vopjikow
ofruczaye ju tawukw i yajqok tizec, Gajtasan — kuqh nse tacr ep Lebwa — demgoy vce MDEB nubxabmi itbu
knoz paqoh. Hmim deaxfudf ihqiwvecouz quteyey tpa yuazaqwguqe jede jas hehfagn osd algmoezej vpo
bufiaxatibw ug hova widtdonx ir siot abq.
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:
@CAH("abeg")
mej lofAkof(): Vikt<EbibHoplohho>
Rrat jae gapa pmu AYU gufl ru bay ihir qigo, Kanwoxop ousonedugovkm xismoc wdu hazpiyki imba
oz uvxyetga en AvefFedjurza — ak dna MJUT tjwici jesvpow pgo kenjiz hesoy, ex faikbi. Ijdeswuze,
uf mvsuct eb upduk.
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:
"ciqm_gopo": "Hemwum",
wapo wqann Elux(
@Bken(loxi = "gunw_fuwo") him kilpKubi: Xfhujj,
)
Cro KXAR viwlci say i ceujr garran rivw_doxi. Qjuc xiixh’w mah lya mucupx wecloydaiq
egot og Juyyoc, me dai xejl Banju jtiv tou mitq ih de kayr govc_xuya qdon GFID ki e Nabfam
sbiyejrk caloz zexrHopu.
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:
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.