https://kotlinlang.org logo
#announcements
Title
# announcements
d

dragas

09/26/2017, 9:46 AM
Regardless, I'd suggest just talking to the person who's in charge of the API to change how it responds since it's not recommended to have numeric values as property names, if possible.
Or you could do something like this
Copy code
private class ObjectPropertyAdapter : JsonDeserializer<ResponseObject>
{
    override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): ResponseObject
    {
        val jobject = json.asJsonObject
        val flattenedArray = jobject.entrySet().flatMap { (key, value) -> context.deserialize<List<ListObject>>(value, token) }
        return ResponseObject(flattenedArray)
    }

    private val token = object : TypeToken<List<ListObject>>(){}.type
}

private data class ResponseObject(val unserializableProperty : List<ListObject>)

private data class ListObject(val value : Int)
(Assuming you're using gson)
s

sqia09

09/26/2017, 10:13 AM
I was using using Gson then I swtiched to moshi, as I thought it might support this kind of structure
d

dragas

09/26/2017, 10:15 AM
Hah, nah. You must use adapters for things like these.
s

sqia09

09/26/2017, 10:16 AM
okay i'll try out the deserializer...
can't i make an adapter using Moshi?
d

dragas

09/26/2017, 10:19 AM
I'd assume you can. All you need to do is get object's key/value set and flatmap it.
That's really it.
s

sqia09

09/26/2017, 10:20 AM
aight
i got the api guy to change the response, thanks anyways
d

dragas

09/26/2017, 4:15 PM
👌