6

I'm trying to deserialize a JSONArray with Gson, one the values' type can vary, the value "in_wanted" can be either a boolean or a JSONObject.

in_wanted as boolean:

{
"movies": [
        {
            "title": "example boolean",
            "in_wanted": false
        }
    ]           
}

in_wanted as JSONObject:

{
"movies": [
        {
            "title": "example object",
            "in_wanted": {
                "profile": {
                    "value": false
                }
            }
        }
    ]           
}

I need the object whenever it's available and i need a deserializer to return null whenever the value of "in_wanted" is a boolean. What would be the best way to do this with Gson?

Bart
  • 769
  • 2
  • 13
  • 29
  • If you're mapping to a class then try to naively let the Gson deserializer work, it should let the reference as null. – Pragmateek Jun 07 '13 at 21:05

2 Answers2

11

You can do this with custom deserializer. At the start we should create data model which can represent your JSON.

class JsonEntity {

    private List<Movie> movies;

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }

    @Override
    public String toString() {
        return "JsonEntity [movies=" + movies + "]";
    }
}

class Movie {

    private String title;
    private Profile in_wanted;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Profile getIn_wanted() {
        return in_wanted;
    }

    public void setIn_wanted(Profile in_wanted) {
        this.in_wanted = in_wanted;
    }

    @Override
    public String toString() {
        return "Movie [title=" + title + ", in_wanted=" + in_wanted + "]";
    }
}

class Profile {

    private boolean value;

    public boolean isValue() {
        return value;
    }

    public void setValue(boolean value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

Now when we have all needed classes we should implement new custom deserializer:

class ProfileJsonDeserializer implements JsonDeserializer<Profile> {
    @Override
    public Profile deserialize(JsonElement jsonElement, Type type,
            JsonDeserializationContext context) throws JsonParseException {
        if (jsonElement.isJsonPrimitive()) {
            return null;
        }

        return context.deserialize(jsonElement, JsonProfile.class);
    }
}

class JsonProfile extends Profile {

}

Please have a look on JsonProfile class. We have to create it to avoid "deserialization loop" (tricky part).

And now we can test our solution with test method:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Profile.class, new ProfileJsonDeserializer());
Gson gson = builder.create();

JsonEntity jsonEntity = gson.fromJson(new FileReader("/tmp/json.txt"),
        JsonEntity.class);
System.out.println(jsonEntity);
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • This did the trick, since manual parsing would have been a large task as you suspected @MikO. Thank you both very much for the help! – Bart Jun 07 '13 at 21:52
4

You could do a manual parsing, something like:

JsonParser parser = new JsonParser();
JsonObject rootObject = parser.parse(yourJsonString).getAsJsonObject();
JsonObject movieObject = rootObject
                           .getAsJsonArray("movies")
                           .get(0).getAsJsonObject();
JsonElement inWantedElement = movieObject.get("in_wanted");

//Check if "in_wanted" element is a boolean or an object
if (inWantedElement.isJsonObject()) {
    //Process the element as an object...
    //for example get the element "value"...
    boolean value = inWantedElement
                      .getAsJsonObject()
                      .getAsJsonObject("profile")
                      .getAsJsonPrimitive("value")
                      .getAsBoolean();
}
else if (inWantedElement.isJsonPrimitive()) {
    //Process the element as a boolean...
    boolean inWanted = inWantedElement.getAsBoolean();
}

Note: see Gson API documentation for further info about types JsonObject, JsonArray, JsonElement, and so on...

MikO
  • 18,243
  • 12
  • 77
  • 109
  • Is it possible to do this for only the in_wanted value? So i would not have to write manual parsing for the other values in the array (which the default deserializer parses fine). – Bart Jun 07 '13 at 21:27
  • 1
    @Meatje: I think there's a possibility, writing a custom deserializer, but before writing that, you have to note that the other solution won't be much simpler that this, and if you have those JSON responses I think this is better... or is it that you only included a small portion of your actual JSON responses? – MikO Jun 07 '13 at 21:36