com.hedera.hapi.node.state.token.codec.AccountJsonCodec Maven / Gradle / Ivy
The newest version!
package com.hedera.hapi.node.state.token.codec;
import com.hedera.pbj.runtime.*;
import com.hedera.pbj.runtime.io.*;
import com.hedera.pbj.runtime.io.buffer.*;
import java.io.IOException;
import java.nio.*;
import java.nio.charset.*;
import java.util.*;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import com.hedera.hapi.node.state.token.Account;
import com.hedera.hapi.node.base.*;
import com.hedera.hapi.node.base.codec.*;
import com.hedera.hapi.node.state.token.*;
import com.hedera.hapi.node.state.token.schema.*;
import com.hedera.pbj.runtime.*;
import com.hedera.pbj.runtime.io.buffer.*;
import java.util.*;
import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.node.state.token.schema.AccountSchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;
/**
* JSON Codec for Account model object. Generated based on protobuf schema.
*/
public final class AccountJsonCodec implements JsonCodec {
/** Constant for an unset oneof for staked_id */
public static final OneOf STAKED_ID_UNSET = new OneOf<>(Account.StakedIdOneOfType.UNSET,null);
/**
* Parses a HashObject object from JSON parse tree for object JSONParser.ObjContext.
* Throws an UnknownFieldException wrapped in a ParseException if in strict mode ONLY.
*
* @param root The JSON parsed object tree to parse data from
* @return Parsed HashObject model object or null if data input was null or empty
* @throws ParseException If parsing fails
*/
public @NonNull Account parse(
@Nullable final JSONParser.ObjContext root,
final boolean strictMode,
final int maxDepth) throws ParseException {
if (maxDepth < 0) {
throw new ParseException("Reached maximum allowed depth of nested messages");
}
try {
// -- TEMP STATE FIELDS --------------------------------------
AccountID temp_account_id = null;
Bytes temp_alias = Bytes.EMPTY;
Key temp_key = null;
long temp_expiration_second = 0;
long temp_tinybar_balance = 0;
String temp_memo = "";
boolean temp_deleted = false;
long temp_staked_to_me = 0;
long temp_stake_period_start = 0;
OneOf temp_staked_id = STAKED_ID_UNSET;
boolean temp_decline_reward = false;
boolean temp_receiver_sig_required = false;
TokenID temp_head_token_id = null;
NftID temp_head_nft_id = null;
long temp_head_nft_serial_number = 0;
long temp_number_owned_nfts = 0;
int temp_max_auto_associations = 0;
int temp_used_auto_associations = 0;
int temp_number_associations = 0;
boolean temp_smart_contract = false;
int temp_number_positive_balances = 0;
long temp_ethereum_nonce = 0;
long temp_stake_at_start_of_last_rewarded_period = 0;
AccountID temp_auto_renew_account_id = null;
long temp_auto_renew_seconds = 0;
int temp_contract_kv_pairs_number = 0;
List temp_crypto_allowances = Collections.emptyList();
List temp_approve_for_all_nft_allowances = Collections.emptyList();
List temp_token_allowances = Collections.emptyList();
int temp_number_treasury_titles = 0;
boolean temp_expired_and_pending_removal = false;
Bytes temp_first_contract_storage_key = Bytes.EMPTY;
PendingAirdropId temp_head_pending_airdrop_id = null;
long temp_number_pending_airdrops = 0;
// -- EXTRACT VALUES FROM PARSE TREE ---------------------------------------------
for (JSONParser.PairContext kvPair : root.pair()) {
switch (kvPair.STRING().getText()) {
case "accountId" /* [1] */ : temp_account_id = AccountID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "alias" /* [2] */ : temp_alias = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
case "key" /* [3] */ : temp_key = Key.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "expirationSecond" /* [4] */ : temp_expiration_second = parseLong(kvPair.value()); break;
case "tinybarBalance" /* [5] */ : temp_tinybar_balance = parseLong(kvPair.value()); break;
case "memo" /* [6] */ : temp_memo = unescape(kvPair.value().STRING().getText()); break;
case "deleted" /* [7] */ : temp_deleted = parseBoolean(kvPair.value()); break;
case "stakedToMe" /* [8] */ : temp_staked_to_me = parseLong(kvPair.value()); break;
case "stakePeriodStart" /* [9] */ : temp_stake_period_start = parseLong(kvPair.value()); break;
case "stakedAccountId" /* [10] */ : temp_staked_id = new OneOf<>(
Account.StakedIdOneOfType
.STAKED_ACCOUNT_ID ,
AccountID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1)); break;
case "stakedNodeId" /* [11] */ : temp_staked_id = new OneOf<>(
Account.StakedIdOneOfType
.STAKED_NODE_ID ,
parseLong(kvPair.value())); break;
case "declineReward" /* [12] */ : temp_decline_reward = parseBoolean(kvPair.value()); break;
case "receiverSigRequired" /* [13] */ : temp_receiver_sig_required = parseBoolean(kvPair.value()); break;
case "headTokenId" /* [14] */ : temp_head_token_id = TokenID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "headNftId" /* [15] */ : temp_head_nft_id = NftID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "headNftSerialNumber" /* [16] */ : temp_head_nft_serial_number = parseLong(kvPair.value()); break;
case "numberOwnedNfts" /* [17] */ : temp_number_owned_nfts = parseLong(kvPair.value()); break;
case "maxAutoAssociations" /* [18] */ : temp_max_auto_associations = parseInteger(kvPair.value()); break;
case "usedAutoAssociations" /* [19] */ : temp_used_auto_associations = parseInteger(kvPair.value()); break;
case "numberAssociations" /* [20] */ : temp_number_associations = parseInteger(kvPair.value()); break;
case "smartContract" /* [21] */ : temp_smart_contract = parseBoolean(kvPair.value()); break;
case "numberPositiveBalances" /* [22] */ : temp_number_positive_balances = parseInteger(kvPair.value()); break;
case "ethereumNonce" /* [23] */ : temp_ethereum_nonce = parseLong(kvPair.value()); break;
case "stakeAtStartOfLastRewardedPeriod" /* [24] */ : temp_stake_at_start_of_last_rewarded_period = parseLong(kvPair.value()); break;
case "autoRenewAccountId" /* [25] */ : temp_auto_renew_account_id = AccountID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "autoRenewSeconds" /* [26] */ : temp_auto_renew_seconds = parseLong(kvPair.value()); break;
case "contractKvPairsNumber" /* [27] */ : temp_contract_kv_pairs_number = parseInteger(kvPair.value()); break;
case "cryptoAllowances" /* [28] */ : temp_crypto_allowances = parseObjArray(kvPair.value().arr(), AccountCryptoAllowance.JSON, maxDepth - 1); break;
case "approveForAllNftAllowances" /* [29] */ : temp_approve_for_all_nft_allowances = parseObjArray(kvPair.value().arr(), AccountApprovalForAllAllowance.JSON, maxDepth - 1); break;
case "tokenAllowances" /* [30] */ : temp_token_allowances = parseObjArray(kvPair.value().arr(), AccountFungibleTokenAllowance.JSON, maxDepth - 1); break;
case "numberTreasuryTitles" /* [31] */ : temp_number_treasury_titles = parseInteger(kvPair.value()); break;
case "expiredAndPendingRemoval" /* [32] */ : temp_expired_and_pending_removal = parseBoolean(kvPair.value()); break;
case "firstContractStorageKey" /* [33] */ : temp_first_contract_storage_key = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
case "headPendingAirdropId" /* [34] */ : temp_head_pending_airdrop_id = PendingAirdropId.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "numberPendingAirdrops" /* [35] */ : temp_number_pending_airdrops = parseLong(kvPair.value()); break;
default: {
if (strictMode) {
// Since we are parsing is strict mode, this is an exceptional condition.
throw new UnknownFieldException(kvPair.STRING().getText());
}
}
}
}
return new Account(temp_account_id, temp_alias, temp_key, temp_expiration_second, temp_tinybar_balance, temp_memo, temp_deleted, temp_staked_to_me, temp_stake_period_start, temp_staked_id, temp_decline_reward, temp_receiver_sig_required, temp_head_token_id, temp_head_nft_id, temp_head_nft_serial_number, temp_number_owned_nfts, temp_max_auto_associations, temp_used_auto_associations, temp_number_associations, temp_smart_contract, temp_number_positive_balances, temp_ethereum_nonce, temp_stake_at_start_of_last_rewarded_period, temp_auto_renew_account_id, temp_auto_renew_seconds, temp_contract_kv_pairs_number, temp_crypto_allowances, temp_approve_for_all_nft_allowances, temp_token_allowances, temp_number_treasury_titles, temp_expired_and_pending_removal, temp_first_contract_storage_key, temp_head_pending_airdrop_id, temp_number_pending_airdrops);
} catch (Exception ex) {
throw new ParseException(ex);
}
}
/**
* Returns JSON string representing an item.
*
* @param data The item to convert. Must not be null.
* @param indent The indent to use for pretty printing
* @param inline When true the output will start with indent end with a new line otherwise
* it will just be the object "{...}"
*/
@Override
public String toJSON(@NonNull Account data, String indent, boolean inline) {
StringBuilder sb = new StringBuilder();
// start
sb.append(inline ? "{\n" : indent + "{\n");
final String childIndent = indent + INDENT;
// collect field lines
final List fieldLines = new ArrayList<>();
// [1] - account_id
if (data.accountId() != null) fieldLines.add(field(childIndent, "accountId", com.hedera.hapi.node.base.AccountID.JSON, data.accountId()));
// [2] - alias
if (data.alias() != Bytes.EMPTY && data.alias() != null && data.alias().length() > 0) fieldLines.add(field("alias", data.alias()));
// [3] - key
if (data.key() != null) fieldLines.add(field(childIndent, "key", com.hedera.hapi.node.base.Key.JSON, data.key()));
// [4] - expiration_second
if (data.expirationSecond() != 0) fieldLines.add(field("expirationSecond", data.expirationSecond()));
// [5] - tinybar_balance
if (data.tinybarBalance() != 0) fieldLines.add(field("tinybarBalance", data.tinybarBalance()));
// [6] - memo
if (data.memo() != "") fieldLines.add(field("memo", data.memo()));
// [7] - deleted
if (data.deleted() != false) fieldLines.add(field("deleted", data.deleted()));
// [8] - staked_to_me
if (data.stakedToMe() != 0) fieldLines.add(field("stakedToMe", data.stakedToMe()));
// [9] - stake_period_start
if (data.stakePeriodStart() != 0) fieldLines.add(field("stakePeriodStart", data.stakePeriodStart()));
// [10] - staked_account_id
if (data.stakedId().kind() == Account.StakedIdOneOfType.STAKED_ACCOUNT_ID)
fieldLines.add(field(childIndent, "stakedAccountId", com.hedera.hapi.node.base.AccountID.JSON, data.stakedAccountId()));
// [11] - staked_node_id
if (data.stakedId().kind() == Account.StakedIdOneOfType.STAKED_NODE_ID)
fieldLines.add(field("stakedNodeId", data.stakedNodeId()));
// [12] - decline_reward
if (data.declineReward() != false) fieldLines.add(field("declineReward", data.declineReward()));
// [13] - receiver_sig_required
if (data.receiverSigRequired() != false) fieldLines.add(field("receiverSigRequired", data.receiverSigRequired()));
// [14] - head_token_id
if (data.headTokenId() != null) fieldLines.add(field(childIndent, "headTokenId", com.hedera.hapi.node.base.TokenID.JSON, data.headTokenId()));
// [15] - head_nft_id
if (data.headNftId() != null) fieldLines.add(field(childIndent, "headNftId", com.hedera.hapi.node.base.NftID.JSON, data.headNftId()));
// [16] - head_nft_serial_number
if (data.headNftSerialNumber() != 0) fieldLines.add(field("headNftSerialNumber", data.headNftSerialNumber()));
// [17] - number_owned_nfts
if (data.numberOwnedNfts() != 0) fieldLines.add(field("numberOwnedNfts", data.numberOwnedNfts()));
// [18] - max_auto_associations
if (data.maxAutoAssociations() != 0) fieldLines.add(field("maxAutoAssociations", data.maxAutoAssociations()));
// [19] - used_auto_associations
if (data.usedAutoAssociations() != 0) fieldLines.add(field("usedAutoAssociations", data.usedAutoAssociations()));
// [20] - number_associations
if (data.numberAssociations() != 0) fieldLines.add(field("numberAssociations", data.numberAssociations()));
// [21] - smart_contract
if (data.smartContract() != false) fieldLines.add(field("smartContract", data.smartContract()));
// [22] - number_positive_balances
if (data.numberPositiveBalances() != 0) fieldLines.add(field("numberPositiveBalances", data.numberPositiveBalances()));
// [23] - ethereum_nonce
if (data.ethereumNonce() != 0) fieldLines.add(field("ethereumNonce", data.ethereumNonce()));
// [24] - stake_at_start_of_last_rewarded_period
if (data.stakeAtStartOfLastRewardedPeriod() != 0) fieldLines.add(field("stakeAtStartOfLastRewardedPeriod", data.stakeAtStartOfLastRewardedPeriod()));
// [25] - auto_renew_account_id
if (data.autoRenewAccountId() != null) fieldLines.add(field(childIndent, "autoRenewAccountId", com.hedera.hapi.node.base.AccountID.JSON, data.autoRenewAccountId()));
// [26] - auto_renew_seconds
if (data.autoRenewSeconds() != 0) fieldLines.add(field("autoRenewSeconds", data.autoRenewSeconds()));
// [27] - contract_kv_pairs_number
if (data.contractKvPairsNumber() != 0) fieldLines.add(field("contractKvPairsNumber", data.contractKvPairsNumber()));
// [28] - crypto_allowances
if (!data.cryptoAllowances().isEmpty()) fieldLines.add(arrayField(childIndent, "cryptoAllowances", com.hedera.hapi.node.state.token.AccountCryptoAllowance.JSON, data.cryptoAllowances()));
// [29] - approve_for_all_nft_allowances
if (!data.approveForAllNftAllowances().isEmpty()) fieldLines.add(arrayField(childIndent, "approveForAllNftAllowances", com.hedera.hapi.node.state.token.AccountApprovalForAllAllowance.JSON, data.approveForAllNftAllowances()));
// [30] - token_allowances
if (!data.tokenAllowances().isEmpty()) fieldLines.add(arrayField(childIndent, "tokenAllowances", com.hedera.hapi.node.state.token.AccountFungibleTokenAllowance.JSON, data.tokenAllowances()));
// [31] - number_treasury_titles
if (data.numberTreasuryTitles() != 0) fieldLines.add(field("numberTreasuryTitles", data.numberTreasuryTitles()));
// [32] - expired_and_pending_removal
if (data.expiredAndPendingRemoval() != false) fieldLines.add(field("expiredAndPendingRemoval", data.expiredAndPendingRemoval()));
// [33] - first_contract_storage_key
if (data.firstContractStorageKey() != Bytes.EMPTY && data.firstContractStorageKey() != null && data.firstContractStorageKey().length() > 0) fieldLines.add(field("firstContractStorageKey", data.firstContractStorageKey()));
// [34] - head_pending_airdrop_id
if (data.headPendingAirdropId() != null) fieldLines.add(field(childIndent, "headPendingAirdropId", com.hedera.hapi.node.base.PendingAirdropId.JSON, data.headPendingAirdropId()));
// [35] - number_pending_airdrops
if (data.numberPendingAirdrops() != 0) fieldLines.add(field("numberPendingAirdrops", data.numberPendingAirdrops()));
// write field lines
if (!fieldLines.isEmpty()){
sb.append(childIndent);
sb.append(String.join(",\n"+childIndent, fieldLines));
sb.append("\n");
}
// end
sb.append(indent + "}");
return sb.toString();
}
}