question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the proper way to deserialize a List of things?

See original GitHub issue

I’ve a data class and a request which returns a list of these objects. What is the proper way to deserialize the list? I don’t want to write an explicit deserializer just for List<CommitStatus>

I tried to do automatic deserialization with .httpGet().responseObject<List<CommitStatus>> { ... } It returns a list of things nicely, but then it fails when accessing the list elements.

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to io.smartly.balex.receiver.CommitStatus

This is my data class:

data class CommitStatus(val state: String, val context: String, val target_url: String) {
    class Deserializer : ResponseDeserializable<CommitStatus> {
        override fun deserialize(content: String): CommitStatus? {
            return Gson().fromJson(content, CommitStatus::class.java)
        }
    }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kittinunfcommented, Sep 5, 2017

@deiga Thanks for reporting this.

Hmm I think I used Fuel to work with List of items before. Can we do something like this?

data class Issue(
            val id: Int = 0,
            val number: Int = 0,
            val title: String = "",
    ) {
        class Deserializer : ResponseDeserializable<Issue> {
            override fun deserialize(reader: Reader) = Gson().fromJson(reader, Issue::class.java)
        }

        class ListDeserializer : ResponseDeserializable<List<Issue>> {
            override fun deserialize(reader: Reader): List<Issue> {
                val type = object : TypeToken<List<Issue>>() {}.type
                return Gson().fromJson(reader, type)
            }
        }
    }

Usage

"https://api.github.com/repos/kittinunf/Fuel/issues".httpGet()
                .responseObject(Issue.ListDeserializer()) { _, _, result ->
                    update(result)
                }

Let me know if it doesn’t.

If this is useful, I think we can add this into the README.md so people can refer it as an example 😉

0reactions
SleeplessBytecommented, Nov 27, 2018

This is fixed properly with 2.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize a Collection - Json.NET
This sample deserializes JSON into a collection.
Read more >
Deserialize list of objects in C# - Stack Overflow
You need to deserialize your List object. You can try using Generics: public List<T> Deserialize<T>(string path) { return JsonConvert.
Read more >
C# - Deserialize JSON to List of Employee Objects - YouTube
In this episode, I show you how to deserialize JSON string into a list of Employee objects and display them on the console...
Read more >
How to Serialize Deserialize List of Objects in Java ... - Crunchify
Here is a complete example. These are the steps: · Create Class Item() which implements Serializable. · In Main – Create 2 Item...
Read more >
Serializing and Deserializing a List with Gson - Baeldung
One way to solve this is to add type information to the serialized JSON. We honor that type information during JSON deserialization. For...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found