Example usage for com.google.gson JsonElement getAsDouble

List of usage examples for com.google.gson JsonElement getAsDouble

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsDouble.

Prototype

public double getAsDouble() 

Source Link

Document

convenience method to get this element as a primitive double value.

Usage

From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java

License:Apache License

/**
 * convert a jsonElement into Object//from  w ww. j  av a 2 s. c  om
 * 
 * @param type
 * @param element
 * @return
 * @throws RepresentationException
 */
protected <T> T get(Class<T> type, JsonElement element) throws RepresentationException {

    if (type == Long.class) {
        return (T) (Long) element.getAsLong();
    } else if (type == Integer.class) {
        return (T) (Integer) element.getAsInt();
    } else if (type == Short.class) {
        return (T) (Short) element.getAsShort();
    } else if (type == Byte.class) {
        return (T) (Byte) element.getAsByte();
    } else if (type == BigInteger.class) {
        return (T) (BigInteger) element.getAsBigInteger();
    } else if (type == Double.class) {
        return (T) (Double) element.getAsDouble();
    } else if (type == Float.class) {
        return (T) (Float) element.getAsFloat();
    } else if (type == BigDecimal.class) {
        return (T) (BigDecimal) element.getAsBigDecimal();
    } else if (type == Boolean.class) {
        return (T) (Boolean) element.getAsBoolean();
    } else if (type == String.class) {
        return (T) element.getAsString();
    } else {
        return (T) gson.fromJson(element, type);
    }
}

From source file:it.enricocandino.tagme4j.json.RelatednessDeserializer.java

License:MIT License

public Relatedness deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    Relatedness relatedness = null;/*ww  w . j a  v a  2 s .c  o  m*/

    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonElement coupleElem = jsonObject.get("couple");
    JsonElement relElem = jsonObject.get("rel");
    JsonElement errElem = jsonObject.get("err");

    if (coupleElem != null) {
        String couple = coupleElem.getAsString();

        if (couple.contains(" ")) {

            if (relElem != null) {
                String[] entities = couple.split(" ");
                double rel = relElem.getAsDouble();

                relatedness = new Relatedness(entities[0], entities[1], rel);

            } else if (errElem != null) {
                String[] entities = couple.split(" ");
                String err = errElem.getAsString();

                relatedness = new Relatedness(entities[0], entities[1], err);
            }
        }
    }

    return relatedness;
}

From source file:jmm.api.tmdb.TMDBMovieWrapper.java

License:Open Source License

/**
 * Get the basic movie information for a specific movie id. <br/>
 * <b>Bold</b> parameters are <b>required</b>.
 * /*from   w ww  .  java  2 s .  c o  m*/
 * @param id <b>the movie id</b>
 * @param language ISO 639-1 code.
 * @param append_to_response Comma separated, any movie method
 * @return the movie wth the given id <br/>null, if no movie was found with the given id
 */
