Example usage for com.google.gson JsonElement getAsBoolean

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

Introduction

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

Prototype

public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:diffboat.type.json.util.JsonUtil.java

License:Open Source License

/**
 * Returns a {@link Boolean} value of a given {@link JsonElement}.
 * /*from w ww  .ja  va 2s.  c  o  m*/
 * @param jsonElement The {@link JsonElement} to get its boolean value.
 * @return a {@link Boolean} value of the given {@link JsonElement}.
 */
public static boolean asBoolean(JsonElement jsonElement) {
    return jsonElement == null ? false : jsonElement.getAsBoolean();
}

From source file:ee.ria.xroad.asyncdb.messagequeue.JsonUtils.java

License:Open Source License

static boolean getBooleanPropertyValue(JsonObject jsonObject, String memberName) {
    JsonElement jsonElement = jsonObject.get(memberName);

    return jsonElement != null && jsonElement.getAsBoolean();
}

From source file:ezbake.services.search.utils.BooleanSerializer.java

License:Apache License

@Override
public Boolean deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2)
        throws JsonParseException {
    if (arg0.getAsJsonPrimitive().isBoolean()) {
        return arg0.getAsBoolean();
    }/*from   w ww.j a v  a 2  s. co m*/
    if (arg0.getAsJsonPrimitive().isString()) {
        return Boolean.parseBoolean(arg0.getAsString());
    } else {
        return arg0.getAsInt() == 1 ? true : false;
    }
}

From source file:io.flutter.inspector.DiagnosticsNode.java

License:Open Source License

private boolean getBooleanMember(String memberName, boolean defaultValue) {
    if (!json.has(memberName)) {
        return defaultValue;
    }/*from   w  w  w. j a va2  s.  c  o m*/
    final JsonElement value = json.get(memberName);
    if (value instanceof JsonNull) {
        return defaultValue;
    }
    return value.getAsBoolean();
}

From source file:io.flutter.inspector.InspectorService.java

License:Open Source License

/**
 * If the widget tree is not ready, the application should wait for the next
 * Flutter.Frame event before attempting to display the widget tree. If the
 * application is ready, the next Flutter.Frame event may never come as no
 * new frames will be triggered to draw unless something changes in the UI.
 *//*w  ww  . java  2s . com*/
public CompletableFuture<Boolean> isWidgetTreeReady() {
    if (isDaemonApiSupported) {
        return invokeServiceMethodDaemonNoGroup("isWidgetTreeReady", new HashMap<>())
                .thenApplyAsync((JsonElement element) -> element.getAsBoolean() == true);
    } else {
        return invokeServiceMethodObservatoryNoGroup("isWidgetTreeReady")
                .thenApplyAsync((InstanceRef ref) -> "true".equals(ref.getValueAsString()));
    }
}

From source file:io.openvidu.server.kurento.core.KurentoSessionManager.java

License:Apache License

@Override
public void streamPropertyChanged(Participant participant, Integer transactionId, String streamId,
        String property, JsonElement newValue, String reason) {
    KurentoParticipant kParticipant = (KurentoParticipant) participant;
    streamId = kParticipant.getPublisherStreamId();
    MediaOptions streamProperties = kParticipant.getPublisherMediaOptions();

    Boolean hasAudio = streamProperties.hasAudio();
    Boolean hasVideo = streamProperties.hasVideo();
    Boolean audioActive = streamProperties.isAudioActive();
    Boolean videoActive = streamProperties.isVideoActive();
    String typeOfVideo = streamProperties.getTypeOfVideo();
    Integer frameRate = streamProperties.getFrameRate();
    String videoDimensions = streamProperties.getVideoDimensions();
    KurentoFilter filter = streamProperties.getFilter();

    switch (property) {
    case "audioActive":
        audioActive = newValue.getAsBoolean();
        break;//from  w  w w . ja v  a2s .c  o  m
    case "videoActive":
        videoActive = newValue.getAsBoolean();
        break;
    case "videoDimensions":
        videoDimensions = newValue.getAsString();
        break;
    }

    kParticipant.setPublisherMediaOptions(new MediaOptions(hasAudio, hasVideo, audioActive, videoActive,
            typeOfVideo, frameRate, videoDimensions, filter));

    sessionEventsHandler.onStreamPropertyChanged(participant, transactionId,
            kParticipant.getSession().getParticipants(), streamId, property, newValue, reason);
}

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

License:Apache License

/**
 * convert a jsonElement into Object//from w w  w .  j  a v  a 2 s.  c  o  m
 * 
 * @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:io.thinger.thinger.views.BoolValue.java

License:Open Source License

@Override
public void refreshContent(JsonElement element) {
    if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isBoolean()) {
        button.setChecked(element.getAsBoolean());
    }/*w w  w .j  av a  2s  .  c  om*/
}

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 w  w  .  ja  v  a  2s .  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.TMDBSearchWrapper.java

License:Open Source License

/**
 * Search for movies by title. <br/>
 * <b>Bold</b> parameters are <b>required</b>.
 * /*from   w  w  w . j a  v  a 2  s  .c  o  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;
}