Example usage for com.google.gson JsonArray JsonArray

List of usage examples for com.google.gson JsonArray JsonArray

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:catalog.CloudantUtil.java

License:Apache License

public static JsonArray createDocumentArray(int numDocs) {
    // Array of docs for bulk
    JsonArray bulkDocs = new JsonArray();
    for (int i = 1; i <= numDocs; i++) {
        JsonObject cloudantDoc = new JsonObject();
        String now = new Date().toString();
        cloudantDoc.addProperty("_id", CloudantUtil.DOC_ID + i);
        cloudantDoc.addProperty("date", now);
        bulkDocs.add(cloudantDoc);/* w  w w. j  a  v  a  2s .  c  om*/
    }
    return bulkDocs;
}

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

License:Open Source License

private void setupListeners() {
    typeList.addMouseListener(new MouseAdapter() {
        @Override//ww  w.  j a  v  a2s.c  om
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                TypeRegistry.getByName(typeList.getSelectedValue()).openNewGui(-1, new JsonObject());
            }
        }
    });
    rewards.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int id = rewards.getSelectedIndex();
                TypeRegistry.getByName(rewards.getSelectedValue()).openNewGui(id,
                        rewardData.get(id).getAsJsonObject().getAsJsonObject("data"));
            }
        }
    });
    rewards.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            deleteSelectedRewardButton.setEnabled(!rewards.isSelectionEmpty());
            duplicateSelectedRewardButton.setEnabled(!rewards.isSelectionEmpty());
        }
    });
    helpMeWithRandomizationButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Help.init();
        }
    });
    mainTable.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                currentlyEditingID = mainTable.getSelectedRow();
                currentlyEditingData = rootArray.get(currentlyEditingID).getAsJsonObject();
                if (currentlyEditingData.has("rewards"))
                    rewardData = JsonNBTHelper.cloneJSON(currentlyEditingData.getAsJsonArray("rewards"))
                            .getAsJsonArray();
                else
                    rewardData = new JsonArray();
                rewards.updateUI();
                rewards.clearSelection();
                typeList.clearSelection();
                saveOverOldGroupButton.setEnabled(true);
                removeGroupButton.setEnabled(true);

                tabbedPane1.setSelectedIndex(1);
                nameField.setText(currentlyEditingData.getAsJsonPrimitive(COLUMN_KEYS[0]).getAsString());
                amountField.setText(currentlyEditingData.getAsJsonPrimitive(COLUMN_KEYS[1]).getAsString());
                messageField.setText(currentlyEditingData.getAsJsonPrimitive(COLUMN_KEYS[2]).getAsString());
                countdownTextField.setText(currentlyEditingData.has(COLUMN_KEYS[3])
                        ? currentlyEditingData.getAsJsonPrimitive(COLUMN_KEYS[3]).getAsString()
                        : "");
            }
        }
    });
    clearButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            clear();
        }
    });
    saveOverOldGroupButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (saveEdits()) {
                JsonArray newRoot = new JsonArray();
                for (int i = 0; i < rootArray.size(); i++)
                    if (i != currentlyEditingID)
                        newRoot.add(rootArray.get(i));

                rootArray = newRoot;
                rootArray.add(currentlyEditingData);
                mainTable.updateUI();
                saveMainJsonToFile();
                ColumnsAutoSizer.sizeColumnsToFit(mainTable, 20);
            }
        }
    });
    saveAsNewGroupButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (saveEdits()) {
                rootArray.add(currentlyEditingData);
                currentlyEditingID = rootArray.size() - 1;
                saveOverOldGroupButton.setEnabled(true);
                removeGroupButton.setEnabled(true);
                mainTable.updateUI();
                saveMainJsonToFile();
                ColumnsAutoSizer.sizeColumnsToFit(mainTable, 20);
            }
        }
    });
    removeGroupButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JsonArray newRoot = new JsonArray();
            for (int i = 0; i < rootArray.size(); i++)
                if (i != currentlyEditingID)
                    newRoot.add(rootArray.get(i));

            rootArray = newRoot;
            clear();
            tabbedPane1.setSelectedIndex(0);
            saveMainJsonToFile();
        }
    });
    duplicateSelectedRewardButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int toDuplicate = rewards.getSelectedIndex();
            rewardData.add(JsonNBTHelper.cloneJSON(rewardData.get(toDuplicate)));

            rewards.updateUI();
        }
    });
    deleteSelectedRewardButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int toRemove = rewards.getSelectedIndex();
            JsonArray newRewardData = new JsonArray();
            for (int i = 0; i < rewardData.size(); i++)
                if (i != toRemove)
                    newRewardData.add(rewardData.get(i));
            rewardData = newRewardData;

            rewards.clearSelection();
            rewards.updateUI();
        }
    });
    makeNiceHtmlPageButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                HTMLGenerator.generate();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}

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

License:Open Source License

