Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.blackducksoftware.integration.email.batch.processor.ProcessorTestUtil.java

License:Apache License

public VulnerabilityView createVulnerability(final String vulnId, final VulnerabilitySeverityEnum severity,
        final Gson gson) throws IntegrationException {
    final Map<String, Object> fieldMap = new HashMap<>();
    fieldMap.put("vulnerabilityName", vulnId);
    fieldMap.put("description", "A vulnerability");
    fieldMap.put("vulnerabilityPublishedDate", "today");
    fieldMap.put("vulnerabilityUpdatedDate", "a minute ago");
    fieldMap.put("baseScore", 10.0);
    fieldMap.put("impactSubscore", 5.0);
    fieldMap.put("exploitabilitySubscore", 1.0);
    fieldMap.put("source", "");
    if (severity != null) {
        fieldMap.put("severity", severity.name());
    }//from   w  w  w . j  a  va  2s .  com
    fieldMap.put("accessVector", "");
    fieldMap.put("accessComplexity", "");
    fieldMap.put("authentication", "");
    fieldMap.put("confidentialityImpact", "");
    fieldMap.put("integrityImpact", "");
    fieldMap.put("availabilityImpact", "");
    fieldMap.put("cweId", vulnId);
    final VulnerabilityView view = ObjectFactory.INSTANCE.createPopulatedInstance(VulnerabilityView.class,
            fieldMap);
    view.json = gson.toJson(view);

    final JsonElement element = jsonParser.parse(view.json);
    final JsonObject jsonObject = element.getAsJsonObject();
    final JsonObject links = new JsonObject();
    links.addProperty(MetaService.VULNERABILITIES_LINK, "");
    jsonObject.add("_meta", links);

    view.json = gson.toJson(jsonObject);
    return view;
}

From source file:com.blackducksoftware.integration.hub.api.notification.NotificationRestService.java

License:Apache License

@Override
public List<NotificationItem> getItems(final JsonObject jsonObject) {
    final JsonArray jsonArray = jsonObject.get("items").getAsJsonArray();
    final List<NotificationItem> allNotificationItems = new ArrayList<>(jsonArray.size());
    for (final JsonElement jsonElement : jsonArray) {
        final String type = jsonElement.getAsJsonObject().get("type").getAsString();
        Class<? extends NotificationItem> clazz = NotificationItem.class;
        if (typeMap.containsKey(type)) {
            clazz = typeMap.get(type);/*from w w w  .j av  a  2s .  c  om*/
        }
        allNotificationItems.add(getRestConnection().getGson().fromJson(jsonElement, clazz));
    }

    return allNotificationItems;
}

From source file:com.blackducksoftware.integration.hub.detect.detector.packagist.PackagistParser.java

License:Apache License

private List<NameVersion> parseDependencies(final JsonObject packageJson, final boolean checkDev) {
    final List<NameVersion> dependencies = new ArrayList<>();

    final JsonElement require = packageJson.get("require");
    if (require != null && require.isJsonObject()) {
        dependencies.addAll(parseDependenciesFromRequire(require.getAsJsonObject()));
    }/*from   w  ww  . ja va 2  s . c  o m*/

    if (checkDev) {
        final JsonElement devRequire = packageJson.get("require-dev");
        if (devRequire != null && devRequire.isJsonObject()) {
            dependencies.addAll(parseDependenciesFromRequire(devRequire.getAsJsonObject()));
        }

    }

    return dependencies;
}

From source file:com.blackducksoftware.integration.hub.fod.utils.HubFoDJsonParser.java

License:Apache License

public static String getVulnerableComponentsURL(final String projectVersionJson) {

    final JsonObject jsonObject = new JsonParser().parse(projectVersionJson).getAsJsonObject();
    final JsonArray linksArray = jsonObject.get(JSON_META_LABEL).getAsJsonObject().get(JSON_META_LINKS)
            .getAsJsonArray();//  www  .j a va 2s  . c om
    for (final JsonElement link : linksArray) {
        final JsonObject linkObj = link.getAsJsonObject();
        if (linkObj.get(JSON_META_REL).getAsString().equalsIgnoreCase(JSON_META_VULNERABLE_COMPONENTS)) {
            return linkObj.get(JSON_HREF).getAsString();
        }
    }

    return null;

}

From source file:com.blackducksoftware.integration.hub.fod.utils.HubFoDJsonParser.java

License:Apache License

public static HashMap<String, String> getVulnerableComponentLicense(final String vulnComponentJson) {
    final HashMap<String, String> licenseMap = new HashMap<>();

    final JsonObject jsonObject = new JsonParser().parse(vulnComponentJson).getAsJsonObject();
    final JsonArray licenseArray = jsonObject.get(JSON_LICENSE).getAsJsonObject().get(JSON_LICENSES)
            .getAsJsonArray();//ww w  .  j ava2  s  . c  o  m

    for (final JsonElement license : licenseArray) {
        final JsonObject licenseObj = license.getAsJsonObject();
        licenseMap.put(licenseObj.get(JSON_NAME).getAsString(),
                licenseObj.get(JSON_CODE_SHARING).getAsString());
    }

    return licenseMap;
}

