Example usage for com.google.gson JsonArray JsonArray

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

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:com.google.dart.server.generated.types.OverrideMember.java

License:Open Source License

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("offset", offset);
    jsonObject.addProperty("length", length);
    if (superclassMember != null) {
        jsonObject.add("superclassMember", superclassMember.toJson());
    }//from  w  w w  .  j ava 2  s . c om
    if (interfaceMembers != null) {
        JsonArray jsonArrayInterfaceMembers = new JsonArray();
        for (OverriddenMember elt : interfaceMembers) {
            jsonArrayInterfaceMembers.add(elt.toJson());
        }
        jsonObject.add("interfaceMembers", jsonArrayInterfaceMembers);
    }
    return jsonObject;
}

From source file:com.google.dart.server.generated.types.SearchResult.java

License:Open Source License

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add("location", location.toJson());
    jsonObject.addProperty("kind", kind);
    jsonObject.addProperty("isPotential", isPotential);
    JsonArray jsonArrayPath = new JsonArray();
    for (Element elt : path) {
        jsonArrayPath.add(elt.toJson());
    }//w w w .j  a va  2 s .  c o  m
    jsonObject.add("path", jsonArrayPath);
    return jsonObject;
}

From source file:com.google.dart.server.generated.types.SourceChange.java

License:Open Source License

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("message", message);
    JsonArray jsonArrayEdits = new JsonArray();
    for (SourceFileEdit elt : edits) {
        jsonArrayEdits.add(elt.toJson());
    }//ww w  .j  a v a2  s.c  o m
    jsonObject.add("edits", jsonArrayEdits);
    JsonArray jsonArrayLinkedEditGroups = new JsonArray();
    for (LinkedEditGroup elt : linkedEditGroups) {
        jsonArrayLinkedEditGroups.add(elt.toJson());
    }
    jsonObject.add("linkedEditGroups", jsonArrayLinkedEditGroups);
    if (selection != null) {
        jsonObject.add("selection", selection.toJson());
    }
    return jsonObject;
}

From source file:com.google.dart.server.generated.types.SourceFileEdit.java

License:Open Source License

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("file", file);
    jsonObject.addProperty("fileStamp", fileStamp);
    JsonArray jsonArrayEdits = new JsonArray();
    for (SourceEdit elt : edits) {
        jsonArrayEdits.add(elt.toJson());
    }//from w  w w.  j  a va  2s .  c o m
    jsonObject.add("edits", jsonArrayEdits);
    return jsonObject;
}

From source file:com.google.dart.server.generated.types.TypeHierarchyItem.java

License:Open Source License

public JsonObject toJson() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.add("classElement", classElement.toJson());
    if (displayName != null) {
        jsonObject.addProperty("displayName", displayName);
    }//from w  ww. jav a2s  .  c o  m
    if (memberElement != null) {
        jsonObject.add("memberElement", memberElement.toJson());
    }
    if (superclass != null) {
        jsonObject.addProperty("superclass", superclass);
    }
    JsonArray jsonArrayInterfaces = new JsonArray();
    for (int elt : interfaces) {
        jsonArrayInterfaces.add(new JsonPrimitive(elt));
    }
    jsonObject.add("interfaces", jsonArrayInterfaces);
    JsonArray jsonArrayMixins = new JsonArray();
    for (int elt : mixins) {
        jsonArrayMixins.add(new JsonPrimitive(elt));
    }
    jsonObject.add("mixins", jsonArrayMixins);
    JsonArray jsonArraySubclasses = new JsonArray();
    for (int elt : subclasses) {
        jsonArraySubclasses.add(new JsonPrimitive(elt));
    }
    jsonObject.add("subclasses", jsonArraySubclasses);
    return jsonObject;
}

From source file:com.google.dart.server.internal.remote.utilities.RequestUtilities.java

License:Open Source License

@VisibleForTesting
public static JsonElement buildJsonElement(Object object) {
    if (object instanceof Boolean) {
        return new JsonPrimitive((Boolean) object);
    } else if (object instanceof Number) {
        return new JsonPrimitive((Number) object);
    } else if (object instanceof String) {
        return new JsonPrimitive((String) object);
    } else if (object instanceof List<?>) {
        List<?> list = (List<?>) object;
        JsonArray jsonArray = new JsonArray();
        for (Object item : list) {
            JsonElement jsonItem = buildJsonElement(item);
            jsonArray.add(jsonItem);// ww  w. j  av a 2s  .c o  m
        }
        return jsonArray;
    } else if (object instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) object;
        JsonObject jsonObject = new JsonObject();
        for (Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            // prepare string key
            String keyString;
            if (key instanceof String) {
                keyString = (String) key;
            } else {
                throw new IllegalArgumentException("Unable to convert to string: " + getClassName(key));
            }
            // prepare JsonElement value
            Object value = entry.getValue();
            JsonElement valueJson = buildJsonElement(value);
            // put a property into the JSON object
            if (keyString != null && valueJson != null) {
                jsonObject.add(keyString, valueJson);
            }
        }
        return jsonObject;
    } else if (object instanceof AnalysisError) {
        return buildJsonObjectAnalysisError((AnalysisError) object);
    } else if (object instanceof AddContentOverlay) {
        return ((AddContentOverlay) object).toJson();
    } else if (object instanceof ChangeContentOverlay) {
        return ((ChangeContentOverlay) object).toJson();
    } else if (object instanceof RemoveContentOverlay) {
        return ((RemoveContentOverlay) object).toJson();
    } else if (object instanceof AnalysisOptions) {
        return ((AnalysisOptions) object).toJson();
    } else if (object instanceof Location) {
        return buildJsonObjectLocation((Location) object);
    }
    throw new IllegalArgumentException("Unable to convert to JSON: " + object);
}

