https://kotlinlang.org logo
Title
m

Mohammed Alaa

11/20/2019, 7:01 PM
Hi guys, I was wondering if there exist something for JSON parsing that work with Retrofit in android like Codable Protocol in ios Swift that allow dynamic json parsing ,or I should create my own JSON converter for every type?
c

Casey Brooks

11/20/2019, 7:06 PM
There’s a Retrofit converter adapter for kotlinx.serialization https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter
m

Mohammed Alaa

11/20/2019, 7:11 PM
Thanks, will try it
a

Anastasia Finogenova

11/20/2019, 10:16 PM
Gson and it has a converter factory for retrofit
c

Cody Engel

11/21/2019, 1:31 AM
Moshi has been nice, you forget there’s even JSON serialization happening
v

varun

11/21/2019, 4:50 AM
I also prefer Moshi over Gson because Moshi gives an exception when you try to parse null/absent values into fields that are non-nullable. Gson silently sets those non-nullable fields to null via reflection and then you get an exception where you won't expect it.
m

Mohammed Alaa

11/21/2019, 7:38 AM
Yes I had tried Converter factory but this will reqire me to know all the keys which valus are changing , lets say for example I have a key which return sometimes JsonArray and other times return JsonObject in the same API Call ,so I need to know this key
v

varun

11/21/2019, 9:25 AM
@Mohammed Alaa Can you post samples of the type of responses you are expecting?
m

Mohammed Alaa

11/21/2019, 9:42 AM
c

Cody Engel

11/21/2019, 1:23 PM
Oh gotcha. So as other countries are added you’ll get a new key and value but the root object would stay the same?
m

Mohammed Alaa

11/21/2019, 2:33 PM
@Cody Engel yes
a

Anastasia Finogenova

11/21/2019, 2:35 PM
I had the same issue that the same key could be either an array or an object and successfully fixed it with Gson
Also if you are using kotlin and null is set to non-nullable value the app will crash
Pretty common case it seems 🤷
m

Mohammed Alaa

11/21/2019, 2:37 PM
@Anastasia Finogenova Thanks, this is very interesting
@Anastasia Finogenova👍 Solution with Generic are good for me so I can't know how many keys will need this
💯 1
c

Cody Engel

11/21/2019, 2:50 PM
This shouldn’t be a common use case but yeah it happens. Moshi lets you write custom converters as well.
💯 1
a

Anastasia Finogenova

11/21/2019, 2:52 PM
Common in a way it doesn't surprise me at all😅
😂 3
m

Mohammed Alaa

11/21/2019, 2:52 PM
@Cody Engel yes it isn't a common use case but it happened with me 😃
a

Anastasia Finogenova

11/21/2019, 2:54 PM
Happened to me too and appearantly if the answer on stackoverflow has 74 upvotes at least to other 74 people 😆
m

Mohammed Alaa

11/21/2019, 2:56 PM
Yes, You count only who up-vote so whats about others who doesn't vote and happened with them 😉
a

Anastasia Finogenova

11/21/2019, 3:01 PM
Exactly, some bad backend designers out there 🤷
c

Cody Engel

11/21/2019, 3:07 PM
For sure, don’t want to say it doesn’t happen regularly with various APIs, just saying that it shouldn’t because an array and map aren’t the same type 😂
But with Moshi you can create custom adapters with the
@FromJson
annotation probably similar to other JSON serialization libraries; because while it shouldn’t be normal it unfortunately is normal enough lol