List of usage examples for com.google.gson JsonObject remove
public JsonElement remove(String property)
From source file:it.intecs.pisa.util.json.DomSerializer.java
License:Open Source License
private void addElement(Element el, JsonObject jsonObj) { String localName;/* w ww. j a v a2 s .c om*/ JsonArray array; localName = el.getLocalName(); if (jsonObj.has(localName)) { JsonElement existingObj = jsonObj.get(localName); if (existingObj instanceof JsonObject) { jsonObj.remove(localName); array = new JsonArray(); jsonObj.add(localName, array); array.add(existingObj); } else array = (JsonArray) existingObj; } }
From source file:it.noovle.dataflow.TwitterProcessor.java
License:Open Source License
/** * Does some processing on the JSON message describing the tweet *//*w w w. j av a2 s .co m*/ private static JsonElement cleanup(JsonElement el) { SimpleDateFormat dateReader = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy"); SimpleDateFormat dateWriter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (el.isJsonNull()) { return el; } else if (el.isJsonObject()) { Set<String> toBeRemoved = Sets.newHashSet(); JsonObject objEl = el.getAsJsonObject(); for (Entry<String, JsonElement> child : objEl.entrySet()) { if (child.getValue().isJsonNull()) { toBeRemoved.add(child.getKey()); } else if (child.getKey().equalsIgnoreCase("created_at")) { // Parse dates like 'Tue Oct 18 07:01:50 +0000 2016' and write them out as "2016-10-18 07:01:50" try { Date date = dateReader.parse(child.getValue().getAsString()); objEl.addProperty(child.getKey(), dateWriter.format(date)); } catch (ParseException e) { LOG.warn("Error while parsing date", e); } } else if (child.getKey().equalsIgnoreCase("coordinates") && child.getValue().isJsonArray()) { objEl.add(child.getKey(), flatten(child.getValue())); } else if (child.getKey().equalsIgnoreCase("attributes")) { objEl.addProperty(child.getKey(), child.getValue().toString()); } else if (child.getValue().isJsonObject()) { objEl.add(child.getKey(), cleanup(child.getValue())); } else if (child.getValue().isJsonArray()) { JsonArray newArray = new JsonArray(); for (JsonElement arrayEl : child.getValue().getAsJsonArray()) { newArray.add(cleanup(arrayEl)); } objEl.add(child.getKey(), newArray); } } // Remove ignored fields // TODO: modify schema to include also these fields for (String key : IGNORED_FIELDS) { if (objEl.has(key)) { objEl.remove(key); } } // Remove fields marked for removal // TODO: modify schema to include also these fields for (String key : toBeRemoved) { objEl.remove(key); } return objEl; } else { return el; } }
From source file:json.JsonCommonValidation.java
/** * Method to sort JSON Array//from w w w.j a v a 2s .c om * @param o JSON Object to be sorted * @return sorted JSON Object */ public static JsonObject sortJsonArray(JsonObject o) { ArrayList<String> arrErr = new ArrayList<>(); JsonArray needSort = (JsonArray) o.get("message"); JsonArray sortedArray = new JsonArray(); for (JsonElement s : needSort) { arrErr.add(s.getAsString()); } Collections.sort(arrErr, new StringComparator()); for (String s : arrErr) { sortedArray.add(new JsonPrimitive(s)); } o.remove("message"); o.add("message", sortedArray); return o; }
From source file:json.JsonCommonValidation.java
/** * Method to sort JSON Bid Array//from w w w . j a v a 2 s. co m * @param o JSON object to be sorted * @return sorted JSON object */ public static JsonObject sortJsonBidArray(JsonObject o) { ArrayList<String> arrErr = new ArrayList<>(); JsonArray needSort = (JsonArray) o.get("bid"); JsonArray sortedArray = new JsonArray(); for (JsonElement s : needSort) { arrErr.add(s.getAsString()); } Collections.sort(arrErr); for (String s : arrErr) { sortedArray.add(new JsonPrimitive(s)); } o.remove("bid"); o.add("bid", sortedArray); return o; }
From source file:net.caseif.flint.common.arena.CommonArena.java
License:Open Source License
/** * Removes this arena from persistent storage. * * @throws IOException If an exception occurs while writing to the * persistent store// ww w. ja v a2s . com */ public void removeFromStore() throws IOException { File arenaStore = CommonDataFiles.ARENA_STORE.getFile(getMinigame()); if (!arenaStore.exists()) { throw new IllegalStateException("Arena store does not exist!"); } JsonObject json = new JsonObject(); try (FileReader reader = new FileReader(arenaStore)) { JsonElement el = new JsonParser().parse(reader); if (el.isJsonObject()) { json = el.getAsJsonObject(); } else { CommonCore.logWarning("Root element of arena store is not object - overwriting"); Files.delete(arenaStore.toPath()); } } json.remove(this.getId()); try (FileWriter writer = new FileWriter(arenaStore)) { writer.write(json.toString()); } }
From source file:net.caseif.flint.common.lobby.CommonLobbySign.java
License:Open Source License
/** * Stores this {@link LobbySign} to persistent storage. *//* w ww .j a v a 2 s . co m*/ private void store(boolean remove) { try { File store = CommonDataFiles.LOBBY_STORE.getFile(getArena().getMinigame()); JsonObject json = JsonHelper.readOrCreateJson(store); JsonObject arena = json.getAsJsonObject(getArena().getId()); if (arena == null) { if (!remove) { // okay to create it since we're newly storing the sign arena = new JsonObject(); json.add(getArena().getId(), arena); } else { // can't delete something that's not there CommonCore .logWarning("Anomaly: Engine requested removal of lobby sign from store, but arena was " + "not defined"); return; } } String locSerial = getLocation().serialize(); if (remove) { if (arena.has(locSerial)) { arena.remove(locSerial); } else { CommonCore .logWarning("Engine requested removal of lobby sign from store, but respective section " + "was not defined"); } } else { JsonObject sign = new JsonObject(); arena.add(locSerial, sign); String type; if (this instanceof StatusLobbySign) { type = PERSIST_TYPE_STATUS; } else if (this instanceof ChallengerListingLobbySign) { type = PERSIST_TYPE_LISTING; } else { throw new AssertionError("Invalid LobbySign object. Report this immediately."); } sign.addProperty(PERSIST_TYPE_KEY, type); if (this instanceof ChallengerListingLobbySign) { sign.addProperty(PERSIST_INDEX_KEY, ((ChallengerListingLobbySign) this).getIndex()); } } try (FileWriter writer = new FileWriter(store)) { writer.write(json.toString()); } } catch (IOException ex) { CommonCore.logSevere("Failed to write to lobby sign store"); ex.printStackTrace(); } }
From source file:net.caseif.flint.common.minigame.CommonMinigame.java
License:Open Source License
public void loadLobbySigns() { try {/*from w w w . ja v a2 s . c o m*/ File store = CommonDataFiles.LOBBY_STORE.getFile(this); Optional<JsonObject> jsonOpt = JsonHelper.readJson(store); if (!jsonOpt.isPresent()) { return; } JsonObject json = jsonOpt.get(); for (Map.Entry<String, JsonElement> entry : json.entrySet()) { if (json.get(entry.getKey()).isJsonObject()) { Optional<Arena> arena = getArena(entry.getKey()); if (arena.isPresent()) { JsonObject arenaJson = json.getAsJsonObject(entry.getKey()); List<String> toRemove = new ArrayList<>(); for (Map.Entry<String, JsonElement> arenaEntry : arenaJson.entrySet()) { if (arenaJson.get(arenaEntry.getKey()).isJsonObject()) { try { Location3D loc = Location3D.deserialize(arenaEntry.getKey()); switch (checkPhysicalLobbySign(loc)) { case 0: break; case 1: continue; case 2: toRemove.add(arenaEntry.getKey()); continue; default: // wtf throw new AssertionError("The platform implementation did something " + "super-wrong. Report this immediately."); } try { LobbySign sign = ((ILobbySignFactory) FactoryRegistry .getFactory(LobbySign.class)).createLobbySign(loc, arena.get(), arenaJson.getAsJsonObject(arenaEntry.getKey())); ((CommonArena) arena.get()).getLobbySignMap().put(loc, sign); } catch (IllegalArgumentException ex) { CommonCore.logWarning("Found lobby sign in store with invalid " + "configuration. Removing..."); json.remove(arenaEntry.getKey()); } } catch (IllegalArgumentException ignored) { CommonCore.logWarning("Found lobby sign in store with invalid location serial. " + "Removing..."); } } } for (String key : toRemove) { arenaJson.remove(key); } } else { CommonCore.logVerbose( "Found orphaned lobby sign group (arena \"" + entry.getKey() + "\") - not loading"); } } } try (FileWriter writer = new FileWriter(store)) { writer.write(json.toString()); } } catch (IOException ex) { CommonCore.logSevere("Failed to load lobby signs for minigame " + getPlugin()); ex.printStackTrace(); } }
From source file:net.caseif.flint.common.util.agent.rollback.CommonRollbackAgent.java
License:Open Source License
@Override public void clearStateStore() throws IOException { JsonObject json = new JsonParser().parse(new FileReader(stateStore)).getAsJsonObject(); if (!json.has(getArena().getId()) || !json.get(getArena().getId()).isJsonObject()) { CommonCore.logWarning("State store clear requested, but arena was not present"); return;// w w w .ja v a2 s.c om } json.remove(getArena().getId()); json.add(getArena().getId(), new JsonObject()); saveState(json); }
From source file:net.caseif.flint.common.util.helper.CommonPlayerHelper.java
License:Open Source License
/** * Gets the given player's stored location from persistent storage and pops * it if found./*from ww w . j a v a 2 s . c om*/ * * @param player The {@link UUID} of the player to load the location of * @return The stored {@link Location3D} * @throws IllegalArgumentException If an error occurs during * deserialization of the stored location * @throws IOException If an exception occurs while saving to disk */ public static Optional<Location3D> getReturnLocation(UUID player) throws IllegalArgumentException, IOException { File store = CommonDataFiles.PLAYER_LOCATION_STORE.getFile(); JsonObject json; if (store.exists()) { try (FileReader reader = new FileReader(store)) { JsonElement el = new JsonParser().parse(reader); if (!el.isJsonObject()) { return Optional.absent(); } json = el.getAsJsonObject(); } } else { return Optional.absent(); } if (json.has(player.toString())) { Location3D l3d = deserializeLocation(json.getAsJsonObject(player.toString())); json.remove(player.toString()); try (FileWriter writer = new FileWriter(store)) { writer.append(json.toString()); } if (!l3d.getWorld().isPresent()) { throw new IllegalArgumentException("World not present in stored location of player " + player); } return Optional.of(l3d); } else { return Optional.absent(); } }
From source file:net.daporkchop.toobeetooteebot.util.Config.java
License:Open Source License
public JsonElement set(@NonNull String key, @NonNull Object val) { synchronized (this.fastAccessCache) { try {//ww w. j a va 2 s. c o m JsonObject parent = null; String name = null; JsonElement element = this.object; for (String s : key.split("\\.")) { if (element.getAsJsonObject().has(name = s)) { element = (parent = element.getAsJsonObject()).get(s); } else { JsonObject object = new JsonObject(); element.getAsJsonObject().add(s, object); parent = element.getAsJsonObject(); element = object; } } parent.remove(name); if (val instanceof JsonElement) { parent.add(name, (JsonElement) val); } else if (val instanceof String) { parent.addProperty(name, (String) val); } else if (val instanceof Number) { parent.addProperty(name, (Number) val); } else if (val instanceof Boolean) { parent.addProperty(name, (Boolean) val); } else if (val instanceof Character) { parent.addProperty(name, (Character) val); } return parent.get(name); } finally { this.dirty = true; this.fastAccessCache.remove(key); } } }