List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:jmm.api.tmdb.TMDBMovieWrapper.java
License:Open Source License
private void addActorsAndDirector(JsonElement rootElement, TMDBVideoFile videoFile) { //handle response if (!rootElement.isJsonNull()) { JsonElement value;/*from www .j av a 2 s . c o m*/ //Publisher value = rootElement.getAsJsonObject().get("casts"); if (value != null && !value.isJsonNull()) { value = value.getAsJsonObject().get("cast"); if (value.isJsonArray()) { JsonArray cast = value.getAsJsonArray(); Iterator<JsonElement> castIterator = cast.iterator(); while (castIterator.hasNext()) { JsonElement actorElement = castIterator.next(); value = actorElement.getAsJsonObject().get("name"); if (!value.isJsonNull()) { Actor actor = new Actor(value.getAsString()); value = actorElement.getAsJsonObject().get("id"); if (!value.isJsonNull()) { actor.setTmdbID(value.getAsInt()); } value = actorElement.getAsJsonObject().get("profile_path"); if (!value.isJsonNull()) { actor.setProfilePicturePath( API_BASE_IMAGEURL + Profile_Sizes.w185 + value.getAsString()); } videoFile.addActor(actor); } } } value = rootElement.getAsJsonObject().get("casts"); value = value.getAsJsonObject().get("crew"); if (value.isJsonArray()) { JsonArray crew = value.getAsJsonArray(); //Look for director Iterator<JsonElement> crewIterator = crew.iterator(); while (crewIterator.hasNext()) { JsonElement crewElement = crewIterator.next(); value = crewElement.getAsJsonObject().get("job"); if (!value.isJsonNull() && (value.getAsString().equalsIgnoreCase("Director"))) { value = crewElement.getAsJsonObject().get("name"); if (!value.isJsonNull()) { videoFile.setDirector(value.getAsString()); } } } } } } }
From source file:jmm.api.tmdb.TMDBMovieWrapper.java
License:Open Source License
/** * Get the list of upcoming movies. This list refreshes every day. The maximum number of items this list will include is 100. <br/> * <b>Bold</b> parameters are <b>required</b>. * //from w w w .j a v a2 s .c o m * @param page * @param language ISO 639-1 code. * @return A list of upcoming movies * @param maxResults the maximum number of results */ public List<TMDBVideoFile> findUpcomingMovies(String page, String language, int maxResults) { paramsMap.clear(); //optional parameters if (page != null) { paramsMap.put(PARAM_PAGE, page); } if (language != null) { paramsMap.put(PARAM_LANGUAGE, language); } String url = buildUrl(API_MOVIE + "/" + "upcoming"); JsonElement response = callAPI(url); //handle response List<TMDBVideoFile> searchResults = new LinkedList<TMDBVideoFile>(); if (!response.isJsonNull()) { JsonElement key = response.getAsJsonObject().get("results"); if (key.isJsonArray()) { JsonArray elements = key.getAsJsonArray(); int arraySize = elements.size(); if (arraySize > maxResults) { arraySize = maxResults; } for (int i = 0; i < arraySize; i++) { JsonElement element = elements.get(i); String title = element.getAsJsonObject().get("title").getAsString(); TMDBVideoFile upcomingMovie = new TMDBVideoFile(title); try { JsonElement value; value = element.getAsJsonObject().get("backdrop_path"); if (!value.isJsonNull()) { upcomingMovie .setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w300 + value.getAsString()); } value = element.getAsJsonObject().get("id"); if (!value.isJsonNull()) { upcomingMovie.setTmdbID(value.getAsString()); } value = element.getAsJsonObject().get("original_title"); if (!value.isJsonNull()) { upcomingMovie.setOriginalTitle(value.getAsString()); } value = element.getAsJsonObject().get("poster_path"); if (!value.isJsonNull()) { upcomingMovie.setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w154 + value.getAsString()); } value = element.getAsJsonObject().get("vote_average"); if (!value.isJsonNull()) { upcomingMovie.setTmdbRating(value.getAsDouble()); } //release date value = element.getAsJsonObject().get("release_date"); if (!value.isJsonNull()) { String rlsDateString = value.getAsString(); Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString); upcomingMovie.setReleaseDate(date); String releaseYear = new SimpleDateFormat("yyyy").format(date); if (!releaseYear.isEmpty()) { upcomingMovie.setReleaseYear(Integer.valueOf(releaseYear)); } } } catch (ParseException | NumberFormatException ex) { LOG.error("Erorr while Parsing JSon ELement: " + element.getAsString(), ex); } searchResults.add(upcomingMovie); //{"page":1,"results":[ //{"backdrop_path":null,"id":107811,"original_title":"21 and Over","release_date":"2013-03-01","poster_path":"/6nHApzZiih5MTQAZq02DLnyrY8m.jpg","title":"21 and Over","vote_average":0.0,"vote_count":0}, //{...} //],"total_pages":5,"total_results":100} } } } return searchResults; }
From source file:jmm.api.tmdb.TMDBMovieWrapper.java
License:Open Source License
/** * Get the list of movies playing in theatres. This list refreshes every day. The maximum number of items this list will include is 100. <br/> * <b>Bold</b> parameters are <b>required</b>. * /* ww w.ja v a 2 s. c om*/ * @param page * @param language ISO 639-1 code. * @return A list of now playing movies * @param maxResults the maximum number of results */ public List<TMDBVideoFile> findNowPlayingMovies(String page, String language, int maxResults) { paramsMap.clear(); //optional parameters if (page != null) { paramsMap.put(PARAM_PAGE, page); } if (language != null) { paramsMap.put(PARAM_LANGUAGE, language); } String url = buildUrl(API_MOVIE + "/" + "now_playing"); JsonElement response = callAPI(url); //handle response List<TMDBVideoFile> searchResults = new LinkedList<TMDBVideoFile>(); if (!response.isJsonNull()) { JsonElement key = response.getAsJsonObject().get("results"); if (key.isJsonArray()) { JsonArray elements = key.getAsJsonArray(); int arraySize = elements.size(); if (arraySize > maxResults) { arraySize = maxResults; } for (int i = 0; i < arraySize; i++) { JsonElement element = elements.get(i); String title = element.getAsJsonObject().get("title").getAsString(); TMDBVideoFile nowPlayingMovie = new TMDBVideoFile(title); try { JsonElement value; value = element.getAsJsonObject().get("backdrop_path"); if (!value.isJsonNull()) { nowPlayingMovie .setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w300 + value.getAsString()); } value = element.getAsJsonObject().get("id"); if (!value.isJsonNull()) { nowPlayingMovie.setTmdbID(value.getAsString()); } value = element.getAsJsonObject().get("original_title"); if (!value.isJsonNull()) { nowPlayingMovie.setOriginalTitle(value.getAsString()); } value = element.getAsJsonObject().get("poster_path"); if (!value.isJsonNull()) { nowPlayingMovie .setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w154 + value.getAsString()); } value = element.getAsJsonObject().get("vote_average"); if (!value.isJsonNull()) { nowPlayingMovie.setTmdbRating(value.getAsDouble()); } //release date value = element.getAsJsonObject().get("release_date"); if (!value.isJsonNull()) { String rlsDateString = value.getAsString(); Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString); nowPlayingMovie.setReleaseDate(date); String releaseYear = new SimpleDateFormat("yyyy").format(date); if (!releaseYear.isEmpty()) { nowPlayingMovie.setReleaseYear(Integer.valueOf(releaseYear)); } } } catch (ParseException | NumberFormatException ex) { LOG.error("Error while parsing JSon Element: " + element.getAsString(), ex); } searchResults.add(nowPlayingMovie); //{"page":1,"results": //[{"backdrop_path":"/AbHkwA7Wesm1zg2NcrrpviJ2LDW.jpg","id":145135,"original_title":"Dark Skies","release_date":"2013-02-22","poster_path":"/3CxmCk8Q60a6ilXsc7pEfCtfTJk.jpg","title":"Dark Skies","vote_average":0.0,"vote_count":0}, //{...} //],"total_pages":3,"total_results":52} } } } return searchResults; }
From source file:jmm.api.tmdb.TMDBMovieWrapper.java
License:Open Source License
/** * Get the list of popular movies on The Movie Database. This list refreshes every day. <br/> * <b>Bold</b> parameters are <b>required</b>. * /*from w w w. j a va2s. com*/ * @param page * @param language ISO 639-1 code. * @return A list of popular movies * @param maxResults the maximum number of results */ public List<TMDBVideoFile> findPopularMovies(String page, String language, int maxResults) { paramsMap.clear(); //optional parameters if (page != null) { paramsMap.put(PARAM_PAGE, page); } if (language != null) { paramsMap.put(PARAM_LANGUAGE, language); } String url = buildUrl(API_MOVIE + "/" + "popular"); JsonElement response = callAPI(url); //handle response List<TMDBVideoFile> searchResults = new LinkedList<TMDBVideoFile>(); if (!response.isJsonNull()) { JsonElement key = response.getAsJsonObject().get("results"); if (key.isJsonArray()) { JsonArray elements = key.getAsJsonArray(); int arraySize = elements.size(); if (arraySize > maxResults) { arraySize = maxResults; } for (int i = 0; i < arraySize; i++) { JsonElement element = elements.get(i); String title = element.getAsJsonObject().get("title").getAsString(); TMDBVideoFile popularMovie = new TMDBVideoFile(title); try { JsonElement value; value = element.getAsJsonObject().get("backdrop_path"); if (!value.isJsonNull()) { popularMovie .setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w300 + value.getAsString()); } value = element.getAsJsonObject().get("id"); if (!value.isJsonNull()) { popularMovie.setTmdbID(value.getAsString()); } value = element.getAsJsonObject().get("original_title"); if (!value.isJsonNull()) { popularMovie.setOriginalTitle(value.getAsString()); } value = element.getAsJsonObject().get("poster_path"); if (!value.isJsonNull()) { popularMovie.setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w154 + value.getAsString()); } value = element.getAsJsonObject().get("vote_average"); if (!value.isJsonNull()) { popularMovie.setTmdbRating(value.getAsDouble()); } //release date value = element.getAsJsonObject().get("release_date"); if (!value.isJsonNull()) { String rlsDateString = value.getAsString(); Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString); popularMovie.setReleaseDate(date); String releaseYear = new SimpleDateFormat("yyyy").format(date); if (!releaseYear.isEmpty()) { popularMovie.setReleaseYear(Integer.valueOf(releaseYear)); } } } catch (ParseException | NumberFormatException ex) { LOG.error("Error while parsing JSon Element: " + element.getAsString(), ex); } searchResults.add(popularMovie); //{"page":1,"results": //[{"backdrop_path":"/xizM09IAYfm8W8U4NIdlqzRjHck.jpg","id":47964,"original_title":"A Good Day to Die Hard","release_date":"2013-02-14","poster_path":"/pjtg4pZE9ItGMNhwB5JDObk8TA4.jpg","title":"Stirb langsam - Ein guter Tag zum Sterben","vote_average":5.8,"vote_count":0}, //{..} //],"total_pages":25,"total_results":490} } } } return searchResults; }
From source file:jmm.api.tmdb.TMDBMovieWrapper.java
License:Open Source License
/** * Get the list of top rated movies. By default, this list will only include movies that have 10 or more votes. This list refreshes every day. <br/> * <b>Bold</b> parameters are <b>required</b>. * /*from ww w . ja va 2s. c o m*/ * @param page * @param language ISO 639-1 code. * @return A list of top rated movies * @param maxResults the maximum number of results */ public List<TMDBVideoFile> findTopRatedMovies(String page, String language, int maxResults) { paramsMap.clear(); //optional parameters if (page != null) { paramsMap.put("page", page); } if (language != null) { paramsMap.put("language", language); } String url = buildUrl(API_MOVIE + "/" + "top_rated"); JsonElement response = callAPI(url); //handle response List<TMDBVideoFile> searchResults = new LinkedList<TMDBVideoFile>(); if (!response.isJsonNull()) { JsonElement key = response.getAsJsonObject().get("results"); if (key.isJsonArray()) { JsonArray elements = key.getAsJsonArray(); int arraySize = elements.size(); if (arraySize > maxResults) { arraySize = maxResults; } for (int i = 0; i < arraySize; i++) { JsonElement element = elements.get(i); String title = element.getAsJsonObject().get("title").getAsString(); TMDBVideoFile topRatedMovie = new TMDBVideoFile(title); try { JsonElement value; value = element.getAsJsonObject().get("backdrop_path"); if (!value.isJsonNull()) { topRatedMovie .setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w300 + value.getAsString()); } value = element.getAsJsonObject().get("id"); if (!value.isJsonNull()) { topRatedMovie.setTmdbID(value.getAsString()); } value = element.getAsJsonObject().get("original_title"); if (!value.isJsonNull()) { topRatedMovie.setOriginalTitle(value.getAsString()); } value = element.getAsJsonObject().get("poster_path"); if (!value.isJsonNull()) { topRatedMovie.setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w154 + value.getAsString()); } value = element.getAsJsonObject().get("vote_average"); if (!value.isJsonNull()) { topRatedMovie.setTmdbRating(value.getAsDouble()); } //release date value = element.getAsJsonObject().get("release_date"); if (!value.isJsonNull()) { String rlsDateString = value.getAsString(); Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString); topRatedMovie.setReleaseDate(date); String releaseYear = new SimpleDateFormat("yyyy").format(date); if (!releaseYear.isEmpty()) { topRatedMovie.setReleaseYear(Integer.valueOf(releaseYear)); } } } catch (ParseException | NumberFormatException ex) { LOG.error("Error while parsing JSon Element: " + element.getAsString(), ex); } searchResults.add(topRatedMovie); //{"page":1,"results": //[{"backdrop_path":"/xBKGJQsAIeweesB79KC89FpBrVr.jpg","id":278,"original_title":"The Shawshank Redemption","release_date":"1994-09-14","poster_path":"/f0YDB0jZOwSIMcrEU1Vo8eh2Dh8.jpg","title":"Die Verurteilten","vote_average":9.1,"vote_count":211}, //{...} //],"total_pages":78,"total_results":1556} } } } return searchResults; }
From source file:jmm.api.tmdb.TMDBSearchWrapper.java
License:Open Source License
/** * Search for movies by title. <br/> * <b>Bold</b> parameters are <b>required</b>. * /* w w w . j av a 2 s. co m*/ * @param query <b>the movie title</b> * @param page * @param language ISO 639-1 code. * @param include_adult Toggle the inclusion of adult titles. Expected value is: true or false * @param year Filter results to only include this value. * @param maxResults the maximum number of results */ public List<TMDBVideoFile> searchMovie(String query, String page, String language, Boolean include_adult, String year, int maxResults) { paramsMap.clear(); //requiered parameters if (query == null) { throw new NullPointerException("Param: " + "query" + "must not be null"); } paramsMap.put(PARAM_QUERY, query); //optional parameters if (page != null) { paramsMap.put(PARAM_PAGE, page); } if (language != null) { paramsMap.put(PARAM_LANGUAGE, language); } if (include_adult != null) { paramsMap.put(PARAM_ADULT, include_adult.toString()); } if (year != null) { paramsMap.put(PARAM_YEAR, year); } String url = buildUrl(API_SEARCH + API_MOVIE); JsonElement response = callAPI(url); //handle response List<TMDBVideoFile> searchResults = new LinkedList<TMDBVideoFile>(); if (!response.isJsonNull()) { JsonElement key = response.getAsJsonObject().get("results"); if (key.isJsonArray()) { JsonArray elements = key.getAsJsonArray(); int arraySize = elements.size(); if (arraySize > maxResults) { arraySize = maxResults; } for (int i = 0; i < arraySize; i++) { JsonElement element = elements.get(i); if (element == null || element.isJsonNull()) { continue; } String title = element.getAsJsonObject().get("title").getAsString(); TMDBVideoFile file = new TMDBVideoFile(title); JsonElement value; value = element.getAsJsonObject().get("adult"); if (!value.isJsonNull()) { file.setAdult(value.getAsBoolean()); } value = element.getAsJsonObject().get("backdrop_path"); if (!value.isJsonNull()) { file.setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w300 + value.getAsString()); } value = element.getAsJsonObject().get("id"); if (!value.isJsonNull()) { file.setTmdbID(value.getAsString()); } value = element.getAsJsonObject().get("original_title"); if (!value.isJsonNull()) { file.setOriginalTitle(value.getAsString()); } value = element.getAsJsonObject().get("poster_path"); if (!value.isJsonNull()) { file.setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w92 + value.getAsString()); } value = element.getAsJsonObject().get("vote_average"); if (!value.isJsonNull()) { file.setTmdbRating(value.getAsDouble()); } //release date value = element.getAsJsonObject().get("release_date"); if (!value.isJsonNull()) { String rlsDateString = value.getAsString(); try { if (!rlsDateString.isEmpty()) { Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString); file.setReleaseDate(date); String releaseYear = new SimpleDateFormat("yyyy").format(date); if (!releaseYear.isEmpty()) { file.setReleaseYear(Integer.valueOf(releaseYear)); } } } catch (ParseException | NumberFormatException ex) { LOG.warn("Release Year " + rlsDateString + "cannot be parsed into a valid format for title: " + title + ".", ex); file.setReleaseYear(0); } } searchResults.add(file); //{"adult":false,"backdrop_path":"/eZxahf9ATsv3oQPtvZPqSZkAO7V.jpg","id":12444,"original_title":"Harry Potter and the Deathly Hallows: Part 1", //"release_date":"2010-11-19","poster_path":"/3ssNt5cCFYGne7OrBFkFFUVq8Cx.jpg","popularity":330304.17, //"title":"Harry Potter und die Heiligtmer des Todes - Teil 1","vote_average":8.3,"vote_count":75} } } } return searchResults; }
From source file:jp.pay.model.EventDataDeserializer.java
License:Open Source License
private Object deserializeJsonElement(JsonElement element) { if (element.isJsonNull()) { return null; } else if (element.isJsonObject()) { Map<String, Object> valueMap = new HashMap<String, Object>(); populateMapFromJSONObject(valueMap, element.getAsJsonObject()); return valueMap; } else if (element.isJsonPrimitive()) { return deserializeJsonPrimitive(element.getAsJsonPrimitive()); } else if (element.isJsonArray()) { return deserializeJsonArray(element.getAsJsonArray()); } else {/*from w w w . j a v a 2 s. c o m*/ System.err.println("Unknown JSON element type for element " + element + ". " + "If you're seeing this messaage, it's probably a bug in the Payjp Java " + "library. Please contact us by email at support@pay.jp."); return null; } }
From source file:json.JsonCommonValidation.java
/** * Does the common validation for the specified field in the method * * @param token Validation token/* w ww . jav a2 s. c o m*/ * @param requiredFields A list of all required fields to check through * @param originalString String object passed in to checked * @return JSON Object of errors if there are errors, if not returns null; */ public static JsonObject validate(String token, ArrayList<String> requiredFields, String originalString) { try { JsonObject toReturn = new JsonObject(); JsonArray errorArray = new JsonArray(); JsonParser jsonParser = new JsonParser(); if (originalString == null) { toReturn.addProperty("status", "error"); errorArray.add(new JsonPrimitive("invalid request object")); toReturn.add("message", errorArray); return toReturn; } JsonElement isNull = jsonParser.parse(originalString); if (isNull.isJsonNull()) { toReturn.addProperty("status", "error"); errorArray.add(new JsonPrimitive("invalid request object")); toReturn.add("message", errorArray); return toReturn; } JsonObject value = (JsonObject) jsonParser.parse(originalString); for (String field : requiredFields) { if (value.get(field) == null) { errorArray.add(new JsonPrimitive("missing " + field)); } else if (value.get(field).getAsString().trim().isEmpty()) { errorArray.add(new JsonPrimitive("blank " + field)); } } if (!checkToken(token).isEmpty()) { errorArray.add(new JsonPrimitive((checkToken(token)))); } if (errorArray.size() > 0) { toReturn.addProperty("status", "error"); toReturn.add("message", errorArray); return toReturn; } //return null if successful return null; } catch (Exception e) { JsonArray errorArray = new JsonArray(); JsonObject toReturn = new JsonObject(); toReturn.addProperty("status", "error"); errorArray.add(new JsonPrimitive("invalid request object")); toReturn.add("message", errorArray); return toReturn; } }
From source file:JsonParser.ParseJson.java
public static void dumpJSONElement(JsonElement element, String type) { if (element.isJsonObject()) { //System.out.println("Is an object"); JsonObject obj = element.getAsJsonObject(); java.util.Set<java.util.Map.Entry<String, JsonElement>> entries = obj.entrySet(); java.util.Iterator<java.util.Map.Entry<String, JsonElement>> iter = entries.iterator(); while (iter.hasNext()) { java.util.Map.Entry<String, JsonElement> entry = iter.next(); // System.out.println("Key: " + entry.getKey()); if (entry.getKey().toString().equals("instances")) { System.out.println("............Topic: "); dumpJSONElement(entry.getValue(), "topic"); }//from w w w . j ava 2s. co m if (entry.getKey().toString().equals("aspects")) { System.out.println("............aspects: "); dumpJSONElement(entry.getValue(), "aspect"); } else if (entry.getKey().toString().equals("positives")) { System.out.println(".............Positive Words "); dumpJSONElement(entry.getValue(), "positive"); } else if (entry.getKey().toString().equals("negatives")) { System.out.println(".............negatives Words "); dumpJSONElement(entry.getValue(), "negative"); } else { dumpJSONElement(entry.getValue(), ""); } } } else if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); //System.out.println("Is an array. Number of values: " + array.size()); java.util.Iterator<JsonElement> iter = array.iterator(); while (iter.hasNext()) { JsonElement entry = iter.next(); dumpJSONElement(entry, ""); } } else if (element.isJsonPrimitive()) { //System.out.println("Is a primitive"); JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isBoolean()) { //System.out.println("Is boolean: " + value.getAsBoolean()); } else if (value.isNumber()) { // System.out.println("Is number: " + value.getAsNumber()); } else if (value.isString()) { //if(!value.getAsString().equals("empty")) //{ //if(type.equals("topic")) //{ System.out.println(type + " :" + value.getAsString()); //} } } else if (element.isJsonNull()) { System.out.println("Is NULL"); } else { System.out.println("Error. Unknown type of element"); } }
From source file:leola.web.WebLeolaLibrary.java
License:Open Source License
/** * Converts the {@link JsonElement} into the equivalent {@link LeoObject} * //w ww .j a va 2 s. co m * @param element * @return the {@link LeoObject} */ private static LeoObject toLeoObject(JsonElement element) { if (element == null || element.isJsonNull()) { return LeoObject.NULL; } if (element.isJsonArray()) { JsonArray array = element.getAsJsonArray(); LeoArray leoArray = new LeoArray(array.size()); array.forEach(e -> leoArray.add(toLeoObject(e))); return leoArray; } if (element.isJsonObject()) { JsonObject object = element.getAsJsonObject(); LeoMap leoMap = new LeoMap(); object.entrySet().forEach(entry -> { leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue())); }); return leoMap; } if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return LeoObject.valueOf(primitive.getAsBoolean()); } if (primitive.isNumber()) { return LeoObject.valueOf(primitive.getAsDouble()); } if (primitive.isString()) { return LeoString.valueOf(primitive.getAsString()); } } return LeoObject.NULL; }