Example usage for com.google.gson JsonObject add

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

Introduction

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

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:bisq.desktop.main.dao.governance.result.VoteResultView.java

License:Open Source License

private JsonElement getVotingHistoryJson() {
    JsonArray cyclesArray = new JsonArray();

    sortedCycleListItemList.sorted(Comparator.comparing(CycleListItem::getCycleStartTime))
            .forEach(cycleListItem -> {
                JsonObject cycleJson = new JsonObject();
                // No domain data, taken from UI model
                // TODO move the data structure needed for UI to core and use as pure domain model and use that here
                cycleJson.addProperty("cycleIndex", cycleListItem.getCycleIndex());
                cycleJson.addProperty("cycleDateTime", cycleListItem.getCycleDateTime(false));
                cycleJson.addProperty("votesCount", cycleListItem.getNumVotesAsString());
                cycleJson.addProperty("voteWeight", cycleListItem.getMeritAndStake());
                cycleJson.addProperty("issuance", cycleListItem.getIssuance());
                cycleJson.addProperty("startTime", cycleListItem.getCycleStartTime());
                cycleJson.addProperty("totalAcceptedVotes",
                        cycleListItem.getResultsOfCycle().getNumAcceptedVotes());
                cycleJson.addProperty("totalRejectedVotes",
                        cycleListItem.getResultsOfCycle().getNumRejectedVotes());

                JsonArray proposalsArray = new JsonArray();
                List<EvaluatedProposal> evaluatedProposals = cycleListItem.getResultsOfCycle()
                        .getEvaluatedProposals();
                evaluatedProposals.sort(Comparator.comparingLong(o -> o.getProposal().getCreationDate()));

                evaluatedProposals.forEach(evaluatedProp -> {
                    JsonObject proposalJson = new JsonObject();
                    proposalJson.addProperty("isAccepted",
                            evaluatedProp.isAccepted() ? "Accepted" : "Rejected");

                    // Proposal
                    Proposal proposal = evaluatedProp.getProposal();
                    proposalJson.addProperty("proposal.name", proposal.getName());
                    proposalJson.addProperty("proposal.link", proposal.getLink());
                    proposalJson.addProperty("proposal.version", proposal.getVersion());
                    proposalJson.addProperty("proposal.creationDate", proposal.getCreationDate());
                    proposalJson.addProperty("proposal.txId", proposal.getTxId());
                    proposalJson.addProperty("proposal.txType", proposal.getTxType().name());
                    proposalJson.addProperty("proposal.quorumParam", proposal.getQuorumParam().name());
                    proposalJson.addProperty("proposal.thresholdParam", proposal.getThresholdParam().name());
                    proposalJson.addProperty("proposal.proposalType", proposal.getType().name());

                    if (proposal.getExtraDataMap() != null)
                        proposalJson.addProperty("proposal.extraDataMap",
                                proposal.getExtraDataMap().toString());

                    switch (proposal.getType()) {
                    case UNDEFINED:
                        break;
                    case COMPENSATION_REQUEST:
                        CompensationProposal compensationProposal = (CompensationProposal) proposal;
                        proposalJson.addProperty("proposal.requestedBsq",
                                compensationProposal.getRequestedBsq().getValue());
                        proposalJson.addProperty("proposal.bsqAddress", compensationProposal.getBsqAddress());
                        break;
                    case REIMBURSEMENT_REQUEST:
                        ReimbursementProposal reimbursementProposal = (ReimbursementProposal) proposal;
                        proposalJson.addProperty("proposal.requestedBsq",
                                reimbursementProposal.getRequestedBsq().getValue());
                        proposalJson.addProperty("proposal.bsqAddress", reimbursementProposal.getBsqAddress());
                        break;
                    case CHANGE_PARAM:
                        ChangeParamProposal changeParamProposal = (ChangeParamProposal) proposal;
                        Param param = changeParamProposal.getParam();
                        proposalJson.addProperty("proposal.param", param.name());
                        proposalJson.addProperty("proposal.param.defaultValue", param.getDefaultValue());
                        proposalJson.addProperty("proposal.param.type", param.getParamType().name());
                        proposalJson.addProperty("proposal.param.maxDecrease", param.getMaxDecrease());
                        proposalJson.addProperty("proposal.param.maxIncrease", param.getMaxIncrease());
                        proposalJson.addProperty("proposal.paramValue", changeParamProposal.getParamValue());
                        break;
                    case BONDED_ROLE:
                        RoleProposal roleProposal = (RoleProposal) proposal;
                        Role role = roleProposal.getRole();
                        proposalJson.addProperty("proposal.requiredBondUnit",
                                roleProposal.getRequiredBondUnit());
                        proposalJson.addProperty("proposal.unlockTime", roleProposal.getUnlockTime());
                        proposalJson.addProperty("proposal.role.uid", role.getUid());
                        proposalJson.addProperty("proposal.role.name", role.getName());
                        proposalJson.addProperty("proposal.role.link", role.getLink());
                        BondedRoleType bondedRoleType = role.getBondedRoleType();
                        proposalJson.addProperty("proposal.bondedRoleType", bondedRoleType.name());
                        // bondedRoleType enum must not change anyway so we don't print it
                        break;
                    case CONFISCATE_BOND:
                        ConfiscateBondProposal confiscateBondProposal = (ConfiscateBondProposal) proposal;
                        proposalJson.addProperty("proposal.lockupTxId", confiscateBondProposal.getLockupTxId());
                        break;
                    case GENERIC:
                        // No extra fields
                        break;
                    case REMOVE_ASSET:
                        RemoveAssetProposal removeAssetProposal = (RemoveAssetProposal) proposal;
                        proposalJson.addProperty("proposal.tickerSymbol",
                                removeAssetProposal.getTickerSymbol());
                        break;
                    }//from   www.j  a v a  2  s  .  c  om

                    ProposalVoteResult proposalVoteResult = evaluatedProp.getProposalVoteResult();
                    proposalJson.addProperty("stakeOfAcceptedVotes",
                            proposalVoteResult.getStakeOfAcceptedVotes());
                    proposalJson.addProperty("stakeOfRejectedVotes",
                            proposalVoteResult.getStakeOfRejectedVotes());
                    proposalJson.addProperty("numAcceptedVotes", proposalVoteResult.getNumAcceptedVotes());
                    proposalJson.addProperty("numRejectedVotes", proposalVoteResult.getNumRejectedVotes());
                    proposalJson.addProperty("numIgnoredVotes", proposalVoteResult.getNumIgnoredVotes());
                    proposalJson.addProperty("numActiveVotes", proposalVoteResult.getNumActiveVotes());
                    proposalJson.addProperty("quorum", proposalVoteResult.getQuorum());
                    proposalJson.addProperty("threshold", proposalVoteResult.getThreshold());

                    // Not part of pure domain data, but useful to add here
                    // required quorum and threshold for cycle for proposal type
                    proposalJson.addProperty("requiredQuorum",
                            proposalService.getRequiredQuorum(proposal).value);
                    proposalJson.addProperty("requiredThreshold",
                            proposalService.getRequiredThreshold(proposal));

                    // TODO provide better domain object as now we loop inside the loop. Use lookup map instead....
                    JsonArray votesArray = new JsonArray();
                    evaluatedProposals.stream()
                            .filter(evaluatedProposal -> evaluatedProposal.getProposal().equals(proposal))
                            .forEach(evaluatedProposal -> {
                                List<DecryptedBallotsWithMerits> decryptedVotesForCycle = cycleListItem
                                        .getResultsOfCycle().getDecryptedVotesForCycle();
                                // Make sure the votes are sorted so we can easier compare json files from different users
                                decryptedVotesForCycle.sort(
                                        Comparator.comparing(DecryptedBallotsWithMerits::getBlindVoteTxId));
                                decryptedVotesForCycle.forEach(decryptedBallotsWithMerits -> {
                                    JsonObject voteJson = new JsonObject();
                                    // Domain data of decryptedBallotsWithMerits
                                    voteJson.addProperty("hashOfBlindVoteList", Utilities.bytesAsHexString(
                                            decryptedBallotsWithMerits.getHashOfBlindVoteList()));
                                    voteJson.addProperty("blindVoteTxId",
                                            decryptedBallotsWithMerits.getBlindVoteTxId());
                                    voteJson.addProperty("voteRevealTxId",
                                            decryptedBallotsWithMerits.getVoteRevealTxId());
                                    voteJson.addProperty("stake", decryptedBallotsWithMerits.getStake());

                                    voteJson.addProperty("voteWeight",
                                            decryptedBallotsWithMerits.getMerit(daoStateService));
                                    String voteResult = decryptedBallotsWithMerits
                                            .getVote(evaluatedProp.getProposalTxId())
                                            .map(vote -> vote.isAccepted() ? "Accepted" : "Rejected")
                                            .orElse("Ignored");
                                    voteJson.addProperty("vote", voteResult);
                                    votesArray.add(voteJson);
                                });
                            });

                    proposalJson.addProperty("numberOfVotes", votesArray.size());
                    proposalJson.add("votes", votesArray);

                    proposalsArray.add(proposalJson);
                });
                cycleJson.addProperty("numberOfProposals", proposalsArray.size());
                cycleJson.add("proposals", proposalsArray);
                cyclesArray.add(cycleJson);
            });
    return cyclesArray;
}

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

