Example usage for com.google.gson JsonArray remove

List of usage examples for com.google.gson JsonArray remove

Introduction

In this page you can find the example usage for com.google.gson JsonArray remove.

Prototype

public JsonElement remove(int index) 

Source Link

Document

Removes the element at the specified position in this array.

Usage

From source file:eu.seaclouds.platform.dashboard.servlets.ListApplicationsServlet.java

License:Apache License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    List<ApplicationSummary> applicationSummaries = BROOKLYN_API.getApplicationApi().list();

    Collections.sort(applicationSummaries, new Comparator<ApplicationSummary>() {
        @Override/*  w  w  w .  j  av a2 s  . co m*/
        public int compare(ApplicationSummary s1, ApplicationSummary s2) {
            return s1.getId().compareTo(s2.getId());
        }
    });

    JsonArray jsonResult = new JsonArray();

    for (ApplicationSummary application : applicationSummaries) {

        JsonObject jsonApplication = new JsonObject();
        jsonResult.add(jsonApplication);

        jsonApplication.addProperty("id", application.getId());
        jsonApplication.addProperty("status", application.getStatus().toString());

        JsonObject jsonSpec = new JsonObject();
        jsonApplication.add("spec", jsonSpec);

        jsonSpec.addProperty("name", application.getSpec().getName());
        jsonSpec.addProperty("type", application.getSpec().getName());

        JsonArray jsonDescendantsEntities = new JsonArray();
        jsonApplication.add("descendants", jsonDescendantsEntities);

        List<EntitySummary> descendants;
        try {
            descendants = BROOKLYN_API.getEntityApi().list(application.getId());

            if (descendants != null) {
                for (EntitySummary childEntity : descendants) {
                    JsonObject jsonDescendantEntity = new JsonObject();
                    jsonDescendantsEntities.add(jsonDescendantEntity);

                    jsonDescendantEntity.addProperty("id", childEntity.getId());
                    jsonDescendantEntity.addProperty("name", childEntity.getName());
                    jsonDescendantEntity.addProperty("type", childEntity.getType());

                    JsonArray jsonArrayLocations = new JsonArray();
                    jsonDescendantEntity.add("locations", jsonArrayLocations);

                    List<LocationSummary> locations = BROOKLYN_API.getEntityApi()
                            .getLocations(application.getId(), childEntity.getId());
                    if (locations != null) {

                        for (LocationSummary locationSummary : locations) {
                            LocationSummary locationDetails = BROOKLYN_API.getLocationApi()
                                    .get(locationSummary.getId(), null);

                            if (locationDetails != null) {
                                JsonObject jsonLocation = new JsonObject();
                                jsonArrayLocations.add(jsonLocation);
                                jsonLocation.addProperty("id", locationDetails.getId());
                                jsonLocation.addProperty("name", locationDetails.getName());
                                jsonLocation.addProperty("type", locationDetails.getType());
                                jsonLocation.addProperty("spec", locationDetails.getSpec());
                            }
                        }
                    }
                }
            }
        } catch (ClientResponseFailure e) {
            // The application removed after calling getApplicationApi().list()
            jsonResult.remove(jsonApplication);
        }
    }

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(jsonResult));
}

From source file:it.unibo.arces.wot.sepa.commons.sparql.BindingsResults.java

License:Open Source License

/**
 * Removes the./*www. ja  va 2 s . c om*/
 *
 * @param solution
 *            the solution
 */
public void remove(Bindings solution) {
    if (solution == null)
        return;
    JsonArray bindings = getBindingsArray();
    if (bindings == null)
        return;

    bindings.remove(solution.toJson());
}

From source file:org.ballerinalang.langserver.extensions.ballerina.document.BallerinaDocumentServiceImpl.java

License:Open Source License

/**
 * Util method to merge annotation attachments.
 *
 * @param targetNode target node/*from ww  w .  j a v  a  2  s. co m*/
 * @param sourceNode source node which will get merged to target node
 */
