List of usage examples for com.google.gson JsonObject remove
public JsonElement remove(String property)
From source file:com.adssets.ejb.DataAccess.java
@Override public String buildFeedForMarket(String marketId, String lazyLoad) { JsonArray jsonArray = new JsonArray(); JsonParser parser = new JsonParser(); // Feed feed = feedFacade.find(marketId); Market market = marketFacade.find(Integer.valueOf(marketId)); List<Feed> feeds = em.createNamedQuery("Feed.findByMarketId").setParameter("marketId", market) .getResultList();/*from w w w . j a v a2 s. c om*/ if (feeds.size() > 0) { for (Feed feed : feeds) { JsonObject obj = new JsonObject(); obj.addProperty("id", feed.getId()); obj.addProperty("marketId", feed.getIdmarket().getId()); obj.addProperty("json", feed.getJson()); jsonArray.add(obj); } JsonObject obj = jsonArray.get(0).getAsJsonObject(); String objArray = obj.get("json").getAsString(); JsonArray objArrayParsed = parser.parse(objArray).getAsJsonArray(); JsonArray objData = new JsonArray(); for (JsonElement objId : objArrayParsed) { JsonObject objElmParsed = parser.parse(objId.toString()).getAsJsonObject(); System.out.println(objElmParsed); // try{ // isInteger(objId.getAsString()); String value = objElmParsed.get("type").getAsString(); System.out.println(value); //CHANGE ADPICTURE TO THE PICTURE STORED IN THE DATABASE if (value.equals("object")) { String result = scout24.getApartments(objElmParsed.get("objectid").getAsString()); if (!result.equals("{\"error\":1}")) { JsonObject resultObj = parser.parse(result).getAsJsonObject(); String link = "{\"link\": \"https://www.immobilienscout24.de/expose/" + objElmParsed.get("objectid").getAsInt() + "?referrer=\"}"; JsonObject clickLink = parser.parse(link).getAsJsonObject(); resultObj.add("clickLink", clickLink); resultObj.add("objectId", objElmParsed.get("objectid")); resultObj.add("marketId", parser.parse(marketId)); // Use the selected image as ad image JsonObject objHref = new JsonObject(); objHref.add("href", objElmParsed.get("url")); resultObj.add("adpicture", objHref); //If lazyLoad = yes dont return "allpictures" LazyLoad=yes is the parameter the ad will send so it does not get unneccesary data if (lazyLoad.equals("yes")) { resultObj.remove("allpictures"); } objData.add(resultObj); } } else { objData.add(objId); } // }catch(UnsupportedOperationException ex){ // objData.add(objId); // } } return objData.toString(); } else { return new JsonArray().toString(); } }
From source file:com.apifest.example.RemoveBalanceFilter.java
License:Apache License
@Override public HttpResponse execute(HttpResponse response) { JsonParser parser = new JsonParser(); JsonObject json = parser.parse(response.getContent().toString(CharsetUtil.UTF_8)).getAsJsonObject(); log.info("response body: " + json.toString()); json.remove("balance"); log.info("modified response body: " + json.toString()); byte[] newContent = json.toString().getBytes(CharsetUtil.UTF_8); response.setContent(ChannelBuffers.copiedBuffer(newContent)); HttpHeaders.setContentLength(response, newContent.length); return response; }
From source file:com.apifest.oauth20.MongoDBManager.java
License:Apache License
@Override @SuppressWarnings("unchecked") public boolean storeScope(Scope scope) { boolean stored = false; String id = scope.getScope(); Gson gson = new Gson(); String json = gson.toJson(scope); JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(json).getAsJsonObject(); jsonObj.remove("scope"); // use scope name as _id jsonObj.addProperty(ID_NAME, id);// w ww . j a v a 2s . c o m try { // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00 Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class); // if scope already exits, updates it, otherwise creates the scope BasicDBObject query = new BasicDBObject(ID_NAME, id); BasicDBObject newObject = new BasicDBObject(result); DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME); coll.update(query, newObject, true, false); stored = true; } catch (JsonParseException e) { log.error("cannot store scope {}", scope.getScope(), e); } catch (JsonMappingException e) { log.error("cannot store scope {}", scope.getScope(), e); } catch (IOException e) { log.error("cannot store scope {}", scope.getScope(), e); } return stored; }
From source file:com.apifest.oauth20.MongoDBManager.java
License:Apache License
protected String constructDbId(Object object) { Gson gson = new Gson(); String json = gson.toJson(object); JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(json).getAsJsonObject(); if (jsonObj.has("id")) { String id = jsonObj.get("id").getAsString(); jsonObj.remove("id"); jsonObj.addProperty(ID_NAME, id); }/*from ww w .j a va 2s .c o m*/ return jsonObj.toString(); }
From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java
License:Apache License
@Override @SuppressWarnings("unchecked") public boolean storeScope(Scope scope) { boolean stored = false; String id = scope.getScope(); Gson gson = new Gson(); String json = gson.toJson(scope); JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(json).getAsJsonObject(); jsonObj.remove("scope"); // use scope name as _id jsonObj.addProperty(CLIENTS_ID, id); try {//from w w w . j a v a 2 s . c o m // use ObjectMapper in order to represent expiresIn as integer not as double - 100 instead of 100.00 Map<String, Object> result = new ObjectMapper().readValue(jsonObj.toString(), Map.class); // if scope already exits, updates it, otherwise creates the scope BasicDBObject query = new BasicDBObject(CLIENTS_ID, id); BasicDBObject newObject = new BasicDBObject(result); DBCollection coll = db.getCollection(SCOPE_COLLECTION_NAME); coll.update(query, newObject, true, false); stored = true; } catch (JsonParseException e) { log.error("cannot store scope {}", scope.getScope(), e); } catch (JsonMappingException e) { log.error("cannot store scope {}", scope.getScope(), e); } catch (IOException e) { log.error("cannot store scope {}", scope.getScope(), e); } return stored; }
From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java
License:Apache License
protected String constructDbId(Object object) { Gson gson = new Gson(); String json = gson.toJson(object); JsonParser parser = new JsonParser(); JsonObject jsonObj = parser.parse(json).getAsJsonObject(); if (jsonObj.has("id")) { String id = jsonObj.get("id").getAsString(); jsonObj.remove("id"); jsonObj.addProperty(CLIENTS_ID, id); }/*from w w w . j a v a2 s . co m*/ return jsonObj.toString(); }
From source file:com.azure.webapi.JsonEntityParser.java
License:Open Source License
/** * Changes returned JSon object's id property name to match with type's id property name. * @param element/*from w w w .j a v a2 s. co m*/ * @param propertyName */ private static void changeIdPropertyName(JsonObject element, String propertyName) { // If the property name is id or if there's no id defined, then return without performing changes if (propertyName.equals("id") || propertyName.length() == 0) return; // Get the current id value and remove the JSon property JsonElement idElement = element.get("id"); if (idElement != null) { String value = idElement.getAsString(); element.remove("id"); // Create a new id property using the given property name element.addProperty(propertyName, value); } }
From source file:com.azure.webapi.MobileServiceJsonTable.java
License:Open Source License
/** * Removes the Id property from a JsonObject * //from w ww . j av a2 s .c o m * @param json * The JsonObject to modify */ private void removeIdFromJson(final JsonObject json) { // Remove id property if exists String[] idPropertyNames = new String[] { "id", "Id", "iD", "ID" }; for (int i = 0; i < 4; i++) { String idProperty = idPropertyNames[i]; if (json.has(idProperty)) { JsonElement idElement = json.get(idProperty); if (isValidTypeId(idElement) && idElement.getAsInt() != 0) { throw new InvalidParameterException( "The entity to insert should not have " + idProperty + " property defined"); } json.remove(idProperty); } } }
From source file:com.azure.webapi.MobileServiceTableBase.java
License:Open Source License
/** * Updates the JsonObject to have an id property * @param json/*from w w w . jav a 2 s . c om*/ * the element to evaluate */ protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException { for (Map.Entry<String, JsonElement> entry : json.entrySet()) { String key = entry.getKey(); if (key.equalsIgnoreCase("id")) { JsonElement element = entry.getValue(); if (isValidTypeId(element)) { if (!key.equals("id")) { //force the id name to 'id', no matter the casing json.remove(key); // Create a new id property using the given property name json.addProperty("id", entry.getValue().getAsNumber()); } return; } else { throw new IllegalArgumentException("The id must be numeric"); } } } }
From source file:com.codereligion.bugsnag.logback.resource.TabVOSerializer.java
License:Apache License
private void filterJsonObject(final JsonObject jsonObject) { for (final Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { final String key = entry.getKey(); if (jsonFilterProvider.isIgnoredByFilter(key)) { jsonObject.remove(key); } else {/* w ww . jav a2s . c om*/ filterJsonElement(entry.getValue()); } } }