public JsonElement encodeBoard() {
    final JsonObject boardState = new JsonObject();
    final JsonObject meta = new JsonObject();
    final JsonObject data = new JsonObject();
    final Board board = this.context.board();
    final int rows = board.rows();
    final int columns = board.columns();
    meta.addProperty("rows", rows);
    meta.addProperty("columns", columns);
    final Set<Colors> colors = board.getColors();
    for (final Colors color : colors) {
        final JsonArray jsonArray = new JsonArray();
        for (final IPosition position : board.getSelves(color))
            jsonArray.add(new JsonPrimitive(columns * position.row() + position.column() % rows)); // TODO extract method
        data.add(color.toString(), jsonArray);
    }//from   w ww .jav  a2  s  . c  om
    boardState.add("dimension", meta);
    boardState.add("cells", data);
    return boardState;
}

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

public JsonElement encodePieces() {
    final JsonObject data = new JsonObject();
    final Context context = this.context;
    for (final Entry<Colors, Side> sideEntry : context.sides()) {
        final Colors color = sideEntry.getKey();
        final Side side = sideEntry.getValue();
        int bits = 1;
        final Pieces remainingPieces = side.remainingPieces();
        for (final Entry<Polyomino, Integer> entry : remainingPieces) {
            if (entry.getKey().ordinal() == 0)
                continue;
            bits = bits << 1 | entry.getValue();
        }//from ww w .ja  va 2 s.  c  om
        data.add(color.toString(), new JsonPrimitive(bits));
    }
    return data;
}

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