private void mergeAnnotations(JsonObject targetNode, JsonObject sourceNode, JsonObject tree) {
    JsonArray annotationAttachments = sourceNode.has("annotationAttachments")
            ? sourceNode.getAsJsonArray("annotationAttachments")
            : sourceNode.getAsJsonArray("annAttachments");
    for (JsonElement item : annotationAttachments) {
        JsonObject sourceNodeAttachment = item.getAsJsonObject();

        JsonObject matchedTargetNode = findAttachmentNode(targetNode, sourceNodeAttachment);

        if (matchedTargetNode != null) {
            if (sourceNodeAttachment.getAsJsonObject("expression").get("kind").getAsString()
                    .equals("RecordLiteralExpr")
                    && matchedTargetNode.getAsJsonObject("expression").get("kind").getAsString()
                            .equals("RecordLiteralExpr")) {

                JsonObject sourceRecord = sourceNodeAttachment.getAsJsonObject("expression");
                JsonObject matchedTargetRecord = matchedTargetNode.getAsJsonObject("expression");

                if (sourceNodeAttachment.getAsJsonObject("annotationName").get("value").getAsString()
                        .equals("MultiResourceInfo")) {
                    JsonArray sourceResourceInformations = sourceRecord.getAsJsonArray("keyValuePairs").get(0)
                            .getAsJsonObject().getAsJsonObject("value").getAsJsonArray("keyValuePairs");
                    JsonArray targetResourceInformations = matchedTargetRecord.getAsJsonArray("keyValuePairs")
                            .get(0).getAsJsonObject().getAsJsonObject("value").getAsJsonArray("keyValuePairs");

                    // Get map values of the resourceInformation map in MultiResourceInfo annotation.
                    for (JsonElement sourceResourceInfoItem : sourceResourceInformations) {
                        JsonObject sourceResourceInfo = sourceResourceInfoItem.getAsJsonObject();
                        JsonObject matchedTargetResourceInfo = null;
                        for (JsonElement targetResourceInfoItem : targetResourceInformations) {
                            JsonObject targetResourceInfo = targetResourceInfoItem.getAsJsonObject();
                            if (targetResourceInfo.has("key") && targetResourceInfo.getAsJsonObject("key")
                                    .get("kind").getAsString().equals("Literal")) {
                                JsonObject targetResourceInfoKey = targetResourceInfo.getAsJsonObject("key");
                                JsonObject sourceResourceInfoKey = sourceResourceInfo.getAsJsonObject("key");

                                if (sourceResourceInfoKey.get("value").getAsString()
                                        .equals(targetResourceInfoKey.get("value").getAsString())) {
                                    matchedTargetResourceInfo = targetResourceInfo;
                                }
                            }
                        }

                        if (matchedTargetResourceInfo != null) {
                            JsonArray sourceResourceInfoOperation = sourceResourceInfo.getAsJsonObject("value")
                                    .getAsJsonArray("keyValuePairs");
                            JsonArray targetResourceInfoOperation = matchedTargetResourceInfo
                                    .getAsJsonObject("value").getAsJsonArray("keyValuePairs");

                            for (JsonElement keyValueItem : sourceResourceInfoOperation) {
                                JsonObject sourceKeyValue = keyValueItem.getAsJsonObject();
                                int matchedKeyValuePairIndex = 0;
                                JsonObject matchedObj = null;
                                for (JsonElement matchedKeyValueItem : targetResourceInfoOperation) {
                                    JsonObject matchedKeyValue = matchedKeyValueItem.getAsJsonObject();
                                    if ((matchedKeyValue.has("key") && matchedKeyValue.getAsJsonObject("key")
                                            .get("kind").getAsString().equals("SimpleVariableRef"))) {
                                        JsonObject matchedKey = matchedKeyValue.getAsJsonObject("key");
                                        JsonObject sourceKey = sourceKeyValue.getAsJsonObject("key");
                                        if (matchedKey.getAsJsonObject("variableName").get("value")
                                                .getAsString().equals(sourceKey.getAsJsonObject("variableName")
                                                        .get("value").getAsString())) {
                                            matchedObj = matchedKeyValue;
                                            break;
                                        }
                                    }
                                    matchedKeyValuePairIndex++;
                                }

                                if (matchedObj != null) {
                                    List<JsonObject> matchedObjWS = FormattingSourceGen.extractWS(matchedObj);
                                    int firstTokenIndex = matchedObjWS.get(0).get("i").getAsInt();
                                    targetResourceInfoOperation.remove(matchedKeyValuePairIndex);
                                    FormattingSourceGen.reconcileWS(sourceKeyValue, targetResourceInfoOperation,
                                            tree, firstTokenIndex);
                                    targetResourceInfoOperation.add(sourceKeyValue);
                                } else {
                                    // Add new key value pair to the annotation record.
                                    FormattingSourceGen.reconcileWS(sourceKeyValue, targetResourceInfoOperation,
                                            tree, -1);
                                    targetResourceInfoOperation.add(sourceKeyValue);

                                    if (targetResourceInfoOperation.size() > 1) {
                                        // Add a new comma to separate the new key value pair.
                                        int startIndex = FormattingSourceGen.extractWS(sourceKeyValue).get(0)
                                                .getAsJsonObject().get("i").getAsInt();
                                        FormattingSourceGen.addNewWS(
                                                matchedTargetResourceInfo.getAsJsonObject("value"), tree, "",
                                                ",", true, startIndex);
                                    }
                                }
                            }

                        } else {
                            FormattingSourceGen.reconcileWS(sourceResourceInfo, targetResourceInformations,
                                    tree, -1);
                            targetResourceInformations.add(sourceResourceInfo);
                        }
                    }

                } else {
                    for (JsonElement keyValueItem : sourceRecord.getAsJsonArray("keyValuePairs")) {
                        JsonObject sourceKeyValue = keyValueItem.getAsJsonObject();
                        int matchedKeyValuePairIndex = 0;
                        JsonObject matchedObj = null;

                        for (JsonElement matchedKeyValueItem : matchedTargetRecord
                                .getAsJsonArray("keyValuePairs")) {
                            JsonObject matchedKeyValue = matchedKeyValueItem.getAsJsonObject();
                            if ((matchedKeyValue.has("key") && matchedKeyValue.getAsJsonObject("key")
                                    .get("kind").getAsString().equals("SimpleVariableRef"))) {
                                JsonObject matchedKey = matchedKeyValue.getAsJsonObject("key");
                                JsonObject sourceKey = sourceKeyValue.getAsJsonObject("key");
                                if (matchedKey.getAsJsonObject("variableName").get("value").getAsString()
                                        .equals(sourceKey.getAsJsonObject("variableName").get("value")
                                                .getAsString())) {
                                    matchedObj = matchedKeyValue;
                                    break;
                                }
                            }
                            matchedKeyValuePairIndex++;
                        }

                        if (matchedObj != null) {
                            List<JsonObject> matchedObjWS = FormattingSourceGen.extractWS(matchedObj);
                            int firstTokenIndex = matchedObjWS.get(0).get("i").getAsInt();
                            matchedTargetRecord.getAsJsonArray("keyValuePairs")
                                    .remove(matchedKeyValuePairIndex);
                            FormattingSourceGen.reconcileWS(sourceKeyValue,
                                    matchedTargetRecord.getAsJsonArray("keyValuePairs"), tree, firstTokenIndex);
                            matchedTargetRecord.getAsJsonArray("keyValuePairs").add(sourceKeyValue);
                        } else {
                            // Add the new record key value pair.
                            FormattingSourceGen.reconcileWS(sourceKeyValue,
                                    matchedTargetRecord.getAsJsonArray("keyValuePairs"), tree, -1);
                            matchedTargetRecord.getAsJsonArray("keyValuePairs").add(sourceKeyValue);

                            if (matchedTargetRecord.getAsJsonArray("keyValuePairs").size() > 1) {
                                // Add a new comma to separate the new key value pair.
                                int startIndex = FormattingSourceGen.extractWS(sourceKeyValue).get(0)
                                        .getAsJsonObject().get("i").getAsInt();
                                FormattingSourceGen.addNewWS(matchedTargetRecord, tree, "", ",", true,
                                        startIndex);
                            }
                        }
                    }
                }
            }
        } else {
            int startIndex = FormattingSourceGen.getStartPosition(targetNode, "annAttachments", -1);
            JsonArray targetAnnAttachments = targetNode.has("annotationAttachments")
                    ? targetNode.getAsJsonArray("annotationAttachments")
                    : targetNode.getAsJsonArray("annAttachments");
            FormattingSourceGen.reconcileWS(sourceNodeAttachment, targetAnnAttachments, tree, startIndex);
            targetAnnAttachments.add(sourceNodeAttachment);
        }

    }
}

