1

I'm now learning with GSON library and API. 'TMDBRating' enumeration class:

public enum TMDBRating {
    A("Adult"),//from true value
    PG("Parental Guideline");//from false value
//Blah...
    public static TMDBRating setValue(boolean adult){
        if(adult) return A;
        else return PG;
    }
}

My model class for MOVIE data

public class Movie extends Model {
    @SerializedName("popularity")
    private double popularity;
    @SerializedName("vote_count")
    private long voteCount;
    @SerializedName("video")
    private boolean video;
    @SerializedName("poster_path")
    private String posterPath;
    @SerializedName("adult")
    private TMDBRating adult;
//...
//WHOLE CONSTRUCTOR, SETTER AND GETTER...
    public TMDBRating getAdult(){
        return  adult;
    }

    public void setAdult(TMDBRating adult) {
        this.adult = adult;
    }

    public void setAdult(boolean adult) {
        this.adult = TMDBRating.setValue(adult);
    }
//...
}

My JSON MOVIE data retrieved from https://www.themoviedb.org/:

{
"popularity": 361.946,
"vote_count": 545,
"video": false,
"poster_path": "/a4BfxRK8dBgbQqbRxPs8kmLd8LG.jpg",
"id": 429203,
"adult": false,
"backdrop_path": "/6X2YjjYcs8XyZRDmJAHNDlls7L4.jpg",
"original_language": "en",
"original_title": "The Old Man & the Gun",
"genre_ids": [
35,
80,
18
],
"title": "The Old Man & the Gun",
"vote_average": 6.3,
"overview": "The true story of Forrest Tucker, from his audacious escape from San Quentin at the age of 70 to an unprecedented string of heists that confounded authorities and enchanted the public. Wrapped up in the pursuit are a detective, who becomes captivated with Forrest’s commitment to his craft, and a woman, who loves him in spite of his chosen profession.",
"release_date": "2018-09-28"
}

This is what I get when retrieving the data: java.lang.IllegalStateException: Expected STRING but was BOOLEAN at path $[0].adult. How do I convert the boolean to java enum? I've been created the setter for 'adult' to convert it into enum value from 'TMDBRating' (like above), but why still not working? Could I keep the boolean value when serialized to JSON?

aminography
  • 21,986
  • 13
  • 70
  • 74

0 Answers0