public TMDBVideoFile findMovie(String id, String language, String append_to_response) {
    paramsMap.clear();
    //requiered parameters
    if (id == null) {
        throw new NullPointerException("Param: " + "id" + "must not be null");
    }
    //optional parameters
    if (language != null) {
        paramsMap.put(PARAM_LANGUAGE, language);
    }
    if (append_to_response != null) {
        paramsMap.put(PARAM_APPEND, append_to_response);
    }

    String url = buildUrl(API_MOVIE + "/" + id);
    JsonElement element = callAPI(url);
    //handle response
    TMDBVideoFile searchResult = null;
    if (!element.isJsonNull()) {
        String title = element.getAsJsonObject().get("title").getAsString();
        searchResult = new TMDBVideoFile(title);
        JsonElement value;
        value = element.getAsJsonObject().get("adult");
        if (!value.isJsonNull()) {
            searchResult.setAdult(value.getAsBoolean());
            if (searchResult.isAdult()) {
                searchResult.setFsk(VideoFile.FSK.FSK_18);
            }
        }
        value = element.getAsJsonObject().get("backdrop_path");
        if (!value.isJsonNull()) {
            searchResult.setBackdropPath(API_BASE_IMAGEURL + Backdrop_Sizes.w780 + value.getAsString());
        }
        //genres
        value = element.getAsJsonObject().get("genres");
        if (!value.isJsonNull()) {
            JsonArray genres = value.getAsJsonArray();
            if (genres.isJsonArray()) {
                Iterator<JsonElement> genresIterator = genres.iterator();
                while (genresIterator.hasNext()) {
                    JsonElement genreElement = genresIterator.next();
                    value = genreElement.getAsJsonObject().get("id");
                    if (!value.isJsonNull()) {
                        int genreID = value.getAsInt();
                        Genre genre = Genre.getGenreByTMDBID(genreID);
                        if (genre != null) {
                            searchResult.addGenreKey(genre.getGenreKey());
                        } else {
                            value = genreElement.getAsJsonObject().get("name");
                            String exceptionString;
                            if (!value.isJsonNull()) {
                                exceptionString = "Missing genre in resource bundle: " + value.getAsString()
                                        + " " + genreID;
                            } else {
                                exceptionString = "Missing genre in resource bundle: " + genreID;
                            }
                            LOG.warn(exceptionString);
                        }
                    }
                    //                            value = genreElement.getAsJsonObject().get("name");
                    //                            if(!value.isJsonNull()){                    
                    //                                String localizedGenre = value.getAsString();
                    //                                String genreKey = LocaleManager.getInstance().determineGenreKeyByLocale(localizedGenre);
                    //                                if(genreKey == null){
                    //                                    MyExceptionHandler.uncaughtExceptionLogger.warning("Missing genre in resource bundle: " + localizedGenre);
                    //                                }else{
                    //                                    searchResult.addGenre(genreKey);
                    //                                }
                    //                            }
                }
            }
        }
        value = element.getAsJsonObject().get("id");
        if (!value.isJsonNull()) {
            searchResult.setTmdbID(value.getAsString());
        }
        value = element.getAsJsonObject().get("imdb_id");
        if (!value.isJsonNull()) {
            searchResult.setImdbID(value.getAsString());
        }
        value = element.getAsJsonObject().get("original_title");
        if (!value.isJsonNull()) {
            searchResult.setOriginalTitle(value.getAsString());
        }
        value = element.getAsJsonObject().get("overview");
        if (!value.isJsonNull()) {
            searchResult.setDescription(value.getAsString());
        }
        value = element.getAsJsonObject().get("poster_path");
        if (!value.isJsonNull()) {
            searchResult.setImagePath(API_BASE_IMAGEURL + Poster_Sizes.w342 + value.getAsString());
        }
        //Publisher
        value = element.getAsJsonObject().get("production_companies");
        if (!value.isJsonNull()) {
            JsonArray publishers = value.getAsJsonArray();
            if (publishers.isJsonArray()) {
                Iterator<JsonElement> publishersIterator = publishers.iterator();
                while (publishersIterator.hasNext()) {
                    //Set only the first publisher
                    JsonElement publisherElement = publishersIterator.next();
                    value = publisherElement.getAsJsonObject().get("name");
                    if (!value.isJsonNull()) {
                        String publisher = value.getAsString();
                        searchResult.setPublisher(publisher);
                        break;
                    }
                }
            }
        }
        value = element.getAsJsonObject().get("runtime");
        if (!value.isJsonNull()) {
            searchResult.setPlaytime(value.getAsInt());
        }
        value = element.getAsJsonObject().get("vote_average");
        if (!value.isJsonNull()) {
            searchResult.setTmdbRating(value.getAsDouble());
            searchResult.setOnlineRating(value.getAsDouble());
        }
        //release date
        value = element.getAsJsonObject().get("release_date");
        if (!value.isJsonNull()) {
            String rlsDateString = value.getAsString();
            try {
                Date date = new SimpleDateFormat("yyyy-mm-dd").parse(rlsDateString);
                searchResult.setReleaseDate(date);
                String releaseYear = new SimpleDateFormat("yyyy").format(date);
                if (!releaseYear.isEmpty()) {
                    searchResult.setReleaseYear(Integer.valueOf(releaseYear));
                }
            } catch (ParseException | NumberFormatException ex) {
                LOG.warn("Release Year " + rlsDateString + "cannot be parsed into a valid format for title: "
                        + title + ".", ex);
                searchResult.setReleaseYear(0);
            }
        }

        //{"adult":false,"backdrop_path":"/eZxahf9ATsv3oQPtvZPqSZkAO7V.jpg","belongs_to_collection":{"id":1241,"name":"Harry Potter Collection",
        //"poster_path":"/fuWOg0iLKPRGTlg7lq4tWDBt5tu.jpg","backdrop_path":"/tpDcuXZGqEoU6CxuJ7e4S2NTIoS.jpg"},"budget":250000000,
        //"genres":[{"id":12,"name":"Abenteuer"},{"id":18,"name":"Drama"},{"id":14,"name":"Fantasy"},{"id":10751,"name":"Familie"}],
        //"homepage":"http://harrypotter.warnerbros.de/hp7a/","id":12444,"imdb_id":"tt0926084","original_title":"Harry Potter and the Deathly Hallows: Part 1",
        //"overview":"Harry sieht sich einer vollkommen vernderten Welt gegenber. Die Todesesser haben das Zauberministerium unter ihre Kontrolle gebracht und es tobt ein offener Kampf zwischen den Mchten des Guten und Bsen. Harry hat sich mit Hermine (Emma Watson) und Ron (Rupert Grint) auf die Suche nach den Horkuxen, magischen Objekten, die die Unsterblichkeit von Lord Voldemort (Ralph Fiennes) garantieren und zerstrt werden mssen. Der dunkle Lord hat seinerseits ein Kopfgeld auf Harry ausgesetzt, dener will sich das Vergngen den Jungen der lebte mit den eigenen Hnden zu tten, nicht nehmen lassen. Unterdessen stt Harry auf die Legende von den Heiligtmern des Todes, die ihm im Kampf gegen seinen Erzfeind das Leben retten knnte. Und so strebt alles unaufhaltsam der finalen Konfrontation zwischen den beiden Magiern entegegen, auf die Harry sich seit Beginn seiner Schullaufbahn vorbereitet hat.",
        //"popularity":305437.691,"poster_path":"/3ssNt5cCFYGne7OrBFkFFUVq8Cx.jpg","production_companies":[{"name":"Warner Bros. Pictures","id":174}],
        //"production_countries":[{"iso_3166_1":"US","name":"United States of America"},{"iso_3166_1":"GB","name":"United Kingdom"}],
        //"release_date":"2010-11-19","revenue":954305868,"runtime":146,
        //"spoken_languages":[{"iso_639_1":"en","name":"English"},{"iso_639_1":"fr","name":"Franais"},{"iso_639_1":"ja","name":"???"}],
        //"status":"Released","tagline":"","title":"Harry Potter und die Heiligtmer des Todes - Teil 1","vote_average":8.3,"vote_count":74}
        if (append_to_response != null && append_to_response.equalsIgnoreCase("casts")) {
            addActorsAndDirector(element, searchResult);
        }
    }
    return searchResult;
}

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  ww .  ja  v  a 2 s . com
 * @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>.
 * /*from w  w w.  j av  a2s.com*/
 * @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  ww w  .  j a va 2  s  . c  om*/
 * @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 w w w . j  a  v  a 2 s.  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 2s  .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:JSONformating.reader.JSONReader.java