From source file:com.google.devfestnorte.server.APIUpdater.java

License:Open Source License

private ManifestData extractManifestData(JsonObject currentManifest, ManifestData copyFrom) {
    ManifestData data = new ManifestData();
    data.majorVersion = copyFrom == null ? Config.MANIFEST_VERSION : copyFrom.majorVersion;
    data.minorVersion = copyFrom == null ? 0 : copyFrom.minorVersion;
    data.dataFiles = new JsonArray();

    if (currentManifest != null) {
        try {//from   w  ww.j a v  a  2  s  . com
            JsonArray files = currentManifest.get("data_files").getAsJsonArray();
            for (JsonElement file : files) {
                String filename = file.getAsString();
                Matcher matcher = Config.SESSIONS_PATTERN.matcher(filename);
                if (matcher.matches()) {
                    // versions numbers are only extracted from the existing session filename if copyFrom is null.
                    if (copyFrom == null) {
                        data.majorVersion = Integer.parseInt(matcher.group(1));
                        data.minorVersion = Integer.parseInt(matcher.group(2));
                    }
                } else {
                    data.dataFiles.add(file);
                }
            }
        } catch (NullPointerException ex) {
            Logger.getLogger(getClass().getName())
                    .warning("Ignoring existing manifest, as it seems " + "to be badly formatted.");
        } catch (NumberFormatException ex) {
            Logger.getLogger(getClass().getName())
                    .warning("Ignoring existing manifest, as it seems " + "to be badly formatted.");
        }
    }

    if (copyFrom == null) {
        // Increment the minor version
        data.minorVersion++;
        data.sessionsFilename = MessageFormat.format(Config.SESSIONS_FORMAT, data.majorVersion,
                data.minorVersion);
    } else {
        data.sessionsFilename = copyFrom.sessionsFilename;
    }

    data.dataFiles.add(new JsonPrimitive(data.sessionsFilename));
    return data;
}

From source file:com.google.gdt.googleapi.core.ApiDirectoryListingJsonCodec.java

License:Open Source License

protected void populateJsonFromApiInfo(ReadableApiInfo src, JsonObject entry, String baseHref) {
    if (null != src.getName()) {
        entry.addProperty("name", src.getName());
    }/*from ww  w  . j  a va2  s .c o m*/
    if (null != src.getVersion()) {
        entry.addProperty("version", src.getVersion());
    }
    if (null != src.getDisplayName()) {
        entry.addProperty("title", src.getDisplayName());
    }
    if (null != src.getPublisher()) {
        entry.addProperty("publisher", src.getPublisher());
    }
    String[] labels = src.getLabels();
    if (labels.length > 0) {
        JsonArray jsonLabels = new JsonArray();
        for (String label : labels) {
            jsonLabels.add(new JsonPrimitive(label));
        }
        entry.add("labels", jsonLabels);
    }
    entry.addProperty("ranking", src.getRanking());
    if (null != src.getDescription()) {
        entry.addProperty("description", src.getDescription());
    }
    Set<String> iconLinkRefs = src.getIconLinkKeys();
    if (!iconLinkRefs.isEmpty()) {
        JsonObject iconLinks = new JsonObject();
        for (String ref : iconLinkRefs) {
            URL link = src.getIconLink(ref);
            if (link == null) {
                break;
            }
            String linkStr = renderLink(link, baseHref);
            if (linkStr == null) {
                break;
            }
            iconLinks.addProperty(ref, linkStr);
        }
        entry.add("icons", iconLinks);
    }
    if (null != src.getTosLink()) {
        entry.addProperty("tosLink", src.getTosLink().toExternalForm());
    }
    if (null != src.getReleaseDate()) {
        entry.addProperty("releaseDate", src.getReleaseDate().toString());
    }
    if (null != src.getReleaseNotesLink()) {
        entry.addProperty("releaseNotesLink", src.getReleaseNotesLink().toExternalForm());
    }
    if (null != src.getDiscoveryLink()) {
        entry.addProperty("discoveryLink", src.getDiscoveryLink().toExternalForm());
    }
    if (null != src.getDocumentationLink()) {
        entry.addProperty("documentationLink", src.getDocumentationLink().toExternalForm());
    }
    if (null != src.getDownloadLink()) {
        entry.addProperty("downloadLink", src.getDownloadLink().toExternalForm());
    }
}

