Example usage for com.google.gson JsonObject getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:com.betafase.mcmanager.utils.spiget.SpigetPlugin.java

public void loadInformation() {
    author = new AuthorNameRequest(id).getName();
    ServerRequest r = new ServerRequest("resources/" + id);
    JsonObject o = r.getAsJsonObject();
    name = o.get("name").getAsString();
    if (name.length() > 60) {
        name = name.substring(0, 60);//from   w w  w  .  ja v  a  2s.c o  m
    }
    tag = o.get("tag").getAsString();
    JsonObject file = o.getAsJsonObject("file");
    external = file.get("type").getAsString().equalsIgnoreCase("external");
    file_size = file.get("size").getAsDouble() + " " + file.get("sizeUnit").getAsString();
    download_url = "https://spigotmc.org/" + file.get("url").getAsString();
    JsonObject ro = o.getAsJsonObject("rating");
    rating = ro.get("average").getAsDouble() + " / 5 (" + ro.get("count") + " reviews)";
    downloads = o.get("downloads").getAsInt();
    last_update = Utils.makeDateReadable(o.get("updateDate").getAsLong() * 1000);
}

From source file:com.blackducksoftware.integration.hub.detect.detector.npm.NpmCliParser.java

License:Apache License

NpmParseResult convertNpmJsonFileToCodeLocation(final String sourcePath, final String npmLsOutput) {
    final JsonObject npmJson = new JsonParser().parse(npmLsOutput).getAsJsonObject();
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();

    final JsonElement projectNameElement = npmJson.getAsJsonPrimitive(JSON_NAME);
    final JsonElement projectVersionElement = npmJson.getAsJsonPrimitive(JSON_VERSION);
    String projectName = null;/*w w  w.j a v a  2s.c om*/
    String projectVersion = null;
    if (projectNameElement != null) {
        projectName = projectNameElement.getAsString();
    }
    if (projectVersionElement != null) {
        projectVersion = projectVersionElement.getAsString();
    }

    populateChildren(graph, null, npmJson.getAsJsonObject(JSON_DEPENDENCIES), true);

    final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPM, projectName,
            projectVersion);

    final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.NPM,
            sourcePath, externalId, graph).build();

    return new NpmParseResult(projectName, projectVersion, codeLocation);

}

From source file:com.blackducksoftware.integration.hub.detect.detector.npm.NpmCliParser.java

License:Apache License

private void populateChildren(final MutableDependencyGraph graph, final Dependency parentDependency,
        final JsonObject parentNodeChildren, final Boolean root) {
    if (parentNodeChildren == null) {
        return;// w  ww  . j a  va 2  s .c o m
    }
    final Set<Entry<String, JsonElement>> elements = parentNodeChildren.entrySet();
    elements.forEach(it -> {
        if (it.getValue() != null && it.getValue().isJsonObject()) {

        }
        final JsonObject element = it.getValue().getAsJsonObject();
        final String name = it.getKey();
        String version = null;
        final JsonPrimitive versionPrimitive = element.getAsJsonPrimitive(JSON_VERSION);
        if (versionPrimitive != null && versionPrimitive.isString()) {
            version = versionPrimitive.getAsString();
        }
        final JsonObject children = element.getAsJsonObject(JSON_DEPENDENCIES);

        if (name != null && version != null) {
            final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPM, name,
                    version);
            final Dependency child = new Dependency(name, version, externalId);

            populateChildren(graph, child, children, false);
            if (root) {
                graph.addChildToRoot(child);
            } else {
                graph.addParentWithChild(parentDependency, child);
            }
        }
    });
}

From source file:com.builtbroken.builder.html.page.PageData.java

/**
 * Called to load the page from json/* ww w . ja v a2s.c  om*/
 */
