0

I get the following output from a request:

{
    "allposts": [
        {
            "created": "2019-07-08T12:25:34.732217Z", 
            "description": "My First ImagePost", 
            "id": 1, 
            "imagepostdata": "http://127.0.0.1:8000/media/Images/None/placeholder.jpg", 
            "owner": "http://127.0.0.1:8000/users/getUserById/1/", 
            "profilePhotoOfUser": "http://127.0.0.1:8000/media/Images/None/placeholder.jpg", 
            "type": "ImagePost", 
            "url": "http://127.0.0.1:8000/posts/getImagePostById/1/"
        }, 
        {
            "audio": "http://127.0.0.1:8000/media/Audios/None/placeholder.3gp", 
            "clique": "http://127.0.0.1:8000/cliques/getCliqueById/1/", 
            "created": "2019-07-08T12:25:56.748829Z", 
            "id": 2, 
            "image": "http://127.0.0.1:8000/media/Images/None/placeholder.jpg", 
            "owner": "http://127.0.0.1:8000/users/getUserById/1/", 
            "profilePhotoOfUser": "http://127.0.0.1:8000/media/Images/None/placeholder.jpg", 
            "text": "My First TextPost", 
            "type": "TextPost", 
            "url": "http://127.0.0.1:8000/posts/getTextPostById/2/", 
            "video": "http://127.0.0.1:8000/media/Videos/None/placeholder.mp4"
        }
    ]
}

The first item in the JSON array represents an image post and the second item represents a text post. I have image and text posts as post type. Here, you can see that the server gives the requesting client the different types collected as one output. The fields of the items can be different. For ex.: imagepostdata vs. textpostdata.

Now, I am not sure how to define the model classes in my Android project. I use Retrofit as networking library combined with Gson.

My question: It is enough to write the ImagePost and TextPost model classes separately and let Retrofit/Gson handle the rest ? Or should I copy/paste the output to http://www.jsonschema2pojo.org/ and get only one model class for the different items.

I am asking because in the callback methods for the Retrofit request, I have to provide also the model class to which the JSON data maps. And I did not know which one to choose.

What is the normal programming approach in such a case?

abdullah celik
  • 327
  • 2
  • 12
  • I see 2 ways: don't bother too much and create a single class which will have all fields, or create 1 base class, and extend it with required fields for every different type, and then write custom deserializer for GSON, which will get the `"type": "TextPost" `, and deserialize to the class depending on the type. First method is easier and probably will work faster, while 2nd is more elegant. – Vladyslav Matviienko Jul 08 '19 at 13:55
  • how would the custom Gson deserialization would look like ? – abdullah celik Jul 08 '19 at 16:03
  • probably this way is actually better: https://stackoverflow.com/a/53088742/1568530 – Vladyslav Matviienko Jul 08 '19 at 17:11
  • I did not understand the solution at that page. – abdullah celik Jul 10 '19 at 12:45

0 Answers0