How to Deserialize Inconsistent JSON Data With a Custom Kotlinx Serializer

preview_player
Показать описание
In this video you'll learn how you can implement a custom serializer with Kotlinx serialization to parse differing JSON structures, for example when the API has not been designed with mobile in mind.

🎓 Become an industry-ready Android developer in the Mobile Dev Campus:

⭐ Courses with real-life practices
⭐ Save countless hours of time
⭐ 100% money back guarantee for 30 days
⭐ Become a professional Android developer now:

🎁 Get my FREE 4.5h Best Practice Guide to Android Architecture:

💻 Let me be your mentor and become an industry-ready Android developer in 10 weeks:

Read our weekly Android blog:

Regular programming advice on my Instagram page:

Join my Discord server:

Get the source code for this video on GitHub:
Рекомендации по теме
Комментарии
Автор

This is good to know if you're working with an API that is no longer being maintained, but if you're working with an API within a company, it's best to reach out to the team that maintains the API and alert them their API response is inconsistent.

imb-alan-ced
Автор

Or you could use a (de)serializer specifically for field "description", i.e. @Serializable(with = val description: String?, and define it as : { ... }.
This way you won't have to deal with all the fields of a possibly large DTO class and also gain direct access to the JsonElement so that you can immediately write the "if (element is JsonObject) ..." part.

gekylafas
Автор

Nice although the easiest approach will be to also serialize nulls have a dto with all available fields on the various responses and then after you receive the dto (with some null values) map it into something appropriate for each case.

nikospsarakis
Автор

Thanks a lot for this guide. I've had this problem not so long ago.

TurboVadim
Автор

any chance of making video about using preference datastore in broadcast receivers

Ashish_singh_dev
Автор

You didn't really need a DescriptionDto, you could have really just retrieved the value from the JsonObject in this case, but I guess there would be more complex cases where it would be useful to go through such an intermediate data class?

DavidA-fijy
Автор

What do you do if the keys provided by the api are completely unknown? I'm working with IoT Philips Hue lights, and they use light IDs as keys in their json data.

scottbiggs
Автор

Any type in kotlin, can't I use that and decide to handle it after serialisation in getter ?

govindyadav
Автор

When response the has irregular values and iam not usjng that field i simply add type jf Jsonelement.

prithvirajdesigner
Автор

Cool, but can a field be either string or object depending on the response coming from the API

patrickadutwum
Автор

what was 1 practice that made your jetpack compose knowledge skyrocket?

muhammadKone-db
Автор

But this is not universal solution is it? If some books have different type of json object?

SlavaBalab
Автор

Step 1: blackmail the maintainer of the API into making it homogenous

ArthurKhazbs
Автор

You manually wrote the code that was supposed to be generated by the kotlin compiler plugin instead of only caring about the specific field that the custom deserializer should be created for ))

UsmonWasTaken
Автор

waaaay to much work, but I understand the concept. Also that class needs a few comments for context

bernardomesk
Автор

This is overkill im my POV .
Just aceept the response as string then convert it to your real object.
And u can use try catch .

tibrec
welcome to shbcf.ru