public void load(PageBuilder builder) {
    imgReplaceKeys = new HashMap();
    linkReplaceKeys = new HashMap();
    //TODO format nano time outputs on debugger
    debug("Loading new Page Data");

    long startTime = System.nanoTime();
    debug("\tLoading file from disk");
    JsonElement element = Utils.toJsonElement(file);
    startTime = System.nanoTime() - startTime;
    debug("\tDone..." + startTime + "ns");

    if (element.isJsonObject()) {
        debug("\tParsing json");
        startTime = System.nanoTime();

        JsonObject object = element.getAsJsonObject();
        if (object.has("pageName")) {
            pageName = object.getAsJsonPrimitive("pageName").getAsString();
            debug("\tDisplay Name: " + pageName);
        }
        if (object.has("fileName")) {
            fileNameOverride = object.getAsJsonPrimitive("fileName").getAsString();
            debug("\tFile Name: " + fileNameOverride);
        }
        if (object.has("fileExtension")) {
            fileExtension = object.getAsJsonPrimitive("fileExtension").getAsString();
            debug("\tFile Extension: " + fileExtension);
        }
        if (object.has("type")) {
            type = object.getAsJsonPrimitive("type").getAsString().toLowerCase();
            debug("\tType: " + type);
            if (type.equalsIgnoreCase("ignore")) {
                return;
            }
        }
        if (object.has("category_display_name")) {
            categoryDisplayName = object.getAsJsonPrimitive("category_display_name").getAsString();
            debug("\tCategory Display Name: " + type);
        }
        if (object.has("category_display_order")) {
            categoryDisplayOrder = object.getAsJsonPrimitive("category_display_order").getAsString();
            debug("\tCategory Display Order: " + type);
        }
        if (object.has("category")) {
            category = object.getAsJsonPrimitive("category").getAsString().toLowerCase();
            debug("\tCategory: " + category);
        }
        if (object.has("replaceKeys")) {
            Gson gson = new Gson();
            Map<String, String> map = new HashMap();
            map = (Map<String, String>) gson.fromJson(object.get("replaceKeys"), map.getClass());
            linkReplaceKeys.putAll(map);
            debug("\tLinks Keys: " + linkReplaceKeys.size());
        }
        if (object.has("imageKeys")) {
            Gson gson = new Gson();
            Map<String, String> map = new HashMap();
            map = (Map<String, String>) gson.fromJson(object.get("imageKeys"), map.getClass());
            imgReplaceKeys.putAll(map);
            debug("\tImage Keys: " + imgReplaceKeys.size());
        }
        if (object.has("content")) {
            debug("\tLoading Page Content");
            long time = System.nanoTime();
            //Split HTML into segments for injection
            process(toHTML(object.getAsJsonObject("content")));
            time = System.nanoTime() - time;
            debug("\tDone..." + time + "ns");
        }

    } else {
        throw new RuntimeException(
                "File " + file + " is not a valid json object so can not be parsed into a wiki page.");
    }

    debug("\tProcessing reference data...");
    //Loop all contained link replace keys
    for (Map.Entry<String, String> entry : linkReplaceKeys.entrySet()) {
        final String key = entry.getKey().toLowerCase();
        if (!builder.linkData.linkReplaceKeys.containsKey(key)) {
            String html = "<a href=\"" + getOutput(builder.vars.get("outputPath")) + "\">" + entry.getValue()
                    + "</a>";
            builder.linkData.linkReplaceKeys.put(key, html);
            debug("\t\tKey: " + key + " --> " + html);
        } else {
            throw new RuntimeException("Duplicate link key[" + entry.getKey() + "] was found for page ["
                    + pageName + "] key is linked to " + builder.linkData.linkReplaceKeys.get(key));
        }
    }
    //Loop all image replace keys
    for (Map.Entry<String, String> entry : imgReplaceKeys.entrySet()) {
        final String key = entry.getKey().toLowerCase();
        if (!builder.imageData.imageReplaceKeys.containsKey(key)) {
            String path = builder.vars.get("imagePath") + entry.getValue();
            String html = "<img src=\"" + entry.getValue() + "\">";
            builder.imageData.imageReplaceKeys.put(key, html);
            builder.imageData.images.put(key, path);
            debug("\t\tKey: " + key + " --> " + html);
        } else {
            throw new RuntimeException("Duplicate image key[" + entry.getKey() + "] was found for page ["
                    + pageName + "] key is linked to " + builder.imageData.imageReplaceKeys.get(key));
        }
    }

    //Debug
    startTime = System.nanoTime() - startTime;
    debug("Done..." + startTime + "ns");
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.BranchParser.java

License:Apache License

@Override
public Branch apply(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();
    final Branch branch = new Branch(json.get("id").getAsString(), json.get("displayId").getAsString(),
            json.get("latestChangeset").getAsString(), json.get("isDefault").getAsBoolean());
    if (json.has("metadata")) {
        branch.setMetadata(json.getAsJsonObject("metadata"));
    }//from  ww w.  jav a2  s .c o  m
    return branch;
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.CommentParser.java

License:Apache License

@Override
public Comment apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }/*from w ww .  j a va  2  s .  c  om*/
    JsonObject json = jsonElement.getAsJsonObject();
    return new Comment(json.get("id").getAsLong(), json.get("version").getAsLong(),
            json.get("text").getAsString(), userParser().apply(json.getAsJsonObject("author")),
            new Date(json.get("createdDate").getAsLong()), new Date(json.get("updatedDate").getAsLong()),
            listParser(commentParser()).apply(json.get("comments")),
            permittedOperationsParser().apply(json.getAsJsonObject("permittedOperations")));

}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.ProjectParser.java

