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.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Convert json string to AppDto array./*  w ww.java2  s. c  o m*/
 * @param json
 *            a json string
 * @return the list of app object
 * @throws IOException
 */
public List<AppDto> jsonToApps(String json) throws IOException {
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    if (!root.isJsonObject())
        return null;

    JsonArray apps = root.getAsJsonObject().get("apps").getAsJsonArray();

    if (!apps.isJsonArray())
        return null;

    Type collectionType = new TypeToken<Collection<AppDto>>() {
    }.getType();
    Gson gson = new Gson();

    return gson.fromJson(apps, collectionType);
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Converts the json string to the commentset object.
 * @param json/*w  ww  .j  ava  2s.co m*/
 *            a json string
 * @return commentset object
 * @throws IOException
 */
public CommentSet jsonToCommentSet(Connection con, String json) throws IOException {

    CommentSet cs = new CommentSet();
    com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
    JsonElement root = parser.parse(json);

    if (root.isJsonObject()) {
        JsonObject obj = root.getAsJsonObject();
        JsonArray comments = obj.get("comments").getAsJsonArray();
        for (JsonElement elem : comments) {
            Comment comment = readComment(elem);
            if (comment != null) {
                cs.add(comment);
            }
        }

        cs.setNewer(obj.get("newer").getAsBoolean());
        cs.setOlder(obj.get("older").getAsBoolean());
    }
    return cs;
}

From source file:com.cybozu.kintone.database.JsonParser.java

License:Apache License

/**
 * Reads and parses each comment element.
 * @param elem/*from w w  w.  j ava2 s.  com*/
 *            a json element represents a comment object
 * @return the comment object created
 * @throws IOException
 */
private Comment readComment(JsonElement elem) throws IOException {

    Comment comment = null;
    Gson gson = new Gson();

    if (elem.isJsonObject()) {
        JsonObject obj = elem.getAsJsonObject();
        long id = obj.get("id").getAsLong();
        String text = obj.get("text").getAsString();
        Date createdAt = getDateTime(obj.get("createdAt").getAsString());

        Type userElementType = new TypeToken<UserDto>() {
        }.getType();

        UserDto user = gson.fromJson(obj.getAsJsonObject("creator"), userElementType);

        Type mentionsElementType = new TypeToken<Collection<MentionDto>>() {
        }.getType();

        List<MentionDto> mentions = gson.fromJson(obj.getAsJsonArray("mentions"), mentionsElementType);

        comment = new Comment(id, text, createdAt, user, mentions);
    }

    return comment;
}

From source file:com.daa.verifier.Models.Verifier.java

/**
 * Turns a revocation list as JSON object into a set of big integers
 * @param json the revocation list as a JSON object
 * @param curve the curve used//from  w  w w .j av  a  2  s .  co  m
 * @return the revocation list as a Set<BigInteger>
 */
public static Set<BigInteger> revocationListFromJson(String json, BNCurve curve) {
    Base64.Decoder decoder = Base64.getUrlDecoder();

    JsonArray object = new JsonParser().parse(json).getAsJsonObject().getAsJsonArray(JSON_REVOCATION_LIST);
    Set<BigInteger> rl = new HashSet<BigInteger>(object.size());
    for (JsonElement element : object) {
        rl.add(curve.bigIntegerFromB(
                decoder.decode(element.getAsJsonObject().get(JSON_REVOCATION_LIST_ENTRY).getAsString())));
    }
    return rl;
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.FacadeService.java

License:Apache License

/**
 * ?360?./* w w w  .  j a v a  2  s .c  o  m*/
 *
 * @param jobName ??
 * @return 360??
 */
public Collection<TaskFullViewInfo> getTaskFullViewInfo(final String jobName) {
    Optional<CloudJobConfiguration> cloudJobConfigurationOptional = this.load(jobName);
    CloudJobConfiguration cloudJobConfiguration;
    String appName = null;
    CloudJobExecutionType cloudJobExecutionType = null;
    if (cloudJobConfigurationOptional.isPresent()) {
        cloudJobConfiguration = cloudJobConfigurationOptional.get();
        appName = cloudJobConfiguration.getAppName();
        cloudJobExecutionType = cloudJobConfiguration.getJobExecutionType();
    }
    Collection<TaskContext> runningTasks = this.getJobRunningTasks(jobName);
    Collection<FailoverTaskInfo> failoverTasks = this.getJobFailoverTasks(jobName);
    Collection<TaskFullViewInfo> result = Lists.newArrayList();
    if (runningTasks.size() > 0) {
        for (TaskContext each : runningTasks) {
            String statusInfo = RUNNING_STATUS;
            if (null != cloudJobExecutionType && cloudJobExecutionType.equals(CloudJobExecutionType.DAEMON)
                    && !this.getRunningTaskInZookeeper(each.getId())) {
                statusInfo = RUNNING_STATUS_COMMENT;
            }
            String taskId = each.getId();
            result.add(new TaskFullViewInfo(taskId, this.getHostNameByTaskId(taskId), statusInfo,
                    mesosStateService.getMesosSandbox(each.getId())));
        }
    }
    if (failoverTasks.size() > 0) {
        JsonArray slaves = mesosSlaveService.findAllSlaves();
        for (FailoverTaskInfo each : failoverTasks) {
            final TaskContext taskContext = TaskContext.from(each.getOriginalTaskId());
            String serverIp = "";
            Optional<JsonElement> slaveOptional = Iterators.tryFind(slaves.iterator(),
                    new Predicate<JsonElement>() {
                        @Override
                        public boolean apply(final JsonElement input) {
                            return input.getAsJsonObject().get("id").getAsString()
                                    .equals(taskContext.getSlaveId());
                        }
                    });
            if (slaveOptional.isPresent()) {
                serverIp = slaveOptional.get().getAsJsonObject().get("hostname").getAsString();
            }
            result.add(new TaskFullViewInfo(taskContext.getId(), serverIp, FAILOVER_STATUS,
                    mesosStateService.getMesosSandbox(each.getOriginalTaskId())));
        }
    }
    return result;
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java

License:Apache License

/**
 * ?slaves.//from   w w w .  j ava2 s .  c o m
 * 
 * @return jsonArray
 */
public JsonArray findAllSlaves() {
    MesosEndpointService mesosEndpointService = MesosEndpointService.getInstance();
    Optional<JsonObject> jsonObject = mesosEndpointService.slaves(JsonObject.class);
    if (!jsonObject.isPresent()) {
        return new JsonArray();
    }
    JsonArray originalSlaves = jsonObject.get().getAsJsonArray("slaves");
    JsonArray result = new JsonArray();
    for (JsonElement each : originalSlaves) {
        result.add(buildSlave(each.getAsJsonObject()));
    }
    return result;
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java

License:Apache License

/**
 * ??roleNameslaves.//from   w w  w . j  av a 2  s.co m
 * 
 * @return jsonArray
 */
public JsonArray findSlavesContainsRole(final String roleName) {
    JsonArray result = new JsonArray();
    for (JsonElement eachSlave : findAllSlaves()) {
        for (JsonElement eachRole : eachSlave.getAsJsonObject().getAsJsonArray("roles")) {
            if (eachRole.getAsJsonObject().get("role_name").getAsString().equalsIgnoreCase(roleName)) {
                result.add(eachSlave);
                break;
            }
        }
    }
    return result;
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java

License:Apache License

private void fillReservedResources(final Map<String, JsonObject> roleMap,
        final JsonObject reservedResourcesFull) {
    if (reservedResourcesFull.isJsonNull()) {
        return;//  w w w .j a va 2  s . com
    }
    for (Map.Entry<String, JsonElement> each : reservedResourcesFull.entrySet()) {
        for (JsonElement eachResource : each.getValue().getAsJsonArray()) {
            filledResource(roleMap, eachResource.getAsJsonObject(), "reserved_resources");
        }
    }
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java

License:Apache License

private void fillUsedResources(final Map<String, JsonObject> roleMap, final JsonArray usedResourcesFull) {
    if (usedResourcesFull.isJsonNull()) {
        return;//from   w  ww  .  j a va 2s  . co  m
    }
    for (JsonElement each : usedResourcesFull) {
        filledResource(roleMap, each.getAsJsonObject(), "used_resources");
    }
}

From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java

License:Apache License

private void fillOfferedResources(final Map<String, JsonObject> roleMap, final JsonArray offeredResourcesFull) {
    if (offeredResourcesFull.isJsonNull()) {
        return;/*from   w w w  .  j  a  va2  s . com*/
    }
    for (JsonElement each : offeredResourcesFull) {
        filledResource(roleMap, each.getAsJsonObject(), "offered_resources");
    }
}