Example usage for com.google.gson JsonArray set

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

Introduction

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

Prototype

public JsonElement set(int index, JsonElement element) 

Source Link

Document

Replaces the element at the specified position in this array with the specified element.

Usage

From source file:com.keydap.sparrow.SparrowClient.java

License:Apache License

private JsonObject keysToLower(JsonObject obj) {
    JsonObject tmp = new JsonObject();
    for (String key : obj.keySet()) {
        JsonElement je = obj.get(key);//from  ww  w  . j  a v  a2 s.co m
        if (key.contains(":")) {
            JsonObject ext = keysToLower((JsonObject) je);
            tmp.add(key, ext);
        } else {
            if (je.isJsonObject()) {
                je = keysToLower((JsonObject) je);
            } else if (je.isJsonArray()) {
                JsonArray arr = je.getAsJsonArray();
                for (int i = 0; i < arr.size(); i++) {
                    JsonElement item = arr.get(i);
                    if (item.isJsonObject()) {
                        item = keysToLower((JsonObject) item);
                        arr.set(i, item);
                    }
                }
            }

            tmp.add(key.toLowerCase(), je);
        }
    }

    return tmp;
}

From source file:com.mweagle.tereus.commands.utils.EmbeddingUtils.java

License:Open Source License

protected static JsonArray parseResource(final String resourceData) throws Exception {
    JsonArray parsedContent = new JsonArray();
    Arrays.stream(resourceData.split("\\r?\\n")).forEach(eachLine -> parsedContent.addAll(parseLine(eachLine)));
    // Get the last element of parsed content.  If it's a JsonPrimitive with some non-empty
    // content then remove the final newline delimiter
    final JsonElement finalElement = parsedContent.get(parsedContent.size() - 1);
    if (finalElement instanceof JsonPrimitive) {
        JsonPrimitive finalPrimitive = (JsonPrimitive) finalElement;
        final String primitiveContent = finalPrimitive.getAsString();
        if (RE_TRAILING_NEWLINE.matcher(primitiveContent).matches()) {
            final JsonPrimitive trimmedPrimitive = new JsonPrimitive(
                    primitiveContent.substring(0, primitiveContent.length() - 1));
            parsedContent.set(parsedContent.size() - 1, trimmedPrimitive);
        }//  w w w.  j a  v a2 s  .c om

    }
    return parsedContent;
}

From source file:de.dfki.mmf.input.worldmodel.WorldModelFactory.java

License:Open Source License

private void jsonArrayToLowerCase(JsonArray array) {
    ArrayList<Map.Entry<Integer, JsonElement>> modifiedArrayEntryList = new ArrayList<>();
    for (int i = 0; i < array.size(); i++) {
        if (array.get(i).isJsonObject()) {
            jsonObjectToLowerCase(array.get(i).getAsJsonObject());
        }/*from  www .j a v a 2s. co  m*/
        if (array.get(i).isJsonArray()) {
            jsonArrayToLowerCase(array.get(i).getAsJsonArray());
        }
        if (array.get(i).isJsonPrimitive()) {
            Gson gson = new Gson();
            String entryString = array.get(i).getAsString().toLowerCase();
            String jsonString = gson.toJson(entryString);
            JsonElement element = gson.fromJson(jsonString, JsonElement.class);
            Map.Entry<Integer, JsonElement> newEntry = new AbstractMap.SimpleEntry<Integer, JsonElement>(i,
                    element);
            modifiedArrayEntryList.add(newEntry);
        }
    }
    for (Map.Entry<Integer, JsonElement> entry : modifiedArrayEntryList) {
        array.set(entry.getKey(), entry.getValue());

    }
    return;

}

From source file:de.sub.goobi.helper.HelperSchritte.java

License:Open Source License

