Gson deserialize json with various types of values

I am trying to deserialize a JSONArray using Gson, one type of value can change, the value of "in_wanted" can be either boolean or 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 an object whenever it is available, and I need a deserializer to return null when the value "in_wanted" is boolean. What would be the best way to do this with Gson?

+6
source share
2 answers

You can do this with a custom deserializer. First, we need to create a data model that 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 that we have all the necessary classes, we need to implement a 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 { } 

Pay attention to the JsonProfile class. We must create it to avoid the “deserialization cycle” (the hard part).

And now we can test our solution using a 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); 
+10
source

You can do manual parsing, for example:

 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: more information on JsonObject , JsonArray , Gson API Documentation types . > etc.

+3
source

Source: https://habr.com/ru/post/946819/


All Articles