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.canoo.cog.sonar.SonarResultParser.java

License:Apache License

List<SonarProject> parseProjects(String projectResultString) {
    List<SonarProject> sonarProjects = new ArrayList<>();

    JsonElement projects = jsonParser.parse(projectResultString);
    JsonArray projectArray = projects.getAsJsonArray();
    for (JsonElement projectJson : projectArray) {
        String key = projectJson.getAsJsonObject().get("key").getAsString();
        String name = projectJson.getAsJsonObject().get("name").getAsString();
        String version = projectJson.getAsJsonObject().get("version").getAsString();
        sonarProjects.add(new SonarProjectImpl(key, name, version));
    }/*from  www . j ava  2s.c  o  m*/
    return sonarProjects;
}

From source file:com.canoo.cog.sonar.SonarResultParser.java

License:Apache License

CityModelImpl parseCity(String cityResultString) {

    JsonElement cityJson = jsonParser.parse(cityResultString);
    JsonArray allComponents = cityJson.getAsJsonArray();

    // Create city
    JsonObject cityObject = allComponents.get(0).getAsJsonObject();
    JsonToModelConverter jsonToModelConverter = new JsonToModelConverter();
    CityModelImpl city = jsonToModelConverter.convertToCity(cityObject);

    // Sort by name
    List<JsonObjectWrapper> javaElements = new ArrayList<>();
    for (JsonElement component : allComponents) {
        javaElements.add(new JsonObjectWrapper(component.getAsJsonObject()));
    }//from   w  ww .ja  va  2  s .co m
    Collections.sort(javaElements);

    // Add neighborhoods
    for (JsonObjectWrapper component : javaElements) {
        JsonObject compObject = component.getObject();
        String scopeString = compObject.get("scope").getAsString();
        if (scopeString.equals("DIR")) {
            HoodModelImpl newHood = jsonToModelConverter.convertToHood(compObject);
            city.addHoodToCity(newHood);
        }
    }

    // Add buildings
    for (JsonObjectWrapper component : javaElements) {
        JsonObject compObject = component.getObject();
        String scopeString = compObject.get("scope").getAsString();
        String qualifierString = compObject.get("qualifier").getAsString();
        if (scopeString.equals("FIL") && !qualifierString.equals("UTS")) {
            BuildingModelImpl buildingModel = jsonToModelConverter.convertToBuilding(compObject);
            city.addBuildingToCity(buildingModel);
        }
    }

    // drop empty hoods
    city.removeEmptyHoods();

    return city;
}

From source file:com.canoo.dolphin.impl.codec.CreatePresentationModelEncoder.java

License:Apache License

@Override
public CreatePresentationModelCommand decode(JsonObject jsonObject) {
    Assert.requireNonNull(jsonObject, "jsonObject");

    try {/*from   w  w w.j  a  va  2  s. com*/
        final CreatePresentationModelCommand command = new CreatePresentationModelCommand();

        command.setPmId(jsonObject.getAsJsonPrimitive(PM_ID).getAsString());
        command.setPmType(jsonObject.getAsJsonPrimitive(PM_TYPE).getAsString());
        command.setClientSideOnly(false);

        final JsonArray jsonArray = jsonObject.getAsJsonArray(PM_ATTRIBUTES);
        final List<Map<String, Object>> attributes = new ArrayList<>();
        for (final JsonElement jsonElement : jsonArray) {
            final JsonObject attribute = jsonElement.getAsJsonObject();
            final HashMap<String, Object> map = new HashMap<>();
            map.put("propertyName", attribute.getAsJsonPrimitive(ATTRIBUTE_NAME).getAsString());
            map.put("id", attribute.getAsJsonPrimitive(ATTRIBUTE_ID).getAsString());
            final Object value = attribute.has(ATTRIBUTE_VALUE) ? decodeValue(attribute.get(ATTRIBUTE_VALUE))
                    : null;
            map.put("value", value);
            map.put("baseValue", value);
            map.put("qualifier", null);
            attributes.add(map);
        }
        command.setAttributes(attributes);

        return command;
    } catch (IllegalStateException | ClassCastException | NullPointerException ex) {
        throw new JsonParseException("Illegal JSON detected", ex);
    }
}

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 {//from w  w w  .java 2  s  .  c  om
        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;
}

From source file:com.ccc.crest.core.client.json.LogoDeserializer.java

License:Open Source License

@Override
public Logo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return new Logo(json.getAsJsonObject());
}

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

License:Apache License

@Override
public ImmutableMap<String, String> getBitBucketApplicationProperties() {
    String requestUrl = "/rest/api/1.0/application-properties";

    JsonElement jsonElement = execute(requestUrl, GET).get();
    ImmutableMap.Builder<String, String> resultBuilder = ImmutableMap.builder();
    if (jsonElement != null) {
        for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
            resultBuilder.put(entry.getKey(), entry.getValue().getAsString());
        }/*  w  w  w .  j a  v  a  2s.  co m*/
    }
    return resultBuilder.build();
}

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"));
    }/*  w  w w . j  a  v  a 2 s.co  m*/
    return branch;
}

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

License:Apache License

@Override
public Map<String, BuildStatsSummary> apply(JsonElement jsonElement) {
    JsonObject json = jsonElement.getAsJsonObject();
    ImmutableMap.Builder<String, BuildStatsSummary> builder = ImmutableMap.builder();
    for (Map.Entry<String, JsonElement> commitEntries : json.entrySet()) {
        BuildStatsSummary.Builder element = BuildStatsSummary.builder();
        for (Map.Entry<String, JsonElement> e : commitEntries.getValue().getAsJsonObject().entrySet()) {
            if ("failed".equals(e.getKey())) {
                element.setFailed(e.getValue().getAsLong());
            } else if ("inProgress".equals(e.getKey())) {
                element.setInProgress(e.getValue().getAsLong());
            } else if ("successful".equals(e.getKey())) {
                element.setSuccessful(e.getValue().getAsLong());
            }/* w w w  .j av  a2  s.  c o  m*/
        }
        builder.put(commitEntries.getKey(), element.build());
    }
    return builder.build();
}

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

License:Apache License

@Override
public CommentAnchor apply(JsonElement jsonElement) {
    if (jsonElement == null || !jsonElement.isJsonObject()) {
        return null;
    }//  w  w  w. j a  va  2s .co m

    JsonObject json = jsonElement.getAsJsonObject();
    return new CommentAnchor(optionalJsonString(json, "fromHash"), json.get("toHash").getAsString(),
            optionalJsonLong(json, "line"),
            json.has("lineType") ? LineType.valueOf(json.get("lineType").getAsString()) : null,
            json.has("fileType") ? FileType.valueOf(json.get("fileType").getAsString()) : null,
            json.get("path").getAsString(), optionalJsonString(json, "srcPath"),
            json.get("orphaned").getAsBoolean());
}

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   ww  w. java2  s.co  m*/
    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")));

}