https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
c

ChChenna Rao

03/02/2023, 10:04 AM
Hi anyone help me I’m not able to serialise my api response to model Example API ; https://food-itema-default-rtdb.firebaseio.com/telugu-skillhub-api/-MsE8GfWtRjc8x_t8pCC.json
Copy code
class APIClient {

    var converter: KotlinxSerializationConverter? = null

    private val httpClient = HttpClient() {
        
        install(ContentNegotiation) {
            converter = KotlinxSerializationConverter(Json {
                prettyPrint = true
                ignoreUnknownKeys = true
                explicitNulls = false
            })

            register(ContentType.Application.Json, converter!!)
        }
    }


    suspend fun getSampleApi(): RootResponse<List<Data>> {

        val response: HttpResponse = httpClient.get {
            url {
                protocol = URLProtocol.HTTPS
                host = "<http://food-itema-default-rtdb.firebaseio.com|food-itema-default-rtdb.firebaseio.com>"
                path("telugu-skillhub-api/-MsE8GfWtRjc8x_t8pCC.json")
            }
        }

        return response.body();
    }
}
1
m

mbonnin

03/02/2023, 10:06 AM
Can you copy paste the error you're getting?
c

ChChenna Rao

03/02/2023, 10:07 AM
HTTP Client - RESPONSE https://food-itema-default-rtdb.firebaseio.com/telugu-skillhub-api/-MsE8GfWtRjc8x_t8pCC.json failed with exception: io.ktor.serialization.JsonConvertException: Illegal input
@mbonnin did you find the issue ?
m

mbonnin

03/02/2023, 10:43 AM
That json looks ok 🤔 . Maybe try to isolate the problem without ktor?
Copy code
val jsonString = """your json here"""
Json.parseToJsonElement(jsonString)
c

ChChenna Rao

03/02/2023, 10:46 AM
If I use response.bodyAsText() the data is coming in string. But I’ trying to serilize that string into Model class.
v

Vidmantas Kerbelis

03/02/2023, 10:49 AM
Did you add
@Serializable
and
@SerialName
fields correctly to the Model class?
c

ChChenna Rao

03/02/2023, 10:49 AM
Yes
m

mbonnin

03/02/2023, 10:50 AM
Is there a
cause
in your JsonConvertException (it should)
If you copy/paste the full stacktrace
c

ChChenna Rao

03/02/2023, 10:51 AM
No
m

mbonnin

03/02/2023, 10:52 AM
Can you copy/paste the full stacktrace?
Illegal Input
happens here, there has to be more information in that exception
c

ChChenna Rao

03/02/2023, 10:55 AM
Please check this below files so you can understand
v

Vidmantas Kerbelis