License:Apache License

@Override
public Project apply(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();
    String selfUrl = null;//from   w  w  w .j  av  a 2 s  .co m

    if (json.has("links")) {
        selfUrl = linkParser().apply(json.getAsJsonObject("links").get("self").getAsJsonArray().get(0))
                .getHref();
    }

    return new Project(json.get("key").getAsString(), json.get("id").getAsLong(),
            json.get("name").getAsString(), optionalJsonString(json, "description"),
            optionalJsonBoolean(json, "public"), optionalJsonBoolean(json, "isPersonal"),
            ProjectType.valueOf(json.get("type").getAsString()), selfUrl);
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.PullRequestActivityParser.java

License:Apache License

@Override
public PullRequestActivity apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }/*www  . j  ava 2s . com*/
    JsonObject json = jsonElement.getAsJsonObject();

    PullRequestActivityActionType actionType = PullRequestActivityActionType
            .valueOf(json.get("action").getAsString());
    if (actionType == PullRequestActivityActionType.COMMENTED) {
        return new PullRequestCommentActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId, json.get("commentAction").getAsString(),
                commentParser().apply(json.getAsJsonObject("comment")),
                commentAnchorParser().apply(json.getAsJsonObject("commentAnchor")),
                json.has("diff") ? diffParser().apply(json.getAsJsonObject("diff")) : null

        );
    } else if (actionType == PullRequestActivityActionType.RESCOPED) {
        List<Commit> added = new ArrayList<>();
        List<Commit> removed = new ArrayList<>();
        if (json.has("added")) {
            added = getCommits(json.get("added").getAsJsonObject());
        }
        if (json.has("removed")) {
            removed = getCommits(json.get("removed").getAsJsonObject());
        }

        return new PullRequestRescopedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId, json.get("fromHash").getAsString(), json.get("previousFromHash").getAsString(),
                json.get("previousToHash").getAsString(), json.get("toHash").getAsString(), added, removed);

    } else if (actionType == PullRequestActivityActionType.MERGED) {
        return new PullRequestMergedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.APPROVED) {
        return new PullRequestApprovedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.DECLINED) {
        return new PullRequestDeclinedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.UNAPPROVED) {
        return new PullRequestUnapprovedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.OPENED) {
        return new PullRequestOpenedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.REOPENED) {
        return new PullRequestReOpenedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.UPDATED) {
        return new PullRequestUpdatedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else if (actionType == PullRequestActivityActionType.REVIEWED) {
        return new PullRequestReviewedActivity(json.get("id").getAsLong(),
                new Date(json.get("createdDate").getAsLong()), userParser().apply(json.getAsJsonObject("user")),
                pullRequestId);
    } else
        throw new RuntimeException("cannot parse action:" + actionType);

}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.PullRequestBranchParser.java

License:Apache License

@Override
public PullRequestBranch apply(JsonElement json) {
    if (json == null || !json.isJsonObject()) {
        return null;
    }//from  www  .j  a  v  a  2 s .  c  om
    JsonObject jsonObject = json.getAsJsonObject();
    JsonObject repository = jsonObject.getAsJsonObject("repository");
    JsonObject project = repository.getAsJsonObject("project");
    return new PullRequestBranch(jsonObject.get("id").getAsString(), repository.get("slug").getAsString(),
            optionalJsonString(repository, "name"), project.get("key").getAsString());
}

From source file:com.ccreanga.bitbucket.rest.client.http.responseparsers.PullRequestChangeParser.java

License:Apache License

@Override
public PullRequestChange apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }//  w w w.j  a  v  a2  s. c o m
    JsonObject json = jsonElement.getAsJsonObject();
    String selfUrl = null;
    if (json.has("links")) {
        selfUrl = linkParser().apply(json.getAsJsonObject("links").get("self").getAsJsonArray().get(0))
                .getHref();
    }
    return new PullRequestChange(json.get("contentId").getAsString(), optionalJsonString(json, "fromContentId"),
            pathParser().apply(json.getAsJsonObject("path")),
            pathParser().apply(json.getAsJsonObject("srcPath")),
            FileChangeType.valueOf(json.get("type").getAsString()), json.get("executable").getAsBoolean(),
            json.get("percentUnchanged").getAsInt(), NodeType.valueOf(json.get("nodeType").getAsString()),
            json.get("executable").getAsBoolean(), selfUrl);

}