From source file:com.google.gerrit.httpd.restapi.ParameterParser.java

License:Apache License

@VisibleForTesting
static JsonObject formToJson(Map<String, String[]> map, Set<String> query) throws BadRequestException {
    JsonObject inputObject = new JsonObject();
    for (Map.Entry<String, String[]> ent : map.entrySet()) {
        String key = ent.getKey();
        String[] values = ent.getValue();

        if (query.contains(key) || values.length == 0) {
            // Disallow processing query parameters as input body fields.
            // Implementations of views should avoid duplicate naming.
            continue;
        }//from  ww  w  .ja  v a 2  s .co  m

        JsonObject obj = inputObject;
        int dot = key.indexOf('.');
        if (0 <= dot) {
            String property = key.substring(0, dot);
            JsonElement e = inputObject.get(property);
            if (e == null) {
                obj = new JsonObject();
                inputObject.add(property, obj);
            } else if (e.isJsonObject()) {
                obj = e.getAsJsonObject();
            } else {
                throw new BadRequestException(String.format("key %s conflicts with %s", key, property));
            }
            key = key.substring(dot + 1);
        }

        if (obj.get(key) != null) {
            // This error should never happen. If all form values are handled
            // together in a single pass properties are set only once. Setting
            // again indicates something has gone very wrong.
            throw new BadRequestException("invalid form input, use JSON instead");
        } else if (values.length == 1) {
            obj.addProperty(key, values[0]);
        } else {
            JsonArray list = new JsonArray();
            for (String v : values) {
                list.add(new JsonPrimitive(v));
            }
            obj.add(key, list);
        }
    }
    return inputObject;
}

From source file:com.google.gerrit.sshd.commands.QueryShell.java

License:Apache License

/**
 * Outputs a result set to stdout in Json format.
 *
 * @param rs ResultSet to show./*from   w  ww  .j  a  v  a2  s .  c  o  m*/
 * @param alreadyOnRow true if rs is already on the first row. false
 *     otherwise.
 * @param start Timestamp in milliseconds when executing the statement
 *     started. This timestamp is used to compute statistics about the
 *     statement. If no statistics should be shown, set it to 0.
 * @param show Functions to map columns
 * @throws SQLException
 */
private void showResultSetJson(final ResultSet rs, boolean alreadyOnRow, long start, Function... show)
        throws SQLException {
    JsonArray collector = new JsonArray();
    final ResultSetMetaData meta = rs.getMetaData();
    final Function[] columnMap;
    if (show != null && 0 < show.length) {
        columnMap = show;

    } else {
        final int colCnt = meta.getColumnCount();
        columnMap = new Function[colCnt];
        for (int colId = 0; colId < colCnt; colId++) {
            final int p = colId + 1;
            final String name = meta.getColumnLabel(p);
            columnMap[colId] = new Identity(p, name);
        }
    }

    int rowCnt = 0;
    while (alreadyOnRow || rs.next()) {
        final JsonObject row = new JsonObject();
        final JsonObject cols = new JsonObject();
        for (Function function : columnMap) {
            String v = function.apply(rs);
            if (v == null) {
                continue;
            }
            cols.addProperty(function.name.toLowerCase(), v);
        }
        row.addProperty("type", "row");
        row.add("columns", cols);
        switch (outputFormat) {
        case JSON:
            println(row.toString());
            break;
        case JSON_SINGLE:
            collector.add(row);
            break;
        default:
            final JsonObject obj = new JsonObject();
            obj.addProperty("type", "error");
            obj.addProperty("message", "Unsupported Json variant");
            println(obj.toString());
            return;
        }
        alreadyOnRow = false;
        rowCnt++;
    }

    JsonObject tail = null;
    if (start != 0) {
        tail = new JsonObject();
        tail.addProperty("type", "query-stats");
        tail.addProperty("rowCount", rowCnt);
        final long ms = TimeUtil.nowMs() - start;
        tail.addProperty("runTimeMilliseconds", ms);
    }

    switch (outputFormat) {
    case JSON:
        if (tail != null) {
            println(tail.toString());
        }
        break;
    case JSON_SINGLE:
        if (tail != null) {
            collector.add(tail);
        }
        println(collector.toString());
        break;
    default:
        final JsonObject obj = new JsonObject();
        obj.addProperty("type", "error");
        obj.addProperty("message", "Unsupported Json variant");
        println(obj.toString());
    }
}