public JsonElement encodeOptions() {
    final Options options = this.context.options();
    final Set<Entry<IPosition, Map<Polyomino, List<Set<IPosition>>>>> byLight = options.byLight().entrySet();
    final JsonObject jsonObject = new JsonObject();
    for (final Entry<IPosition, Map<Polyomino, List<Set<IPosition>>>> entry : byLight) {
        final String light = this.gson.toJson(entry.getKey(), IPosition.class);
        final JsonElement positionsByPolyominos = this.gson.toJsonTree(entry.getValue(),
                POSITIONS_BY_POLYOMINO);
        jsonObject.add(light, positionsByPolyominos);
    }//from w  w w  . j a  v  a 2s  . c o m
    return jsonObject;
}

From source file:blockplus.exports.ContextRepresentation.java

License:Open Source License

@Override
public String toString() {
    final JsonObject data = new JsonObject();
    data.addProperty("color", this.context.side().toString());
    data.addProperty("isTerminal", this.context.isTerminal());
    data.add("board", this.encodeBoard());
    data.add("pieces", this.encodePieces());
    data.add("options", this.encodeOptions());
    return data.toString();
}

From source file:blockplus.transport.events.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getClass().getSimpleName());
    final JsonObject data = new JsonObject();
    data.addProperty("name", this.getName());
    data.addProperty("game", this.getGame().toString());
    data.addProperty("io", this.getEndpoint().toString());
    jsonObject.add("data", data);
    return jsonObject.toString();
}

From source file:blockplus.transport.messages.Client.java

License:Open Source License

@Override
public String toString() {
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("type", this.getType());
    jsonObject.add("data", this.getData());
    return jsonObject.toString();
}

From source file:blockplus.transport.messages.MoveSubmit.java

License:Open Source License

@Override
public JsonObject getData() {
    final JsonObject data = new JsonObject();
    data.add("positions", this.getPositions());
    return data;//  www.  j  a v  a2s .co m
}

From source file:blusunrize.immersiveengineering.client.models.ModelData.java

public static ModelData fromJson(JsonObject customData, Collection<String> knownKeys, String modelKey,
        ImmutableMap<String, String> texReplacements) {
    String baseLocStr = customData.get(modelKey).getAsString();
    ResourceLocation baseLoc = new ResourceLocation(baseLocStr);
    JsonObject customBase = new JsonObject();
    if (customData.has("custom"))
        customBase = customData.get("custom").getAsJsonObject();
    for (Entry<String, JsonElement> e : customData.entrySet())
        if (!knownKeys.contains(e.getKey()) && !customBase.has(e.getKey()))
            customBase.add(e.getKey(), e.getValue());
    if (customData.has("textures")) {
        JsonObject obj = customData.get("textures").getAsJsonObject();
        ImmutableMap.Builder<String, String> b = ImmutableMap.builder();
        b.putAll(texReplacements);//  w w  w.  java2 s.  c o  m
        b.putAll(asMap(obj, true));
        texReplacements = b.build();
    }
    return new ModelData(baseLoc, customBase, texReplacements);
}

From source file:blusunrize.immersiveengineering.client.models.ModelData.java

public static JsonObject asJsonObject(Map<String, String> map) {
    JsonObject ret = new JsonObject();
    JsonParser parser = new JsonParser();
    for (Entry<String, String> e : map.entrySet()) {
        ret.add(e.getKey(), parser.parse(e.getValue()));
    }/*from  w w w  .  ja va  2 s .c om*/
    return ret;
}