private void replaceJsonElement(JsonElement jel, VariableReplacer replacer) {
    if (jel.isJsonObject()) {
        JsonObject obj = jel.getAsJsonObject();
        for (Entry<String, JsonElement> objEntry : obj.entrySet()) {
            if (objEntry.getValue().isJsonPrimitive()) {
                JsonPrimitive jPrim = objEntry.getValue().getAsJsonPrimitive();
                if (jPrim.isString()) {
                    String newVal = replacer.replace(jPrim.getAsString());
                    obj.addProperty(objEntry.getKey(), newVal);
                }/*from   ww w.j ava  2 s  . c o  m*/
            } else {
                replaceJsonElement(objEntry.getValue(), replacer);
            }
        }
    } else if (jel.isJsonArray()) {
        JsonArray jArr = jel.getAsJsonArray();
        for (int i = 0; i < jArr.size(); i++) {
            JsonElement innerJel = jArr.get(i);
            if (innerJel.isJsonPrimitive()) {
                JsonPrimitive jPrim = innerJel.getAsJsonPrimitive();
                if (jPrim.isString()) {
                    String newVal = replacer.replace(jPrim.getAsString());
                    jArr.set(i, new JsonPrimitive(newVal));
                }
            } else {
                replaceJsonElement(innerJel, replacer);
            }
        }
    }

}

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

License:Open Source License

/**
 * format Compilation Unit node./*ww w. j  ava  2s  . c om*/
 *
 * @param node {JsonObject} node as json object
 */
public void formatCompilationUnitNode(JsonObject node) {
    JsonArray topLevelNodes = node.get("topLevelNodes").getAsJsonArray();
    for (int i = 0; i < topLevelNodes.size(); i++) {
        JsonElement child = topLevelNodes.get(i);
        child.getAsJsonObject().get("position").getAsJsonObject().addProperty("startColumn", 1);
    }

    if (node.has("ws") && topLevelNodes.size() > 0) {
        JsonArray ws = node.get("ws").getAsJsonArray();
        this.preserveHeight(ws, null);
        if (!this.isHeightAvailable(ws.get(0).getAsJsonObject().get("ws").getAsString())) {
            ws.get(0).getAsJsonObject().addProperty("ws", NEW_LINE);
        } else if (!this.isNewLine(ws.get(0).getAsJsonObject().get("ws").getAsString()
                .charAt(ws.get(0).getAsJsonObject().get("ws").getAsString().length() - 1) + "")) {
            ws.get(0).getAsJsonObject().addProperty("ws",
                    (ws.get(0).getAsJsonObject().get("ws").getAsString() + NEW_LINE));
        }
    }

    int i, j;
    boolean swapped = false;
    for (i = 0; i < topLevelNodes.size() - 1; i++) {
        swapped = false;
        for (j = 0; j < topLevelNodes.size() - i - 1; j++) {
            if (topLevelNodes.get(j).getAsJsonObject().get("kind").getAsString().equals("Import")
                    && topLevelNodes.get(j + 1).getAsJsonObject().get("kind").getAsString().equals("Import")) {
                String refImportName = topLevelNodes.get(j).getAsJsonObject().get("orgName").getAsJsonObject()
                        .get("value").getAsString() + "/"
                        + topLevelNodes.get(j).getAsJsonObject().get("packageName").getAsJsonArray().get(0)
                                .getAsJsonObject().get("value").getAsString();

                String compImportName = topLevelNodes.get(j + 1).getAsJsonObject().get("orgName")
                        .getAsJsonObject().get("value").getAsString() + "/"
                        + topLevelNodes.get(j + 1).getAsJsonObject().get("packageName").getAsJsonArray().get(0)
                                .getAsJsonObject().get("value").getAsString();

                int comparisonResult = refImportName.compareTo(compImportName);
                // Swap if the comparison value is positive.
                if (comparisonResult > 0) {
                    // Swap ws to keep the formatting in level.
                    String refWS = topLevelNodes.get(j).getAsJsonObject().get("ws").getAsJsonArray().get(0)
                            .getAsJsonObject().get("ws").getAsString();

                    String compWS = topLevelNodes.get(j + 1).getAsJsonObject().get("ws").getAsJsonArray().get(0)
                            .getAsJsonObject().get("ws").getAsString();

                    JsonElement tempNode = topLevelNodes.get(j);
                    topLevelNodes.set(j, topLevelNodes.get(j + 1));
                    tempNode.getAsJsonObject().get("ws").getAsJsonArray().get(0).getAsJsonObject()
                            .addProperty("ws", compWS);
                    topLevelNodes.get(j).getAsJsonObject().get("ws").getAsJsonArray().get(0).getAsJsonObject()
                            .addProperty("ws", refWS);
                    topLevelNodes.set(j + 1, tempNode);

                    swapped = true;
                }
            }
        }
        // If not swapped, break.
        if (!swapped) {
            break;
        }
    }
}

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