public static JValue getJValue(JsonObject cell) throws IOException {
    JSONFormatType type = JSONFormatType.getType(cell.get("type").getAsString());
    JsonElement value = cell.get("value");
    switch (type) {
    case BOOLEAN:
        JBooleanValue boolValue = new JBooleanValue();
        boolValue.setValue(value.getAsBoolean());
        return boolValue;
    case DATE://from w w  w  .j  a v  a 2  s.  c  om
    case IMAGE:
    case STRING:
    case URL:
    case VERSION:
        JStringValue stringValue = new JStringValue();
        stringValue.setValue(value.getAsString());
        return stringValue;
    case INTEGER:
    case REAL:
        JNumberValue numValue = new JNumberValue();
        numValue.setValue(value.getAsDouble());
        return numValue;
    case MULTIPLE:
        JMultipleValue mulValue = new JMultipleValue();
        JsonArray array = value.getAsJsonArray();
        for (JsonElement j : array) {
            if (j instanceof JsonNull) {
                System.out.println("JsonNull in JSONReader.getJValue(...)");
                System.out.println(value.toString());
            } else {
                mulValue.addValue(getJValueForMultiple(j));
            }
        }
        return mulValue;
    case UNDEFINED:
    default:
        return null;
    }
}

From source file:JSONformating.reader.JSONReader.java

private static JValue getJValueForMultiple(JsonElement j) {
    try {/*from  ww w.  j a  v a  2 s .c om*/
        Double d = j.getAsDouble();
        JNumberValue numberValue = new JNumberValue();
        numberValue.setValue(d);
        return numberValue;
    } catch (java.lang.NumberFormatException e) {
        String str = j.getAsString();
        if (str.equals("true") || str.equals("false")) {
            JBooleanValue booleanValue = new JBooleanValue();
            booleanValue.setValue(j.getAsBoolean());
            return booleanValue;
        } else {
            JStringValue stringValue = new JStringValue();
            stringValue.setValue(str);
            return stringValue;
        }
    }
}