Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

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;/*from w w w .  ja  va2  s.co  m*/
    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.brighttag.kairosdb.TridentDataPointFactory.java

License:Apache License

@Override
public DataPoint getDataPoint(long timestamp, JsonElement json) throws IOException {
    return factory(timestamp, json.getAsString());
}

From source file:com.builtbroken.builder.html.parts.HTMLPartHeader.java

@Override
public String process(JsonElement value) {
    try {//  w  ww . jav a2s.c  o m
        if (value.isJsonObject() && !value.isJsonPrimitive()) {
            JsonObject h = value.getAsJsonObject();
            String text = h.getAsJsonPrimitive("text").getAsString();
            int size = 2;
            if (h.has("size")) {
                JsonPrimitive p = h.getAsJsonPrimitive("size");
                if (p.isString()) {
                    String s = p.getAsString().toLowerCase();
                    if (s.equals("small")) {
                        size = 3;
                    } else if (s.equals("medium")) {
                        size = 2;
                    } else if (s.equals("large")) {
                        size = 1;
                    }
                } else {
                    size = p.getAsInt();
                }
            }
            if (h.has("link")) {
                String link = h.getAsJsonPrimitive("link").getAsString();
                if (link.startsWith("url")) {
                    return "<h" + size + "><a href=\"" + link + "\">" + text + "</a></h" + size + ">";
                } else if (link.endsWith(".json")) {
                    return "<h" + size + "><a href=\"#PageRef:" + link + "#\">" + text + "</a></h" + size + ">";
                } else {
                    return "<h" + size + "><a href=\"#" + link + "#\">" + text + "</a></h" + size + ">";
                }
            }
            return "<h" + size + ">" + text + "</h" + size + ">";
        }
        return "<h2>" + value.getAsString() + "</h2>";
    } catch (Exception e) {
        throw new RuntimeException("Unexpected error while parsing header tag " + value, e);
    }
}

From source file:com.builtbroken.builder.html.parts.HTMLPartParagraph.java

@Override
public String process(JsonElement element) {
    return "<p>" + element.getAsString() + "</p>";
}

From source file:com.ccc.crest.core.cache.crest.alliance.Alliance.java

License:Open Source License

@Override
public Alliance deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (ShortNameKey.equals(key))
            shortName = value.getAsString();
        else if (HrefKey.equals(key))
            url = value.getAsString();//from   w w w  .j  av a 2  s.  co  m
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.corporation.Corporation.java

License:Open Source License

@Override
public Corporation deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = value.getAsString();
        else if (HeadquartersKey.equals(key)) {
            headquarters = new Headquarters();
            headquarters.deserialize(value, typeOfT, context);
        } else if (UrlKey.equals(key))
            corpUrl = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (LoyaltyKey.equals(key)) {
            ExternalRef ref = new ExternalRef();
            ref.deserialize(value, typeOfT, context);
            loyaltyUrl = ref.url;/* www .j  a v a2  s. com*/
        } else if (TickerKey.equals(key))
            ticker = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.corporation.Headquarters.java

License:Open Source License

@Override
public Headquarters deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (HrefKey.equals(key))
            stationUrl = value.getAsString();
        else if (NameKey.equals(key))
            name = value.getAsString();/*www  . j  ava 2 s  .  c  o m*/
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.dogma.DogmaAttribute.java

License:Open Source License

@Override
public DogmaAttribute deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (DescriptionKey.equals(key))
            description = value.getAsString();
        else if (HeadquartersKey.equals(key)) {
            ;//from w w  w.  j a  va 2s .  c  om
        } else if (UrlKey.equals(key))
            corpUrl = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (LoyaltyKey.equals(key)) {
            ExternalRef ref = new ExternalRef();
            ref.deserialize(value, typeOfT, context);
            loyaltyUrl = ref.url;
        } else if (TickerKey.equals(key))
            ticker = value.getAsString();
        else if (IdStrKey.equals(key))
            idStr = value.getAsString();
        else if (IdKey.equals(key))
            id = value.getAsLong();
        else if (NameKey.equals(key))
            name = value.getAsString();
        else
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.ExternalRef.java

