List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:it.geosolutions.android.map.geostore.model.GeoStoreAttributeTypeAdapter.java
License:Open Source License
@Override public List<Attribute> deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException { List<Attribute> vals = new ArrayList<Attribute>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { vals.add((Attribute) ctx.deserialize(e, Attribute.class)); }/*w w w .j a v a2 s . c o m*/ } else if (json.isJsonObject()) { vals.add((Attribute) ctx.deserialize(json, Attribute.class)); } else { return vals;//throw new RuntimeException("Unexpected JSON type: " + json.getClass()); } return vals; }
From source file:it.geosolutions.android.map.geostore.model.GeoStoreResourceTypeAdapter.java
License:Open Source License
@Override public List<Resource> deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException { List<Resource> vals = new ArrayList<Resource>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { vals.add((Resource) ctx.deserialize(e, Resource.class)); }//from w w w . j a v a 2s. c o m } else if (json.isJsonObject()) { vals.add((Resource) ctx.deserialize(json, Resource.class)); } else { return vals;//throw new RuntimeException("Unexpected JSON type: " + json.getClass()); } return vals; }
From source file:it.geosolutions.android.map.geostore.model.GeoStoreTypeAdapter.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/* ww w . j ava 2 s.co m*/ public List<T> deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException { List<T> vals = new ArrayList<T>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { vals.add((T) ctx.deserialize(e, this.type)); } } else if (json.isJsonObject()) { vals.add((T) ctx.deserialize(json, type)); } else if (json.isJsonPrimitive()) { //empty is a string return vals; } else { throw new RuntimeException("Unexpected JSON type: " + json.getClass()); } return vals; }
From source file:it.noovle.dataflow.TwitterProcessor.java
License:Open Source License
/** * Flattens nested JsonArrays/*from w ww .j a v a2 s .c om*/ */ private static JsonArray flatten(JsonElement el) { JsonArray result = new JsonArray(); if (el.isJsonArray()) { for (JsonElement arrayEl : el.getAsJsonArray()) { result.addAll(flatten(arrayEl)); } } else { result.add(el); } return result; }
From source file:it.unibo.arces.wot.sepa.pattern.JSAP.java
License:Open Source License
private JsonObject merge(JsonObject temp, JsonObject jsap, boolean replace) { for (Entry<String, JsonElement> entry : temp.entrySet()) { JsonElement value = entry.getValue(); String key = entry.getKey(); if (!jsap.has(key)) { jsap.add(key, value);//w ww. j a v a2 s. c o m continue; } if (value.isJsonPrimitive()) { if (!replace) continue; jsap.add(key, value); } else if (value.isJsonObject()) { JsonObject obj = merge(value.getAsJsonObject(), jsap.getAsJsonObject(key), replace); jsap.add(key, obj); } else if (value.isJsonArray()) { for (JsonElement arr : value.getAsJsonArray()) { jsap.getAsJsonArray(key).add(arr); } } } return jsap; }
From source file:itcc.li.vereinsaustausch.fcmServerComponent.VereinsaustauschFcmServer.java
License:Open Source License
/** * Send client list to newly registered client. When a new client is * registered, that client must be informed about the other registered * clients./*w ww.ja v a2 s . c o m*/ * * @param client * Newly registered client. */ private void sendClientList(ClientModel client) { ArrayList<ClientModel> clientList = new ArrayList<ClientModel>(); for (Entry<String, ClientModel> clientEntry : clientMap.entrySet()) { ClientModel currentClient = clientEntry.getValue(); if (currentClient.registrationToken != client.registrationToken) { clientList.add(currentClient); } } JsonElement clientElements = gson.toJsonTree(clientList, new TypeToken<Collection<ClientModel>>() { }.getType()); if (clientElements.isJsonArray()) { JsonObject jSendClientList = new JsonObject(); JsonObject jData = new JsonObject(); jData.addProperty(ACTION_KEY, SEND_CLIENT_LIST); jData.add(CLIENTS_KEY, clientElements); jSendClientList.add(DATA_KEY, jData); this.send(client.registrationToken, jSendClientList); } }
From source file:javaeetutorial.web.websocketbot.decoders.NBADecoder.java
public List<NBA> decode(String string) { List<NBA> list = new ArrayList<NBA>(); Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonElement jsonEl = parser.parse(string); JsonObject jsonObject = null;/* www . j ava2 s . co m*/ jsonObject = jsonEl.getAsJsonObject(); String reason = jsonObject.get("reason").getAsString(); if (reason.equals("successed!")) { JsonElement resultElement = jsonObject.get("result"); if (resultElement.isJsonArray()) { JsonArray jsonArray = resultElement.getAsjsonArray(); NBA nba; for (Iterator iter = jsonArray.iterator(); iter.hasNext();) { JsonObject obj = (JsonObject) iter.next(); nba = new NBA(); nba = gson.formJson(obj, NBA.class); list.add(nba); } } return list; } else { return null; } }
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; //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()); }// ww w .ja va 2 s. c o m 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 a2s . c om * @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; }