From source file:org.ballerinalang.langserver.formatting.FormattingSourceGen.java

License:Open Source License

/**
 * process the given json before source generation.
 *
 * @param json       Current JSON AST node
 * @param parentKind Parent kind/*from   w  ww .j a v a2s  .c  o m*/
 * @return {@link JsonObject} Processed AST node
 */
public static JsonObject build(JsonObject json, String parentKind) {
    String kind = json.get("kind").getAsString();
    for (Map.Entry<String, JsonElement> child : json.entrySet()) {
        if (!child.getKey().equals("position") && !child.getKey().equals("ws")) {
            if (child.getValue().isJsonObject() && child.getValue().getAsJsonObject().get("kind") != null) {
                json.add(child.getKey(), build(child.getValue().getAsJsonObject(), kind));
            } else if (child.getValue().isJsonArray()) {
                JsonArray childArray = child.getValue().getAsJsonArray();
                for (int j = 0; j < childArray.size(); j++) {
                    JsonElement childItem = childArray.get(j);
                    if ("CompilationUnit".equals(kind)
                            && childItem.getAsJsonObject().get("kind").getAsString().equals("Function")
                            && childItem.getAsJsonObject().has("lambda")
                            && childItem.getAsJsonObject().get("lambda").getAsBoolean()) {
                        childArray.remove(j);
                        j--;
                    } else if (childItem.isJsonObject() && childItem.getAsJsonObject().get("kind") != null) {
                        build(childItem.getAsJsonObject(), kind);
                    }
                }
            }
        }
    }
    modifyNode(json, parentKind);
    return json;
}

