I guess I'll try my luck here, as it doesn't fit i...
# announcements
b
I guess I'll try my luck here, as it doesn't fit in a channel and is imo not exactly a beginner question. I am trying to develop a client for the deezer api. I want to do it, using both retrofit and moshi. As you can see here (https://developers.deezer.com/api/explorer?url=album/302127), Deezer has the issue, that it wraps every array in a object called "data". I try to unwrap that unnecessary data-object to get a nice and clean api. This is the code I have so far: (https://pastebin.com/wTRRZn8E). Unfortunately it does crash with the following exception: https://pastebin.com/6hjVGECP I hope you can help me out 🙂
e
Assuming you're trying to use generics with the Moshi KotlinJsonAdapterFactory, it's a known limitation. https://github.com/square/moshi/issues/309
Maybe consider installing a Java JsonAdapter.Factory like the one here https://github.com/square/moshi/blob/master/examples/src/main/java/com/squareup/moshi/recipes/Unwrap.java and apply its Enveloped qualifier to types you want unwrapped?
b
Thank you so much for your tips 🙂 I'll try 🙂
c
@beatbrot For fun, I parsed it with Klaxon and the following works just fine:
Copy code
data class Track(val id: Long, val title: String)
    data class TrackData(val data: List<Track>)
    data class Album(val id: Int, val title: String, val link: String, val cover_big: String, val duration: Int,
            val rating: Int, val tracks: TrackData)

    fun t() {
        val json =
            URL("<http://api.deezer.com/album/53227232>").openConnection().inputStream.reader().readText()
        Klaxon().parse<Album>(json)?.let {
            println("Album: ${it.title} ${it.tracks.data.size} tracks")
        }
    }
Note that I adapted the data classes to match the shape of the JSON more closely. No need for an adapter.
e
Well, if you're doing that without the Kotlin box generic type, Moshi will work fine, too, and no need for an adapter.
c
@eric Not sure what you mean, there is a generic type up there (see the
parse
invocation)
Kotlin's reflection has a few added features over Java's which allows for some cool things in a Kotlin-based JSON parser
e
my point is that he was asking about custom generic types, like asking Moshi for an adapter for
Box<T>
. which doesn't work in the KotlinJsonAdapter in Moshi right now. without custom generic types, there's no need for anything special in Moshi. from your code:
Copy code
val adapter = Moshi.Builder().build().adapter<Album>(Album::class.java)
    URL("<http://api.deezer.com/album/53227232>").openConnection().inputStream.use {
      val album = adapter.fromJson(JsonReader.of(Okio.buffer(Okio.source(it))))
      println(album)
    }
works fine.
c
Yup. Note that you need the extra class parameter
Album::class.java
.
I don't see any
Box
in https://pastebin.com/wTRRZn8E
e
DataArray is the box.