Example usage for com.google.gson JsonObject getAsJsonArray

List of usage examples for com.google.gson JsonObject getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonArray.

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

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

License:Open Source License

/**
 * format Constrained Type node.//from   www. ja  v  a2  s. com
 *
 * @param node {JsonObject} node as json object
 */
public void formatConstrainedTypeNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);
        String indentation = this.getIndentation(formatConfig, true);

        // Preserve line separations that already available.
        this.preserveHeight(ws, indentation);

        // Update whitespace for type node.
        if (node.has("type")) {
            node.getAsJsonObject("type").add(FormattingConstants.FORMATTING_CONFIG, formatConfig);
        }

        // Update whitespace for constraint.
        if (node.has("constraint")) {
            JsonObject constraintFormatConfig = this.getFormattingConfig(0, 0, 0, false,
                    this.getWhiteSpaceCount(indentation), false);
            node.getAsJsonObject("constraint").add(FormattingConstants.FORMATTING_CONFIG,
                    constraintFormatConfig);
        }

        // Update whitespace for open constraint symbol.
        for (JsonElement item : ws) {
            JsonObject wsItem = item.getAsJsonObject();
            if (this.noHeightAvailable(wsItem.get(FormattingConstants.WS).getAsString())) {
                String text = wsItem.get(FormattingConstants.TEXT).getAsString();
                if (text.equals("<") || text.equals(">")) {
                    wsItem.addProperty(FormattingConstants.WS, FormattingConstants.EMPTY_SPACE);
                }
            }
        }

    }
}

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

License:Open Source License

/**
 * format documentation parameter node.//  w w w .  java2s .c  o m
 *
 * @param node {JsonObject} node as json object
 */
public void formatDocumentationParameterNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);

        // Calculate indentation for the documentation parameter.
        String indentation = this.getParentIndentation(formatConfig);

        // Save new lines of a whitespace if available.
        this.preserveHeight(ws, indentation);

        // Iterate through whitespaces and update accordingly.
        for (int i = 0; i < ws.size(); i++) {
            JsonObject currentWS = ws.get(i).getAsJsonObject();
            if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) {
                String text = currentWS.get(FormattingConstants.TEXT).getAsString();
                String[] splitText = text.split(" ");

                // If text equals `#` or has `#` and `+` in order or has `#`, `+`, `return` and `-`
                // Change the whitespace for the current whitespace.
                if (text.equals("#")
                        || (splitText.length == 2 && splitText[0].equals("#") && splitText[1].equals("+"))
                        || (splitText.length == 4 && splitText[0].equals("#") && splitText[1].equals("+")
                                && splitText[2].equals("return") && splitText[3].equals("-"))) {
                    currentWS.addProperty(FormattingConstants.WS,
                            this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt())
                                    + indentation);
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * format markdown documentation node./*from  w w w .  jav  a  2 s  .  c o m*/
 *
 * @param node {JsonObject} node as json object
 */
public void formatMarkdownDocumentationNode(JsonObject node) {
    if (node.has(FormattingConstants.WS) && node.has(FormattingConstants.FORMATTING_CONFIG)) {
        JsonArray ws = node.getAsJsonArray(FormattingConstants.WS);
        JsonObject formatConfig = node.getAsJsonObject(FormattingConstants.FORMATTING_CONFIG);

        // Calculate indentation for the documentation parameter.
        String indentation = this.getParentIndentation(formatConfig);

        // Save new lines of a whitespace if available.
        this.preserveHeight(ws, indentation);

        // Iterate through whitespaces and update accordingly.
        for (int i = 0; i < ws.size(); i++) {
            JsonObject currentWS = ws.get(i).getAsJsonObject();
            if (this.noHeightAvailable(currentWS.get(FormattingConstants.WS).getAsString())) {
                String text = currentWS.get(FormattingConstants.TEXT).getAsString();
                if (text.equals("#")) {
                    currentWS.addProperty(FormattingConstants.WS,
                            this.getNewLines(formatConfig.get(FormattingConstants.NEW_LINE_COUNT).getAsInt())
                                    + indentation);
                }
            }
        }

        if (node.has("parameters") && node.getAsJsonArray("parameters").size() > 0) {
            JsonArray parameters = node.getAsJsonArray("parameters");
            for (JsonElement parameter : parameters) {
                parameter.getAsJsonObject().add(FormattingConstants.FORMATTING_CONFIG, formatConfig);
            }
        }

        if (node.has("returnParameter")) {
            JsonObject returnParameter = node.getAsJsonObject("returnParameter");
            returnParameter.getAsJsonObject().add(FormattingConstants.FORMATTING_CONFIG, formatConfig);
        }
    }
}