Hi can anyone help we with kotlin deserialization?...
# serialization
y
Hi can anyone help we with kotlin deserialization? I’m using retrofit with kotlinx serialization and I have a case where I can get 2 types of responses from the server: the first case returns a list of results with “drinks” key,
{"drinks":[{"strDrink":"Jelly Bean","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/bglc6y1504388797.jpg","idDrink":"13775"},{"strDrink":"Turf Cocktail","strDrinkThumb":"https:\/\/<http://www.thecocktaildb.com|www.thecocktaildb.com>\/images\/media\/drink\/utypqq1441554367.jpg","idDrink":"12418"}]}
but sometimes the server returns the following:
Copy code
"{"drinks":"None Found"}"
Before using kotlinx I had a custom type convertor with Gson, and that worked fine, but I’m struggling with kotlin. I tried to create a KSerializer to try to parse the result, and return an object with an empty list if an exception is thrown, but I keep getting this exception:
java.lang.IllegalStateException: Reader has not consumed the whole input: JsonReader(source='{"drinks":"None Found"}', currentPosition=22, tokenClass=1, tokenPosition=10, offset=11)
Any ideas how to solve this? I can always return a string instead of a data class, but this is ugly AF
y
Thanks! sorry for the copy/paste, I’ll check it out
@ephemient Looks good! thanks!