License:Open Source License

@Override
public ExternalRef deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Iterator<Entry<String, JsonElement>> objectIter = ((JsonObject) json).entrySet().iterator();
    while (objectIter.hasNext()) {
        Entry<String, JsonElement> objectEntry = objectIter.next();
        String key = objectEntry.getKey();
        JsonElement value = objectEntry.getValue();
        if (HrefKey.equals(key))
            url = value.getAsString();
        else if (LinkTypeKey.equals(key))
            linkType = value.getAsString();
        else/* ww w  .  j a v a  2s.  co  m*/
            LoggerFactory.getLogger(getClass())
                    .warn(key + " has a field not currently being handled: \n" + objectEntry.toString());
    }
    return this;
}

From source file:com.ccc.crest.core.cache.crest.schema.endpoint.EndpointCollection.java

License:Open Source License

@Override
public EndpointCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject topObj = (JsonObject) json;
    Set<Entry<String, JsonElement>> topSet = topObj.entrySet();
    Iterator<Entry<String, JsonElement>> topIter = topSet.iterator();
    do {// w w  w. j a  v  a2s  . c o  m
        if (!topIter.hasNext())
            break;
        Entry<String, JsonElement> topEntry = topIter.next();
        String topKey = topEntry.getKey();
        JsonElement topElement = topEntry.getValue();
        if (topKey.equals(UserCountKey)) {
            userCount = Long.parseLong(topElement.getAsString());
            continue;
        }
        if (topKey.equals(UserCountStrKey))
            continue;
        if (topKey.equals(ServerVersionKey)) {
            serverVersion = topElement.getAsString();
            continue;
        }
        if (topKey.equals(ServerNameKey)) {
            serverName = topElement.getAsString();
            continue;
        }
        if (topKey.equals(ServerStatusKey)) {
            serviceStatus = topElement.getAsString();
            continue;
        }
        // if its not a top object level known variable from above list, it must be a group object
        if (topElement.isJsonPrimitive()) {
            log.warn("unexpected key: " + topKey + " = " + topObj.toString());
            continue;
        }
        if (!topElement.isJsonObject()) {
            log.warn("expected an object: " + topKey + " = " + topObj.toString());
            continue;
        }
        // first pass you should have a group in the topElement
        String groupName = topKey;
        EndpointGroup endpointGroup = new EndpointGroup(groupName);
        callGroups.add(endpointGroup);
        Set<Entry<String, JsonElement>> groupSet = topElement.getAsJsonObject().entrySet();
        Iterator<Entry<String, JsonElement>> groupIter = groupSet.iterator();
        do {
            if (!groupIter.hasNext())
                break;
            Entry<String, JsonElement> groupEntry = groupIter.next();
            // expecting a primitive href here
            String endpointName = groupEntry.getKey();
            JsonElement hrefElement = groupEntry.getValue();
            if (hrefElement.isJsonObject()) {
                JsonObject groupChildObj = (JsonObject) hrefElement;
                Set<Entry<String, JsonElement>> groupChildSet = groupChildObj.entrySet();
                Iterator<Entry<String, JsonElement>> groupChildIter = groupChildSet.iterator();
                if (!groupChildIter.hasNext())
                    break;
                Entry<String, JsonElement> groupChildEntry = groupChildIter.next();
                String groupChildKey = groupChildEntry.getKey();
                JsonElement groupChildElement = groupChildEntry.getValue();
                endpointGroup.addEndpoint(new CrestEndpoint(endpointName, groupChildElement.getAsString()));
                continue;
            }
            // expect an object with href in it
            if (!hrefElement.isJsonPrimitive()) {
                log.warn("expected a primitive after group: " + groupName + " = " + hrefElement.toString());
                continue;
            }
            endpointGroup.addEndpoint(new CrestEndpoint(endpointName, hrefElement.getAsString()));
            break;
        } while (true);
    } while (true);
    return this;
}