Example usage for com.google.gson JsonObject getAsJsonArray

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

Introduction

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

Prototype

public JsonArray getAsJsonArray(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonArray.

Usage

From source file:bmw.assigment.BMWAssigment.java

/**
 * Recursive method to populate the data extracted from the Json file
 * /*  w ww  .ja va2  s . com*/
 * @param employee
 * @return 
 */
public static AbstractEmployee populateSubordinate(JsonObject employee) {

    AbstractEmployee emp = null;

    if (employee.has("_subordinates")) {
        emp = new Manager(employee.get("_name").getAsString(), employee.get("_salary").getAsLong());
        JsonArray array = employee.getAsJsonArray("_subordinates");
        for (int i = 0; i < array.size(); i++) {
            emp.addEmployee(populateSubordinate(array.get(i).getAsJsonObject()));
        }
    } else {
        emp = new Employee(employee.get("_name").getAsString(), employee.get("_salary").getAsLong());
    }

    return emp;
}

From source file:brooklyn.entity.mesos.framework.MesosFrameworkImpl.java

License:Apache License

public List<String> scanTasks(JsonArray frameworks) {
    String frameworkId = sensors().get(FRAMEWORK_ID);
    Entity mesosCluster = sensors().get(MESOS_CLUSTER);
    LOG.debug("Periodic scanning of framework tasks: frameworkId={}, mesosCluster={}", frameworkId,
            mesosCluster);//from w  w  w  . ja v a2  s. co m

    for (int i = 0; i < frameworks.size(); i++) {
        JsonObject framework = frameworks.get(i).getAsJsonObject();
        if (frameworkId.equals(framework.get("id").getAsString())) {
            JsonArray completed = framework.getAsJsonArray("completed_tasks");
            JsonArray tasks = framework.getAsJsonArray("tasks");
            sensors().set(MESOS_COMPLETED_TASKS, completed.size());
            sensors().set(MESOS_RUNNING_TASKS, tasks.size());

            List<String> taskNames = MutableList.<String>of();
            for (int j = 0; j < tasks.size(); j++) {
                JsonObject json = tasks.get(j).getAsJsonObject();
                String id = json.get("id").getAsString();
                String name = json.get("name").getAsString();
                String state = json.get("state").getAsString();

                Optional<Entity> taskEntity = Iterables.tryFind(sensors().get(FRAMEWORK_TASKS).getMembers(),
                        Predicates.compose(Predicates.equalTo(id),
                                EntityFunctions.attribute(MesosTask.TASK_ID)));
                MesosTask task = null;
                if (taskEntity.isPresent()) {
                    // Only interested in tasks for our own use of Marathon.
                    // Tasks that other people create we'll ignore.
                    task = (MesosTask) taskEntity.get();
                }
                if (task != null) {
                    taskNames.add(name);
                    task.sensors().set(MesosTask.TASK_ID, id);
                    task.sensors().set(MesosTask.TASK_STATE, state);
                }
            }

            Set<String> taskNamesSet = Sets.newHashSet(taskNames);
            for (Entity member : ImmutableList.copyOf(getTaskCluster().getMembers())) {
                final String name = member.sensors().get(MesosTask.TASK_NAME);
                if (name != null) {
                    boolean found = taskNamesSet.contains(name);
                    if (found)
                        continue;
                }

                // Stop and then remove the task as it is no longer running, unless ON_FIRE
                //
                // TODO Aled worries about this: if task has gone, then MarathonTask polling
                // the task's URL will fail, setting the serviceUp to false, setting it 
                // on-fire. What will ever remove the task on a graceful shutdown of a container?
                // Or is that handled elsewhere?
                Lifecycle state = member.sensors().get(Attributes.SERVICE_STATE_ACTUAL);
                if (Lifecycle.ON_FIRE.equals(state) || Lifecycle.STARTING.equals(state)) {
                    continue;
                } else if (Lifecycle.STOPPING.equals(state) || Lifecycle.STOPPED.equals(state)) {
                    getTaskCluster().removeMember(member);
                    getTaskCluster().removeChild(member);
                    Entities.unmanage(member);
                } else {
                    ServiceStateLogic.setExpectedState(member, Lifecycle.STOPPING);
                }
            }
            return taskNames;
        }
    }
    // not found
    return null;
}

From source file:ccm.pay2spawn.configurator.Configurator.java

License:Open Source License

private void setupModels() {
    mainTable.setModel(new AbstractTableModel() {
        @Override/*from www  . j a v a 2s  .  c  o m*/
        public int getRowCount() {
            return rootArray.size();
        }

        @Override
        public int getColumnCount() {
            return COLUMN_NAMES.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            JsonObject jsonObject = rootArray.get(rowIndex).getAsJsonObject();
            if (!jsonObject.has(COLUMN_KEYS[columnIndex]))
                return "";
            switch (columnIndex) {
            default:
                return jsonObject.get(COLUMN_KEYS[columnIndex]).getAsString();
            case 4:
                HashSet<String> types = new HashSet<>();
                for (JsonElement element : jsonObject.getAsJsonArray(COLUMN_KEYS[columnIndex]))
                    types.add(element.getAsJsonObject().get("type").getAsString());
                return JOINER_COMMA_SPACE.join(types);
            }
        }

        @Override
        public String getColumnName(int column) {
            return COLUMN_NAMES[column];
        }
    });

    typeList.setModel(new AbstractListModel<String>() {
        final ArrayList<String> names = TypeRegistry.getNames();

        @Override
        public int getSize() {
            return names.size();
        }

        @Override
        public String getElementAt(int index) {
            return names.get(index);
        }
    });

    rewards.setModel(new AbstractListModel<String>() {
        @Override
        public int getSize() {
            return rewardData == null ? 0 : rewardData.size();
        }

        @Override
        public String getElementAt(int index) {
            if (rewardData.get(index).getAsJsonObject().has("type"))
                return rewardData.get(index).getAsJsonObject().getAsJsonPrimitive("type").getAsString();
            else
                return "ERROR IN CONFIG - No type for reward " + index;
        }
    });
}

From source file:ccm.pay2spawn.permissions.Group.java

License:Open Source License

public Group(JsonObject jsonObject) {
    name = jsonObject.get("name").getAsString();
    if (jsonObject.has("parent"))
        parent = jsonObject.get("parent").getAsString();
    for (JsonElement node : jsonObject.getAsJsonArray("nodes"))
        nodes.add(new Node(node.getAsString()));
}

From source file:ccm.pay2spawn.permissions.PermissionsDB.java

License:Open Source License

public void load() throws IOException {
    File file = getFile();// w  w  w . j  a  v  a 2  s .c  o  m
    if (file.exists()) {
        JsonObject rootObject = JSON_PARSER.parse(new FileReader(file)).getAsJsonObject();

        for (JsonElement element : rootObject.getAsJsonArray("players")) {
            Player player = new Player(element.getAsJsonObject());
            playerDB.put(player.getName(), player);
        }

        for (JsonElement element : rootObject.getAsJsonArray("groups")) {
            Group group = new Group(element.getAsJsonObject());
            groupDB.put(group.getName(), group);
        }
    } else {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();
        JsonObject rootObject = new JsonObject();
        rootObject.add("players", new JsonArray());
        rootObject.add("groups", new JsonArray());
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(GSON.toJson(rootObject));
        bw.close();
    }
}

From source file:ccm.pay2spawn.permissions.Player.java

License:Open Source License

public Player(JsonObject jsonObject) {
    name = jsonObject.get("name").getAsString();
    if (jsonObject.has("groups"))
        for (JsonElement groupName : jsonObject.getAsJsonArray("groups"))
            groups.add(groupName.getAsString());
    if (jsonObject.has("overrideNodes"))
        for (JsonElement node : jsonObject.getAsJsonArray("overrideNodes"))
            overrideNodes.add(new Node(node.getAsString()));
}

From source file:ccm.pay2spawn.util.Reward.java

License:Open Source License

public Reward(JsonObject json) {
    name = json.get("name").getAsString();
    amount = json.get("amount").getAsDouble();
    message = Helper.formatColors(json.get("message").getAsString());
    rewards = json.getAsJsonArray("rewards");
    try {/* w w  w .jav a2 s. co  m*/
        countdown = json.get("countdown").getAsInt();
    } catch (Exception e) {
        countdown = 0;
    }
    /**
     * To try and catch errors in the config file ASAP
     */
    try {
        JsonNBTHelper.parseJSON(rewards);
    } catch (Exception e) {
        Pay2Spawn.getLogger().warn("ERROR TYPE 2: Error in reward " + name + "'s NBT data.");
        Pay2Spawn.getLogger().warn(rewards.toString());
        throw e;
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void getLabels(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(LABEL);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);// w ww.  j  a  v  a2 s  .co  m
        JsonObject o = e.getAsJsonObject();
        poi.addLabel(getPOITermType(o));
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void getDescription(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(DESCRIPTION);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);/*  w  ww . ja va  2  s  .  com*/
        JsonObject o = e.getAsJsonObject();
        poi.addDescription(getPOIBaseType(o));
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private void getCategories(POI poi, JsonObject jObject) {
    JsonArray jArray = jObject.getAsJsonArray(CATEGORY);
    for (int i = 0; i < jArray.size(); i++) {
        JsonElement e = jArray.get(i);/*ww  w .j  a v a 2s.  c o  m*/
        JsonObject o = e.getAsJsonObject();
        poi.addCategory(getPOITermType(o));
    }
}