From source file:org.ballerinalang.langserver.formatting.FormattingSourceGen.java

License:Open Source License

private static void literalWSAssignForTemplates(int currentWs, int nextWs, JsonArray literals, JsonArray ws,
        int wsStartLocation) {
    if (literals.size() == (ws.size() - wsStartLocation)) {
        for (int i = 0; i < literals.size(); i++) {
            if (literals.get(i).getAsJsonObject().get("kind").getAsString().equals("Literal")) {
                if (!literals.get(i).getAsJsonObject().has("ws")) {
                    literals.get(i).getAsJsonObject().add("ws", new JsonArray());
                }//  w  w  w. ja v a2 s  . com

                stringTemplateSourceFromWS(currentWs, nextWs, literals, ws, i);

                if (i == (literals.size() - 1)) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    literals.get(i).getAsJsonObject().addProperty("value",
                            ws.get(currentWs).getAsJsonObject().get("text").getAsString());
                    literals.get(i).getAsJsonObject().addProperty("lastNodeValue", true);
                    // TODO: use splice.
                    ws.remove(currentWs);
                }
            }
        }
    } else if ((literals.size() - 1) == (ws.size() - wsStartLocation)) {
        for (int i = 0; i < literals.size(); i++) {
            if (literals.get(i).getAsJsonObject().get("kind").getAsString().equals("Literal")) {
                if (!literals.get(i).getAsJsonObject().has("ws")) {
                    literals.get(i).getAsJsonObject().add("ws", new JsonArray());
                }

                stringTemplateSourceFromWS(currentWs, nextWs, literals, ws, i);
            }
        }
    }
}

