Java Code Examples for com.google.gson.JsonArray#remove()

The following examples show how to use com.google.gson.JsonArray#remove() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * 清除集合中其他元素。只保留第一个
 *
 * @param val
 */
private void removeArrayEle(Object val) {
    if (val instanceof JsonArray) {
        JsonArray arr = (JsonArray) val;
        int len = arr.size();
        for (int i = 0; i < len; i++) {
            if (i > 0) {
                arr.remove(1);
            } else {
                Object v = arr.get(i);
                removeArrayEle(v);
            }
        }
    } else if (val instanceof JsonObject) {
        JsonObject jo = (JsonObject) val;
        jo.entrySet().forEach((next) -> {
            removeArrayEle(next.getValue());
        });
    }
}
 
Example 2
/**
 *
 * @param oports        output ports of an operator
 * @param iportName     the target input port name
 * @param shouldRemove  true if the target connection should be removed
 * @return the first output port that is connected to the target input port
 */
static private JsonObject findOutputPort(
        JsonArray oports, String iportName, boolean shouldRemove) {
    for (JsonElement oport: oports) {
        JsonArray outConns = oport.getAsJsonObject().get("connections").getAsJsonArray();
        for (JsonElement outConn: outConns) {
            if (outConn.getAsString().equals(iportName)) {
                if (shouldRemove) {
                    outConns.remove(outConn);
                }
                return oport.getAsJsonObject();
            }
        }
    }
    return null;
}
 
Example 3
private JsonArray unlistChannelIdsArray(Long channelId, JsonArray channels) {
    // no need unlist
    if (channels == null) {
        return null;
    }

    if (channels.contains(new JsonPrimitive(channelId))) {
        channels.remove(new JsonPrimitive(channelId));
        return channels;
    } else {
        // unlist before; nothing to do
        return null;
    }
}
 
Example 4
Source Project: java-slack-sdk   File: MergeJsonBuilder.java    License: MIT License 5 votes vote down vote up
private static void mergeJsonObjects(
        JsonObject leftObj,
        JsonObject rightObj,
        ConflictStrategy conflictStrategy) throws JsonConflictException {

    for (Map.Entry<String, JsonElement> rightEntry : rightObj.entrySet()) {
        String rightKey = rightEntry.getKey();
        JsonElement rightVal = rightEntry.getValue();
        if (leftObj.has(rightKey)) {
            //conflict
            JsonElement leftVal = leftObj.get(rightKey);
            if (leftVal.isJsonArray() && rightVal.isJsonArray()) {
                JsonArray leftArr = leftVal.getAsJsonArray();
                JsonArray rightArr = rightVal.getAsJsonArray();
                for (int i = 0; i < rightArr.size(); i++) {
                    JsonElement rightArrayElem = rightArr.get(i);
                    if (!leftArr.contains(rightArrayElem)) {
                        // remove temporarily added an empty string
                        if (rightArrayElem.isJsonObject()
                                && leftArr.size() == 1
                                && leftArr.get(i).isJsonPrimitive()
                                && leftArr.get(i).getAsString().equals("")) {
                            leftArr.remove(0);
                        }
                        leftArr.add(rightArrayElem);
                    }
                }
            } else if (leftVal.isJsonObject() && rightVal.isJsonObject()) {
                //recursive merging
                mergeJsonObjects(leftVal.getAsJsonObject(), rightVal.getAsJsonObject(), conflictStrategy);
            } else {//not both arrays or objects, normal merge with conflict resolution
                handleMergeConflict(rightKey, leftObj, leftVal, rightVal, conflictStrategy);
            }
        } else {//no conflict, add to the object
            leftObj.add(rightKey, rightVal);
        }
    }
}
 
Example 5
Source Project: java-slack-sdk   File: MergeJsonBuilder.java    License: MIT License 5 votes vote down vote up
private static void mergeJsonObjects(
        JsonObject leftObj,
        JsonObject rightObj,
        ConflictStrategy conflictStrategy) throws JsonConflictException {

    for (Map.Entry<String, JsonElement> rightEntry : rightObj.entrySet()) {
        String rightKey = rightEntry.getKey();
        JsonElement rightVal = rightEntry.getValue();
        if (leftObj.has(rightKey)) {
            //conflict
            JsonElement leftVal = leftObj.get(rightKey);
            if (leftVal.isJsonArray() && rightVal.isJsonArray()) {
                JsonArray leftArr = leftVal.getAsJsonArray();
                JsonArray rightArr = rightVal.getAsJsonArray();
                for (int i = 0; i < rightArr.size(); i++) {
                    JsonElement rightArrayElem = rightArr.get(i);
                    if (!leftArr.contains(rightArrayElem)) {
                        // remove temporarily added an empty string
                        if (rightArrayElem.isJsonObject()
                                && leftArr.size() == 1
                                && leftArr.get(i).isJsonPrimitive()
                                && leftArr.get(i).getAsString().equals("")) {
                            leftArr.remove(0);
                        }
                        leftArr.add(rightArrayElem);
                    }
                }
            } else if (leftVal.isJsonObject() && rightVal.isJsonObject()) {
                //recursive merging
                mergeJsonObjects(leftVal.getAsJsonObject(), rightVal.getAsJsonObject(), conflictStrategy);
            } else {//not both arrays or objects, normal merge with conflict resolution
                handleMergeConflict(rightKey, leftObj, leftVal, rightVal, conflictStrategy);
            }
        } else {//no conflict, add to the object
            leftObj.add(rightKey, rightVal);
        }
    }
}
 
Example 6
public boolean removeCard(MagicEdition me, MagicCard mc) throws IOException {
	File f = new File(setDirectory, me.getId() + ext);
	JsonObject root = FileTools.readJson(f).getAsJsonObject();
	JsonArray cards = root.get(CARDS).getAsJsonArray();

	for (int i = 0; i < cards.size(); i++) {
		JsonElement el = cards.get(i);
		if (el.getAsJsonObject().get("id").getAsString().equals(mc.getId())) {
			cards.remove(el);
			FileTools.saveFile(new File(setDirectory, me.getId() + ext), root.toString());
			return true;
		}
	}
	return false;
}
 
Example 7
private void clear(String property, JsonArray values) {
   for(int i=values.size(); i > 0; i--) {
      values.remove(i - 1);
   }
}