License:Apache License

private void replace(JsonElement newValue) throws SubdocException {
    if (!match.isFound()) {
        throw new PathNotFoundException();
    } else if (path.size() == 0) {
        throw new CannotInsertException("Cannot replace root element!");
    }//from w w  w.j a  v a2  s .  com

    JsonElement parent = match.getMatchParent();
    Component comp = path.getLast();

    if (comp.isIndex()) {
        int index = comp.getIndex();
        JsonArray array = parent.getAsJsonArray();
        if (index == -1) {
            index = parent.getAsJsonArray().size() - 1;
        }
        array.set(index, value);
    } else {
        parent.getAsJsonObject().add(comp.getString(), newValue);
    }
}

From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcComposer.java

License:Open Source License

private <T> List<T> composeMany(Class<T> type, List<?> paramsList) {
    if (paramsList.isEmpty()) {
        return emptyList();
    }/*from  w  ww .j ava 2 s  .c om*/

    if (paramsList.get(0) instanceof JsonElement) {
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < paramsList.size(); i++) {
            JsonElement jsonElement = (JsonElement) paramsList.get(i);
            jsonArray.set(i, jsonElement);
        }
        return DtoFactory.getInstance().createListDtoFromJson(jsonArray.toString(), type);
    }

    return cast(paramsList);
}

From source file:org.hibernate.search.backend.elasticsearch.gson.impl.ArrayElementJsonAccessor.java

License:LGPL

@Override
protected void doSet(JsonArray parent, JsonElement newValue) {
    fillTo(parent, index);
    parent.set(index, newValue);
}

From source file:org.hibernate.search.backend.elasticsearch.impl.ElasticsearchIndexWorkVisitor.java

License:LGPL

private JsonObject getOrCreateDocumentTree(JsonObject source, String path) {
    if (path == null) {
        return source;
    }/*from   w  w w.  j a v a2s .com*/

    // embedded property Create JSON hierarchy as needed
    String[] parts = DOT.split(path);
    JsonObject parent = source;

    for (int i = 0; i < parts.length; i++) {
        Matcher nameAndIndex = NAME_AND_INDEX.matcher(parts[i]);
        nameAndIndex.matches();

        String name = nameAndIndex.group(1);
        String idx = nameAndIndex.group(3);
        Integer index = null;

        if (idx != null) {
            index = Integer.valueOf(idx);
            JsonArray array = parent.getAsJsonArray(name);
            if (array == null) {
                array = new JsonArray();
                parent.add(name, array);
            }

            JsonObject newParent = index < array.size() ? array.get(index).getAsJsonObject() : null;
            if (newParent == null) {
                newParent = new JsonObject();

                if (index >= array.size()) {
                    for (int j = array.size(); j <= index; j++) {
                        array.add(JsonNull.INSTANCE);
                    }
                }
                array.set(index, newParent);
            }

            parent = newParent;
        } else {
            JsonObject newParent = parent.getAsJsonObject(name);
            if (newParent == null) {
                newParent = new JsonObject();
                parent.add(name, newParent);
            }
            parent = newParent;
        }
    }

    return parent;
}