From source file:org.ballerinalang.langserver.formatting.FormattingSourceGen.java

License:Open Source License

private static void stringTemplateSourceFromWS(int currentWs, int nextWs, JsonArray literals, JsonArray ws,
        int i) {//from  w ww.jav a  2s  .  c o  m
    if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("${")) {
        literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
        literals.get(i).getAsJsonObject().addProperty("value",
                ws.get(currentWs).getAsJsonObject().get("text").getAsString());
        // TODO: use splice
        ws.remove(currentWs);
        literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);

    } else if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("}")) {
        literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
        if (ws.get(nextWs).getAsJsonObject().get("text").getAsString().contains("${")) {
            literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(nextWs));
            literals.get(i).getAsJsonObject().addProperty("value",
                    ws.get(nextWs).getAsJsonObject().get("text").getAsString());
            literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);
            // TODO: use splice
            ws.remove(nextWs);
        }
        // TODO: use splice
        ws.remove(currentWs);
        literals.get(i).getAsJsonObject().addProperty("endTemplateLiteral", true);
    }
}

From source file:org.ballerinalang.langserver.SourceGen.java

License:Open Source License

@FindbugsSuppressWarnings
void literalWSAssignForTemplates(int currentWs, int nextWs, JsonArray literals, JsonArray ws,
        int wsStartLocation) {
    if (literals.size() == (ws.size() - wsStartLocation)) {
        for (int i = 0; i < literals.size(); i++) {
            if (literals.get(i).getAsJsonObject().get("kind").getAsString().equals("Literal")) {
                if (!literals.get(i).getAsJsonObject().has("ws")) {
                    literals.get(i).getAsJsonObject().add("ws", new JsonArray());
                }//  w ww . j  a v  a 2s . c o m

                if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("{{")) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    literals.get(i).getAsJsonObject().addProperty("value",
                            ws.get(currentWs).getAsJsonObject().get("text").getAsString());
                    // TODO: use splice
                    ws.remove(currentWs);
                    literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);

                } else if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("}}")) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    if (ws.get(nextWs).getAsJsonObject().get("text").getAsString().contains("{{")) {
                        literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(nextWs));
                        literals.get(i).getAsJsonObject().addProperty("value",
                                ws.get(nextWs).getAsJsonObject().get("text").getAsString());
                        literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);
                        // TODO: use splice
                        ws.remove(nextWs);
                    }
                    // TODO: use splice
                    ws.remove(currentWs);
                    literals.get(i).getAsJsonObject().addProperty("endTemplateLiteral", true);
                }

                if (i == (literals.size() - 1)) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    literals.get(i).getAsJsonObject().addProperty("value",
                            ws.get(currentWs).getAsJsonObject().get("text").getAsString());
                    literals.get(i).getAsJsonObject().addProperty("lastNodeValue", true);
                    // TODO: use splice.
                    ws.remove(currentWs);
                }
            }
        }
    } else if ((literals.size() - 1) == (ws.size() - wsStartLocation)) {
        for (int i = 0; i < literals.size(); i++) {
            if (literals.get(i).getAsJsonObject().get("kind").getAsString().equals("Literal")) {
                if (!literals.get(i).getAsJsonObject().has("ws")) {
                    literals.get(i).getAsJsonObject().add("ws", new JsonArray());
                }

                if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("{{")) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    literals.get(i).getAsJsonObject().addProperty("value",
                            ws.get(currentWs).getAsJsonObject().get("text").getAsString());
                    //TODO: use splice.
                    ws.remove(currentWs);
                    literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);
                } else if (ws.get(currentWs).getAsJsonObject().get("text").getAsString().contains("}}")) {
                    literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(currentWs));
                    if (ws.get(nextWs).getAsJsonObject().get("text").getAsString().contains("{{")) {
                        literals.get(i).getAsJsonObject().get("ws").getAsJsonArray().add(ws.get(nextWs));
                        literals.get(i).getAsJsonObject().addProperty("value",
                                ws.get(nextWs).getAsJsonObject().get("text").getAsString());
                        literals.get(i).getAsJsonObject().addProperty("startTemplateLiteral", true);
                        //TODO: use splice.
                        ws.remove(nextWs);
                    }
                    //TODO: use splice.
                    ws.remove(currentWs);
                    literals.get(i).getAsJsonObject().addProperty("endTemplateLiteral", true);
                }
            }
        }
    }
}