From source file:com.blackypaw.mc.i18n.chat.ChatComponentDeserializer.java

License:Open Source License

/**
 * Creates a text component given its JSON representation. Both the shorthand notation as
 * a raw string as well as the notation as a full-blown JSON object are supported.
 *
 * @param json The JSON element to be deserialized into a text component
 *
 * @return The deserialized TextComponent
 *///from  w w  w  .  j a va  2 s  .  c  o  m
private TextComponent deserializeTextComponent(JsonElement json) {
    final TextComponent component = new TextComponent("");

    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            component.setText(primitive.getAsString());
        }
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();

        if (object.has("text")) {
            JsonElement textElement = object.get("text");
            if (textElement.isJsonPrimitive()) {
                JsonPrimitive textPrimitive = textElement.getAsJsonPrimitive();
                if (textPrimitive.isString()) {
                    component.setText(textPrimitive.getAsString());
                }
            }
        }

        if (object.has("extra")) {
            JsonElement extraElement = object.get("extra");
            if (extraElement.isJsonArray()) {
                JsonArray extraArray = extraElement.getAsJsonArray();
                for (int i = 0; i < extraArray.size(); ++i) {
                    JsonElement fieldElement = extraArray.get(i);
                    component.addChild(this.deserializeComponent(fieldElement));
                }
            }
        }
    }

    return component;
}

From source file:com.bosch.osmi.bdp.access.mock.BdpApiAccessMockImpl.java

License:Open Source License

private JsonObject readJsonFile(Reader jsonReader) {
    // Read from File to String
    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(jsonReader);
    return jsonElement.getAsJsonObject();
}

From source file:com.brainardphotography.gravatar.contact.PCContactLoader.java

License:Apache License

@Override
public PCContact loadContact(Reader reader) throws IOException {
    JsonElement element = jsonParser.parse(reader);
    JsonObject object = element.getAsJsonObject();
    JsonArray entries = object.getAsJsonArray("entry");

    if (entries.size() > 0)
        return gson.fromJson(entries.get(0), PCContact.class);

    return null;/*from   w w  w.  j av a2s  . co  m*/
}

From source file:com.buddycloud.friendfinder.provider.Facebook.java

License:Apache License

@Override
public ContactProfile getProfile(String accessToken) throws Exception {

    StringBuilder meURLBuilder = new StringBuilder(GRAPH_URL);
    meURLBuilder.append("/me?fields=id&access_token=").append(accessToken);
    String myId = HttpUtils.consumeJSON(meURLBuilder.toString()).getAsJsonObject().get("id").getAsString();

    ContactProfile contactProfile = new ContactProfile(HashUtils.encodeSHA256(PROVIDER_NAME, myId));

    StringBuilder friendsURLBuilder = new StringBuilder(GRAPH_URL);
    friendsURLBuilder.append("/me/friends?fields=id&access_token=").append(accessToken);

    while (true) {
        JsonObject friendsJson = HttpUtils.consumeJSON(friendsURLBuilder.toString()).getAsJsonObject();

        JsonArray friendsArray = friendsJson.get("data").getAsJsonArray();
        for (JsonElement friendJson : friendsArray) {
            JsonObject friendObject = friendJson.getAsJsonObject();
            String friendId = friendObject.get("id").getAsString();
            contactProfile.addFriendHash(HashUtils.encodeSHA256(PROVIDER_NAME, friendId));
        }//w  ww . j ava2 s .c  o  m
        JsonObject paging = friendsJson.get("paging").getAsJsonObject();
        if (!paging.has("next")) {
            break;
        }
        friendsURLBuilder = new StringBuilder(paging.get("next").getAsString());
    }

    return contactProfile;
}

From source file:com.builtbroken.builder.html.data.ImageData.java

/**
 * Loads a file from disk as a json file
 * then parses the file looking for an array
 * of links formatted as {"key":{"text":"String","url":"String"}}
 *
 * @param file - file to load, throws an exception if the file is invalid
 *//*from w  ww. j  a v a  2s.co m*/
public void loadDataFromFile(File file) {
    if (file.exists()) {
        if (file.isFile()) {
            JsonElement linkJson = Utils.toJsonElement(file);
            if (linkJson.isJsonObject()) {
                parseJsonImageArray(linkJson.getAsJsonObject());
            } else {
                throw new IllegalArgumentException("Link data file is not a valid json object. File = " + file);
            }
        } else {
            throw new IllegalArgumentException("Link data file is not a valid file. File = " + file);
        }
    } else {
        throw new IllegalArgumentException("Link data file is missing. File = " + file);
    }
}