List of usage examples for com.google.gson JsonObject remove
public JsonElement remove(String property)
From source file:com.vmware.admiral.test.upgrade.version2.UpgradeNewService7StateConverter.java
License:Open Source License
@Override public UpgradeNewService7State deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.has("field3")) { // "field3" renamed to "upgradedField3" JsonElement field3 = jsonObject.remove("field3"); String field3Value = field3.getAsString(); jsonObject.addProperty("upgradedField3", field3Value); }// w ww . j a v a 2 s .c om return JSON_MAPPER.fromJson(json, UpgradeNewService7State.class); }
From source file:com.vmware.admiral.test.upgrade.version2.UpgradeNewService7StateConverter.java
License:Open Source License
@Override public JsonElement serialize(UpgradeNewService7State state, Type typeOfSrc, JsonSerializationContext context) { String version = ThreadLocalVersionHolder.getVersion(); String json = JSON_MAPPER.toJson(state); JsonElement jsonElement = JSON_PARSER.parse(json); if (ReleaseConstants.API_VERSION_0_9_1.equals(version)) { /*/*from ww w . j a va2s. c o m*/ * Handle the backward compatibility with the previous version */ JsonObject jsonObject = jsonElement.getAsJsonObject(); // "upgradedField3" renamed to "field3" JsonElement field3 = jsonObject.remove("upgradedField3"); String field3Value = field3.getAsString(); jsonObject.addProperty("field3", field3Value); } return jsonElement; }
From source file:com.vmware.dcp.common.DigestThreadLocal.java
License:Open Source License
public static Object setJsonProperty(Object body, String fieldName, String fieldValue) { JsonObject jo = null; if (body instanceof JsonObject) { jo = (JsonObject) body;//from w w w .ja va 2s .c o m } else { jo = new JsonParser().parse((String) body).getAsJsonObject(); } jo.remove(fieldName); if (fieldValue != null) { jo.addProperty(fieldName, fieldValue); } return jo; }
From source file:com.vmware.xenon.common.Utils.java
License:Open Source License
public static Object setJsonProperty(Object body, String fieldName, String fieldValue) { JsonObject jo; if (body instanceof JsonObject) { jo = (JsonObject) body;/* w w w .j a v a 2s.c o m*/ } else { jo = new JsonParser().parse((String) body).getAsJsonObject(); } jo.remove(fieldName); if (fieldValue != null) { jo.addProperty(fieldName, fieldValue); } return jo; }
From source file:com.zack6849.alphabot.api.PermissionManager.java
License:Open Source License
public void save() { System.out.println("Saving...."); Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser parser = new JsonParser(); String json = null;// ww w . j a v a2s . c om try { json = Files.toString(new File("permissions.json"), Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset()); JsonElement jelement = parser.parse(json); JsonObject output = jelement.getAsJsonObject(); HashMap<String, String> masks = new HashMap<String, String>(); for (Group g : getGroups()) { if (!g.getName().equalsIgnoreCase("default")) { for (User u : g.getUsers().keySet()) { if (!masks.containsKey(g.getUsers().get(u))) { masks.put(g.getUsers().get(u), g.getName()); } } } } JsonElement obj = output.get("users"); Set<Map.Entry<String, JsonElement>> users = output.get("users").getAsJsonObject().entrySet(); Iterator it = users.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); masks.put(entry.getKey().toString().replaceAll("\"", ""), entry.getValue().toString().replaceAll("\"", "")); } for (String s : masks.keySet()) { obj.getAsJsonObject().remove(s); obj.getAsJsonObject().addProperty(s, masks.get(s)); } JsonObject gr = output.get("groups").getAsJsonObject(); for (Group g : groups) { gr.remove(g.getName()); //temporary group object to store the "new" group object //loop through all permissions in the actual group, and if the permission is inherited, then remove it. List<String> permissions = new ArrayList<String>(); Iterator<Permission> iter = g.getPermissions().iterator(); while (iter.hasNext()) { Permission perm = iter.next(); if (!perm.isInheirited()) { permissions.add(perm.getPermission()); } } JsonObject group = parser.parse(gson.toJson(g)).getAsJsonObject(); group.remove("permissions"); group.add("permissions", parser.parse(gson.toJson(permissions))); group.remove("inheritance"); group.add("inheritance", parser.parse(gson.toJson(g.getInheritance()))); gr.add(g.getName(), group); } output.remove("groups"); output.add("groups", gr); output.remove("users"); output.add("users", obj); Files.write(gson.toJson(output), new File("permissions.json"), Charset.isSupported("UTF-8") ? Charset.forName("UTF-8") : Charset.defaultCharset()); System.out.println("6"); } catch (IOException e) { e.printStackTrace(); } }
From source file:de.dfki.mmf.input.worldmodel.WorldModelFactory.java
License:Open Source License
private void jsonObjectToLowerCase(JsonObject object) { if (object.isJsonNull()) { return;//from w ww . j ava 2s .c o m } ArrayList<Map.Entry<String, JsonElement>> modifiedEntryList = new ArrayList<>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { if (entry.getValue().isJsonObject()) { jsonObjectToLowerCase(entry.getValue().getAsJsonObject()); } if (entry.getValue().isJsonArray()) { JsonArray array = entry.getValue().getAsJsonArray(); jsonArrayToLowerCase(array); Map.Entry<String, JsonElement> newEntry = new AbstractMap.SimpleEntry<String, JsonElement>( entry.getKey(), array); modifiedEntryList.add(newEntry); } if (entry.getValue().isJsonPrimitive()) { Gson gson = new Gson(); String entryString = entry.getValue().getAsString().toLowerCase(); String jsonString = gson.toJson(entryString); JsonElement element = gson.fromJson(jsonString, JsonElement.class); Map.Entry<String, JsonElement> newEntry = new AbstractMap.SimpleEntry<String, JsonElement>( entry.getKey(), element); modifiedEntryList.add(newEntry); } } for (Map.Entry<String, JsonElement> newEntry : modifiedEntryList) { object.remove(newEntry.getKey()); object.add(newEntry.getKey().toLowerCase(), newEntry.getValue()); } return; }
From source file:de.innovationgate.wgpublisher.webtml.portlet.TMLPortletState.java
License:Open Source License
public void setContextPath(String contextPath) { JsonObject state = getControllerState(); if (contextPath == null || contextPath.trim().equalsIgnoreCase(TMLPortlet.PORTLETCONTEXT_NONE)) { if (state.has(STATEPROP_CONTEXT)) { state.remove(STATEPROP_CONTEXT); }//w w w. j a va2s. co m } else { state.addProperty(STATEPROP_CONTEXT, contextPath); } _transmitToClient = true; }
From source file:edu.mit.media.funf.config.ConfigRewriteUtil.java
License:Open Source License
/** * If the given JsonObject contains a member named "@schedule", returns * a CompositeDataSource./*w w w. j a v a 2s .c o m*/ * * Renames the "strict" property in the schedule object to "exact" in * the AlarmProbe. * * eg. * { "@probe": ".AccelerometerSensorProbe", * "sensorDelay": "MEDIUM", * "@schedule": { "@probe": ".ActivityProbe", * "sensorDelay": "FASTEST", * "@schedule": { "interval": 60, "offset": 12345 }, * "@filters": [ * { "@type": ".KeyValueFilter", * "matches": { "motionState": "Driving" } }, * { "@type": ".ProbabilityFilter", "probability": 0.5 } * ] } * } * * will be rewritten as: * * { "@type": "edu.mit.media.fun.datasource.CompositeDataSource", * "source": { "@type": "edu.mit.media.fun.datasource.CompositeDataSource", * "source": { "@probe": ".AlarmProbe", "interval": 60, "offset": 12345 }, * "@action": { "@type": ".StartDataSourceAction", * "delegate": { "@probe": ".ActivityProbe", * "sensorDelay": "FASTEST", * "@filters": [ * { "@type": ".KeyValueFilter", * "matches": { "motionState": "Driving" } }, * { "@type": ".ProbabilityFilter", * "probability": 0.5 } * ] } } } * "@action": { "@type: ".StartDataSourceAction", * "delegate": { "@probe": ".AccelerometerSensorProbe", * "sensorDelay": "MEDIUM" } } } * * @param baseObj The JsonObject which contains a member named "@schedule" */ public static JsonObject rewriteScheduleAnnotation(JsonObject baseObj) { if (baseObj == null) return null; // The CompositeDataSource which will be returned from this rewrite. JsonObject dataSourceObj = new JsonObject(); dataSourceObj.addProperty(TYPE, COMPOSITE_DS); JsonObject scheduleObj = (JsonObject) baseObj.remove(SCHEDULE); // If scheduleObj is already a CompositeDataSource, it implies that // there was a nested @schedule annotation which has already been // taken care of by an earlier call to this function. In that case // the scheduleObj will simply be taken as a nested "source" of // the current CompositeDataSource. if (!isDataSourceObject(scheduleObj)) { // If it is not a data source, then the "@probe" annotation in the // scheduleObj will be parsed as a data source by rewriteProbeAnnotation(). // For compatibility, "@type" annotations indicating probes are // converted to "@probe" annotations. // For compatibility, if neither "@type" or "@probe" annotations // exist, an "AlarmProbe" is added (as that signifies the default behavior // of the earlier scheduling system). if (scheduleObj.has(TYPE)) { renameJsonObjectKey(scheduleObj, TYPE, PROBE); } else if (!scheduleObj.has(PROBE)) { scheduleObj.addProperty(PROBE, ALARM_PROBE); } } Double duration = 0.0; if (scheduleObj.has(PROBE) && ALARM_PROBE.equals(scheduleObj.get(PROBE).getAsString())) { renameJsonObjectKey(scheduleObj, "strict", "exact"); if (scheduleObj.has(DURATION_FIELD_NAME)) duration = scheduleObj.remove(DURATION_FIELD_NAME).getAsDouble(); } JsonElement filtersEl = null; if (scheduleObj.has(FILTER)) { filtersEl = scheduleObj.remove(FILTER); } // If baseObj is itself a schedule object (i.e this is a nested schedule), // a "@trigger" annotation would provide the action to be performed by // the outer schedule object. This must be kept separate from other members // of baseObj. JsonObject triggerObj = null; if (baseObj.has(TRIGGER)) { triggerObj = baseObj.remove(TRIGGER).getAsJsonObject(); } // To select the action to be performed whenever this schedule object fires: // 1. First check if the baseObj is really a probe or a data source. If // it is empty except for the "@schedule" tag, then don't register any action. // (Happens in the direct "schedules" member.) // 2. If it is not empty, then check if a "@trigger" annotation exists, which denotes // a user-specified custom action to be registered whenever scheduler fires. // 3. If no "@trigger" exists, then select a default Action. If "duration" was // specified in the schedule object, add a StartableAction to run // the dependent data source for that duration. // 4. If no non-zero duration was specified, add a StartDataSourceAction to simply start // the dependent data source whenever this schedule object fires. JsonObject actionObj = null; if (baseObj.has(PROBE) || baseObj.has(TYPE)) { if (!isDataSourceObject(baseObj)) { renameJsonObjectKey(baseObj, TYPE, PROBE); } if (scheduleObj.has(TRIGGER)) { actionObj = scheduleObj.remove(TRIGGER).getAsJsonObject(); } else { actionObj = new JsonObject(); if (duration > 0) { actionObj.addProperty(TYPE, STARTABLE_ACTION); actionObj.addProperty(DURATION_FIELD_NAME, duration); } else { actionObj.addProperty(TYPE, START_DS_ACTION); } } actionObj.add(TARGET_FIELD_NAME, baseObj); } dataSourceObj.add(SOURCE_FIELD_NAME, scheduleObj); if (filtersEl != null) dataSourceObj.add(FILTER, filtersEl); if (actionObj != null) dataSourceObj.add(ACTION, actionObj); if (triggerObj != null) dataSourceObj.add(TRIGGER, triggerObj); return dataSourceObj; }
From source file:edu.mit.media.funf.config.ConfigRewriteUtil.java
License:Open Source License
/** * Rewrites the filter array denoted by "@filters" to a "filters" member * with nested filters, in the order of their appearance in the array. * // w w w . j a va 2 s . c o m * If the baseObj is not a CompositeDataSource, converts it into one, and * pushing the "@probe" annotation (if it exists) to the "source" field. * * If the entire filter class name is not specified, it will be prefixed by * FILTER_PREFIX. * * eg. * { "@probe": ".ActivityProbe", * "sensorDelay": "FASTEST", * "@filters": [{ "@type": ".KeyValueFilter", "matches": { "motionState": "Driving" } }, * { "@type": ".ProbabilityFilter", "probability": 0.5 } ] * } * * will be rewritten to * * { "@type": "edu.mit.media.funf.datasource.CompositeDataSource", * "source": { "@probe": ".ActivityProbe", "sensorDelay": "FASTEST" }, * "filters": { "@type": "edu.mit.media.funf.filter.KeyValueFilter", * "matches": { "motionState": "Driving" } * "listener": { "@type": "edu.mit.media.funf.filter.ProbabilityFilter", * "probability": 0.5 } } * } * * @param baseObj */ public static JsonObject rewriteFiltersAnnotation(JsonObject baseObj) { if (baseObj == null) return null; JsonElement filterEl = baseObj.remove(FILTER); JsonObject filterObj = null; if (filterEl.isJsonArray()) { for (JsonElement filterIter : filterEl.getAsJsonArray()) { if (filterIter.isJsonObject()) { // Add the filter class name prefix if not specified. addTypePrefix(filterIter.getAsJsonObject(), FILTER_PREFIX); } } filterObj = new JsonObject(); filterObj.addProperty(TYPE, COMPOSITE_FILTER); filterObj.add("filters", filterEl.getAsJsonArray()); } else { filterObj = filterEl.getAsJsonObject(); } // Add the filter class name prefix if not specified. addTypePrefix(filterObj.getAsJsonObject(), FILTER_PREFIX); // Insert the filter object denoted by "@filter" to the existing // "filter" field. insertFilter(baseObj, filterObj); // If the baseObj is not a data source, convert it into CompositeDataSource. if (!isDataSourceObject(baseObj)) { JsonObject dataSourceObj = new JsonObject(); dataSourceObj.addProperty(TYPE, COMPOSITE_DS); dataSourceObj.add(FILTER_FIELD_NAME, baseObj.remove(FILTER_FIELD_NAME)); if (baseObj.has(ACTION)) { dataSourceObj.add(ACTION, baseObj.remove(ACTION)); } // The remaining fields of baseObj should be "@probe" annotation and // probe parameters. dataSourceObj.add(SOURCE_FIELD_NAME, baseObj); return dataSourceObj; } else { return baseObj; } }
From source file:edu.mit.media.funf.config.ConfigRewriteUtil.java
License:Open Source License
/** * Rewrites "@action" annotation as an Action object, and adds it to the * end of the "filter" chain.// w w w . j av a 2 s.c om * * If the specified Action does not implement the DataListener interface, * it is wrapped by an ActionAdapter. * * If the entire action class name is not specified, it will be prefixed by * ACTION_PREFIX. * * @param baseObj */ public static JsonObject rewriteActionAnnotation(JsonObject baseObj) { if (baseObj == null) return null; // Add prefix if entire class name is not specified. JsonObject actionObj = baseObj.remove(ACTION).getAsJsonObject(); addTypePrefix(actionObj, ACTION_PREFIX); // Check if the specified Action type implements the DataListener // interface. String actionType = actionObj.get(TYPE).getAsString(); boolean isDataListener = false; try { Class<?> runtimeClass = Class.forName(actionType); for (Class<?> runtimeInterface : runtimeClass.getInterfaces()) { if (DATA_LISTENER.equals(runtimeInterface.getName())) { isDataListener = true; break; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } // Wrap the Action with an ActionAdapter if it does not // implement the DataListener interface and insert the // Action to the end of the "filter" chain. if (!isDataListener) { JsonObject actionAdapter = new JsonObject(); actionAdapter.addProperty(TYPE, ACTION_ADAPTER); actionAdapter.add(TARGET_FIELD_NAME, actionObj); insertFilter(baseObj, actionAdapter); } else { insertFilter(baseObj, actionObj); } // If the baseObj is not a data source, convert it into CompositeDataSource. if (!isDataSourceObject(baseObj)) { JsonObject dataSourceObj = new JsonObject(); dataSourceObj.addProperty(TYPE, COMPOSITE_DS); dataSourceObj.add(FILTER_FIELD_NAME, baseObj.remove(FILTER_FIELD_NAME)); // The remaining fields of baseObj should be "@probe" annotation and // probe parameters. dataSourceObj.add(SOURCE_FIELD_NAME, baseObj); return dataSourceObj; } else { return baseObj; } }