From source file:org.ballerinalang.langserver.SourceGen.java

License:Open Source License

@FindbugsSuppressWarnings
public JsonObject build(JsonObject json, JsonObject parent, String parentKind) {
    String kind = json.get("kind").getAsString();
    for (Map.Entry<String, JsonElement> child : json.entrySet()) {
        if (!child.getKey().equals("position") && !child.getKey().equals("ws")) {
            if (child.getValue().isJsonObject() && child.getValue().getAsJsonObject().get("kind") != null) {
                json.add(child.getKey(), build(child.getValue().getAsJsonObject(), json, kind));
            } else if (child.getValue().isJsonArray()) {
                JsonArray childArray = child.getValue().getAsJsonArray();
                for (int j = 0; j < childArray.size(); j++) {
                    JsonElement childItem = childArray.get(j);
                    if (kind.equals("CompilationUnit")
                            && childItem.getAsJsonObject().get("kind").getAsString().equals("Function")
                            && childItem.getAsJsonObject().has("lambda")
                            && childItem.getAsJsonObject().get("lambda").getAsBoolean()) {
                        childArray.remove(j);
                        j--;//w w w.  ja  va2s  .  c  om
                    } else if (childItem.isJsonObject() && childItem.getAsJsonObject().get("kind") != null) {
                        childItem = build(childItem.getAsJsonObject(), json, kind);
                    }
                }
            }
        }
    }
    modifyNode(json, parentKind);
    json.add("parent", parent);
    return json;
}

From source file:org.couchbase.mock.subdoc.Executor.java

License:Apache License

private void insertInJsonArray(JsonArray array, int index) {
    // Because JsonArray doesn't implement Collection or List, we need
    // to use a temporary list, and then reassemble the contents into
    // an array/*from  w  w w. j  ava2s.com*/
    List<JsonElement> elements = new ArrayList<JsonElement>();
    while (array.size() > 0) {
        elements.add(array.remove(0));
    }

    List<JsonElement> newElements = new ArrayList<JsonElement>();
    if (isMultiValue) {
        for (JsonElement elem : value.getAsJsonArray()) {
            newElements.add(elem);
        }
    } else {
        newElements.add(value);
    }

    elements.addAll(index, newElements);
    for (JsonElement elem : elements) {
        array.add(elem);
    }
}

From source file:org.couchbase.mock.subdoc.Executor.java

License:Apache License

private Result remove() throws SubdocException {
    if (!match.isFound()) {
        throw new PathNotFoundException();
    } else if (path.size() == 0) {
        throw new CannotInsertException("Cannot delete root element!");
    }/*w w w . j a va 2  s. c  om*/

    JsonElement parent = match.getImmediateParent();
    JsonElement removedElement;
    Component lastComp = path.getLast();

    if (parent.isJsonObject()) {
        removedElement = parent.getAsJsonObject().remove(lastComp.getString());
    } else {
        JsonArray array = parent.getAsJsonArray();
        int index = lastComp.getIndex();
        if (index == -1) {
            index = array.size() - 1;
        }
        removedElement = array.remove(index);
    }

    return new Result(removedElement, match.getRoot());
}