public void clear() {
    currentlyEditingID = -1;/*ww w  .  j a  v a  2s. c  o  m*/
    currentlyEditingData = new JsonObject();
    rewardData = new JsonArray();
    rewards.updateUI();
    rewards.clearSelection();
    typeList.clearSelection();
    saveOverOldGroupButton.setEnabled(false);
    removeGroupButton.setEnabled(false);

    nameField.setText("");
    amountField.setText("");
    messageField.setText("");
    mainTable.updateUI();
    countdownTextField.setText("");

    nameLabel.setForeground(Color.black);
    amountLabel.setForeground(Color.black);
}

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

License:Open Source License

public JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    root.addProperty("parent", getParent());

    JsonArray nodes = new JsonArray();
    for (Node node : this.nodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("nodes", nodes);

    return root;//from www.ja  va  2s .  c om
}

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

License:Open Source License

public void save() {
    try {/*w  w  w .jav a  2s .  c  o  m*/
        File file = getFile();
        if (!file.exists()) //noinspection ResultOfMethodCallIgnored
            file.createNewFile();
        JsonObject rootObject = new JsonObject();

        JsonArray players = new JsonArray();
        for (Player player : playerDB.values())
            players.add(player.toJson());
        rootObject.add("players", players);

        JsonArray groups = new JsonArray();
        for (Group group : groupDB.values())
            groups.add(group.toJson());
        rootObject.add("groups", groups);

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(GSON.toJson(rootObject));
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public void load() throws IOException {
    File file = getFile();// ww  w  .j a va2 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 JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    JsonArray groups = new JsonArray();
    for (String group : this.getGroups())
        groups.add(new JsonPrimitive(group));
    root.add("groups", groups);

    JsonArray nodes = new JsonArray();
    for (Node node : this.overrideNodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("overrides", nodes);

    return root;/*from  w  w w  .  j  a  v a  2s . c o m*/
}

From source file:ccm.pay2spawn.random.RndColors.java

License:Open Source License

@Override
public String solverRandom(int type, String value) {
    Matcher mRGB = PATTERN.matcher(value);
    if (type == INT_ARRAY) {
        JsonArray colors = new JsonArray();
        mRGB.find();/*from ww w .  ja v a  2s  .c o  m*/
        for (int i = 0; i < Integer.parseInt(mRGB.group(1)); i++)
            colors.add(new JsonPrimitive(
                    (RANDOM.nextInt(200) << 16) + (RANDOM.nextInt(200) << 8) + RANDOM.nextInt(200)));
        return mRGB.replaceFirst(colors.toString());
    } else {
        return mRGB.replaceFirst(
                "" + ((RANDOM.nextInt(200) << 16) + (RANDOM.nextInt(200) << 8) + RANDOM.nextInt(200)));
    }
}

From source file:ccm.pay2spawn.types.guis.FireworksTypeGui.java

License:Open Source License

@Override
public void readJson() {
    amountTextField.setText(readValue(AMOUNT_KEY, data));
    radiusTextField.setText(readValue(RADIUS_KEY, data));
    HTMLTextField.setText(readValue(CUSTOMHTML, data));

    if (!data.has("tag"))
        data.add("tag", new JsonObject());
    if (!data.getAsJsonObject("tag").has(FIREWORKS_KEY))
        data.getAsJsonObject("tag").add(FIREWORKS_KEY, new JsonObject());
    if (!data.getAsJsonObject("tag").getAsJsonObject(FIREWORKS_KEY).has(EXPLOSIONS_KEY))
        data.getAsJsonObject("tag").getAsJsonObject(FIREWORKS_KEY).add(EXPLOSIONS_KEY, new JsonArray());

    fireworks = data.getAsJsonObject("tag").getAsJsonObject(FIREWORKS_KEY);
    flightMultiplierTextField.setText(readValue(FLIGHT_KEY, fireworks));

    String ride = readValue(RIDETHISMOB_KEY, data);
    dontRidemob.setSelected(ride.equals(FALSE_BYTE) || ride.equals(""));
    rideThisFireworkRadioButton.setSelected(ride.equals(TRUE_BYTE));
    randomlyRideMob.setSelected(ride.startsWith(RANDOM_BOOLEAN));

    explosionList.updateUI();//from w w w.  j a  v  a2 s  .  c  o m

    jsonPane.setText(GSON.toJson(data));
}

From source file:ccm.pay2spawn.types.guis.FireworksTypeGui.java

License:Open Source License

public void callback(int id, JsonObject data) {
    if (id == -1)
        fireworks.getAsJsonArray(EXPLOSIONS_KEY).add(data);
    else {/*from   w  ww . j  av  a 2 s.c o  m*/
        JsonArray newFireworks = new JsonArray();
        for (int i = 0; i < fireworks.getAsJsonArray(EXPLOSIONS_KEY).size(); i++)
            if (i != id)
                newFireworks.add(fireworks.getAsJsonArray(EXPLOSIONS_KEY).get(i));
        newFireworks.add(data);
    }
    readJson();
}