03/02/2023, 10:56 AM
I mean this runs just fine:
Copy code
@Test
    fun test() {
        val jsonString =
            """{"items":[{"category":"Pizza","id":"19-Q9JSH3ZvC40lOlUttxf4wtPVm9ctIU","name":"American Cheesy Pizza","prize":300,"url":"<http://docs.google.com/uc?export=open&id=19-Q9JSH3ZvC40lOlUttxf4wtPVm9ctIU>"},{"category":"Pizza","id":"1QB2q13sop1x86CbOf5c5NBY0geWhk2hZ","name":"Bacon Pizza","prize":450,"url":"<http://docs.google.com/uc?export=open&id=1QB2q13sop1x86CbOf5c5NBY0geWhk2hZ>"},{"category":"Pizza","id":"1ARbwqW_uOLb4eCeWsyCJVZSYUIreXsrD","name":"Double Cheese Pizza","prize":500,"url":"<http://docs.google.com/uc?export=open&id=1ARbwqW_uOLb4eCeWsyCJVZSYUIreXsrD>"},{"category":"Pizza","id":"1a5kM2bkvvvG9x12V_KkuZ1HRor2xgU20","name":"Italian Cheesy Pizza","prize":450,"url":"<http://docs.google.com/uc?export=open&id=1a5kM2bkvvvG9x12V_KkuZ1HRor2xgU20>"},{"category":"Pizza","id":"12fiRkYTdFZWr3qaczRBUu-c6ciQ1FY2V","name":"Keema Pizza","prize":350,"url":"<http://docs.google.com/uc?export=open&id=12fiRkYTdFZWr3qaczRBUu-c6ciQ1FY2V>"},{"category":"Pizza","id":"1RtJQB2pxhHWFGSH2vdOJxgSI4b1vEdTm","name":"Non Veg Pizza","prize":500,"url":"<http://docs.google.com/uc?export=open&id=1RtJQB2pxhHWFGSH2vdOJxgSI4b1vEdTm>"},{"category":"Pizza","id":"1gWKqlaXkO7h-w3--WygpgUPy28a21QHp","name":"Olives Pizza","prize":200,"url":"<http://docs.google.com/uc?export=open&id=1gWKqlaXkO7h-w3--WygpgUPy28a21QHp>"},{"category":"Pizza","id":"1hUGxEC95jEwnlXVh08isFuoxivT5-HwB","name":"Onion Pizza","prize":250,"url":"<http://docs.google.com/uc?export=open&id=1hUGxEC95jEwnlXVh08isFuoxivT5-HwB>"},{"category":"Pizza","id":"1u8NjRiICc3J4U9WBgarM7gFvkyTEOtz-","name":"Oven Pizza","prize":300,"url":"<http://docs.google.com/uc?export=open&id=1u8NjRiICc3J4U9WBgarM7gFvkyTEOtz->"},{"category":"Pizza","id":"1RQ1mCBZPYA486Oox1wjIAKDoW-JlBNdp","name":"Tomato Pizza","prize":350,"url":"<http://docs.google.com/uc?export=open&id=1RQ1mCBZPYA486Oox1wjIAKDoW-JlBNdp>"},{"category":"Pizza","id":"1w-VuYFP68NaI3QTUic3teSxSA1e90QYT","name":"Veg Pizza","prize":240,"url":"<http://docs.google.com/uc?export=open&id=1w-VuYFP68NaI3QTUic3teSxSA1e90QYT>"},{"category":"Pizza","id":"1q6JXHunKjE6ew0WSPRZ0wnCGqxkpoTjX","name":"Italian Pizza","prize":380,"url":"<http://docs.google.com/uc?export=open&id=1q6JXHunKjE6ew0WSPRZ0wnCGqxkpoTjX>"},{"category":"Rice Items","id":"1hshLhPmvU8MGU0dJL3QAD9sfwLfrZudE","name":"Camel Meat Biriyani","prize":250,"url":"<http://docs.google.com/uc?export=open&id=1hshLhPmvU8MGU0dJL3QAD9sfwLfrZudE>"},{"category":"Rice Items","id":"1DcmVft5AozS0TCCKyige1j4RVTiycfRB","name":"Chicken Biriyani","prize":300,"url":"<http://docs.google.com/uc?export=open&id=1DcmVft5AozS0TCCKyige1j4RVTiycfRB>"},{"category":"Rice Items","id":"1JKKnUJ50gFHESkLl_1uvYzNq0U7lliVc","name":"Veg Biriyani","prize":220,"url":"<http://docs.google.com/uc?export=open&id=1JKKnUJ50gFHESkLl_1uvYzNq0U7lliVc>"},{"category":"Rice Items","id":"1whqo8bnsDFZlQgeE8dLckVH-q1CvDQmJ","name":"Dum Biriyani","prize":320,"url":"<http://docs.google.com/uc?export=open&id=1whqo8bnsDFZlQgeE8dLckVH-q1CvDQmJ>"},{"category":"Rice Items","id":"1TMqorI3sEtWu8tGnR92fbj2HMbqFghqG","name":"Egg Biriyani","prize":240,"url":"<http://docs.google.com/uc?export=open&id=1TMqorI3sEtWu8tGnR92fbj2HMbqFghqG>"},{"category":"Rice Items","id":"1mc2F0bagZxRuTFGAzZ934Vs4IUT3tf8-","name":"Fried Rice","prize":260,"url":"<http://docs.google.com/uc?export=open&id=1mc2F0bagZxRuTFGAzZ934Vs4IUT3tf8->"},{"category":"Rice Items","id":"1PkbIazPAZNcYBicZoIg808Mz6Q4Ff1zQ","name":"Full Meals","prize":180,"url":"<http://docs.google.com/uc?export=open&id=1PkbIazPAZNcYBicZoIg808Mz6Q4Ff1zQ>"},{"category":"Rice Items","id":"1SGTRIPCFs2S5DDa5EX6I8ck8J5FkOZJa","name":"Grilled Chicken Biriyani","prize":350,"url":"<http://docs.google.com/uc?export=open&id=1SGTRIPCFs2S5DDa5EX6I8ck8J5FkOZJa>"},{"category":"Rice Items","id":"1y_V1D-n5ifzBodqUwL3cMn6wnBHW0HfF","name":"Cuisine Biriyani Dish","prize":320,"url":"<http://docs.google.com/uc?export=open&id=1y_V1D-n5ifzBodqUwL3cMn6wnBHW0HfF>"},{"category":"Cold Drinks","id":"1zTPFO3DVl96hq-AtLAvUAI8GV4cMmOR3","name":"Cold Coffee","prize":50,"url":"<http://docs.google.com/uc?export=open&id=1zTPFO3DVl96hq-AtLAvUAI8GV4cMmOR3>"},{"category":"Cold Drinks","id":"1iInoRGdGPefRrsZQMbeHF0KkuPmU01Yu","name":"Strawberry Milk Shake","prize":80,"url":"<http://docs.google.com/uc?export=open&id=1iInoRGdGPefRrsZQMbeHF0KkuPmU01Yu>"},{"category":"Cold Drinks","id":"1BlPB70Cma4KtUDfAqQq90AlElD9-29MR","name":"Cherry Milk Shake","prize":90,"url":"<http://docs.google.com/uc?export=open&id=1BlPB70Cma4KtUDfAqQq90AlElD9-29MR>"},{"category":"Cold Drinks","id":"13KuZtpbCinD8R41biG6psQJSSQ5u9Ej2","name":"Cold Hazelnut","prize":110,"url":"<http://docs.google.com/uc?export=open&id=13KuZtpbCinD8R41biG6psQJSSQ5u9Ej2>"},{"category":"Cold Drinks","id":"12nNcmfmX8C9Mca_CWoMsBmNPbA6Msqnf","name":"Cold Chocolate","prize":100,"url":"<http://docs.google.com/uc?export=open&id=12nNcmfmX8C9Mca_CWoMsBmNPbA6Msqnf>"},{"category":"Cold Drinks","id":"1-Xl7Ilzrw3egBF2cl4rbqo6YhPoOngy4","name":"Raspberry Milk Shake","prize":90,"url":"<http://docs.google.com/uc?export=open&id=1-Xl7Ilzrw3egBF2cl4rbqo6YhPoOngy4>"},{"category":"Cold Drinks","id":"1N3PflFuRAf5Wt5wXvcmK0Mq4DEfwOABc","name":"Fruit Milk Shake","prize":80,"url":"<http://docs.google.com/uc?export=open&id=1N3PflFuRAf5Wt5wXvcmK0Mq4DEfwOABc>"},{"category":"Cold Drinks","id":"115Mtm-zHd3mE6g33VBO9GztfBLiv2geO","name":"Milk Shake Combo","prize":150,"url":"<http://docs.google.com/uc?export=open&id=115Mtm-zHd3mE6g33VBO9GztfBLiv2geO>"},{"category":"Cold Drinks","id":"1RfcdvzvCIbaOglZPPp4oZanMC6Jdgk-x","name":"Strawberry Cappuccino","prize":100,"url":"<http://docs.google.com/uc?export=open&id=1RfcdvzvCIbaOglZPPp4oZanMC6Jdgk-x>"},{"category":"Hot Drinks","id":"11OcppqrIJ2bn2P_akQdl2-cwCNxAo1eR","name":"Black Coffee","prize":30,"url":"<http://docs.google.com/uc?export=open&id=11OcppqrIJ2bn2P_akQdl2-cwCNxAo1eR>"},{"category":"Hot Drinks","id":"1Fnk8IeV9ecicqvIhsRBYiK3zY3glCl8b","name":"Hot Tea","prize":40,"url":"<http://docs.google.com/uc?export=open&id=1Fnk8IeV9ecicqvIhsRBYiK3zY3glCl8b>"},{"category":"Hot Drinks","id":"1Nv9RfgoQrSwEgAdFwIBSxj9wSEh4oCtT","name":"Hot Coffee","prize":40,"url":"<http://docs.google.com/uc?export=open&id=1Nv9RfgoQrSwEgAdFwIBSxj9wSEh4oCtT>"},{"category":"Hot Drinks","id":"1ipTABJXebiFAgfDYDZjYBSAGXygSF-pO","name":"Hot Cappuccino","prize":80,"url":"<http://docs.google.com/uc?export=open&id=1ipTABJXebiFAgfDYDZjYBSAGXygSF-pO>"},{"category":"Hot Drinks","id":"1Oo5yON_Rhv3c6Tc4WgycB3IYZgRvVdTJ","name":"Hot Chocolate","prize":90,"url":"<http://docs.google.com/uc?export=open&id=1Oo5yON_Rhv3c6Tc4WgycB3IYZgRvVdTJ>"},{"category":"Hot Drinks","id":"1eYIOFAdSpIvTwXULXR7j-Ivw7uZ6U_dI","name":"Hot Hazelnut","prize":100,"url":"<http://docs.google.com/uc?export=open&id=1eYIOFAdSpIvTwXULXR7j-Ivw7uZ6U_dI>"}]}"""
        val something = Json {
            prettyPrint = true
            ignoreUnknownKeys = true
            explicitNulls = false
        }.decodeFromString(serializer<Something>(), jsonString)

        println("Something: ${something.items.joinToString("\n") { it.toString() }}")
    }

    @kotlinx.serialization.Serializable
    data class Something(
        @SerialName("items") val items: List<Item>
    ) {
        @kotlinx.serialization.Serializable
        data class Item(
            @SerialName("id") val id: String,
            @SerialName("category") val category: String,
            @SerialName("name") val name: String,
            @SerialName("prize") val prize: Long,
            @SerialName("url") val url: String,
        )
    }
c

ChChenna Rao

03/02/2023, 10:56 AM
let me check @Vidmantas Kerbelis
v

Vidmantas Kerbelis

03/02/2023, 10:57 AM
I took your input, took your Json {} configuration and created the data classes appropriately myself to what I observed in the JSON string
c

ChChenna Rao

03/02/2023, 11:02 AM
I follow the above steps it is working fine @Vidmantas Kerbelis. Thanks for the help @mbonnin
21 Views