All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hedera.hapi.node.state.consensus.codec.TopicJsonCodec Maven / Gradle / Ivy

There is a newer version: 0.54.0
Show newest version
package com.hedera.hapi.node.state.consensus.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.consensus.Topic;

import com.hedera.hapi.node.base.*;
import com.hedera.hapi.node.base.codec.*;
import com.hedera.hapi.node.state.consensus.*;
import com.hedera.hapi.node.state.consensus.schema.*;
import com.hedera.pbj.runtime.io.buffer.*;

import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.node.state.consensus.schema.TopicSchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;

/**
 * JSON Codec for Topic model object. Generated based on protobuf schema.
 */
public final class TopicJsonCodec implements JsonCodec {
    

        /**
     * 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 Topic 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 --------------------------------------
                TopicID temp_topic_id = null;
        long temp_sequence_number = 0;
        long temp_expiration_second = 0;
        long temp_auto_renew_period = 0;
        AccountID temp_auto_renew_account_id = null;
        boolean temp_deleted = false;
        Bytes temp_running_hash = Bytes.EMPTY;
        String temp_memo = "";
        Key temp_admin_key = null;
        Key temp_submit_key = null;
    
            // -- EXTRACT VALUES FROM PARSE TREE ---------------------------------------------
    
            for (JSONParser.PairContext kvPair : root.pair()) {
                switch (kvPair.STRING().getText()) {
                    case "topicId" /* [1] */ : temp_topic_id = TopicID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "sequenceNumber" /* [2] */ : temp_sequence_number = parseLong(kvPair.value()); break;
    case "expirationSecond" /* [3] */ : temp_expiration_second = parseLong(kvPair.value()); break;
    case "autoRenewPeriod" /* [4] */ : temp_auto_renew_period = parseLong(kvPair.value()); break;
    case "autoRenewAccountId" /* [5] */ : temp_auto_renew_account_id = AccountID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "deleted" /* [6] */ : temp_deleted = parseBoolean(kvPair.value()); break;
    case "runningHash" /* [7] */ : temp_running_hash = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
    case "memo" /* [8] */ : temp_memo = unescape(kvPair.value().STRING().getText()); break;
    case "adminKey" /* [9] */ : temp_admin_key = Key.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "submitKey" /* [10] */ : temp_submit_key = Key.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    
                    default: {
                        if (strictMode) {
                            // Since we are parsing is strict mode, this is an exceptional condition.
                            throw new UnknownFieldException(kvPair.STRING().getText());
                        }
                    }
                }
            }
    
            return new Topic(temp_topic_id, temp_sequence_number, temp_expiration_second, temp_auto_renew_period, temp_auto_renew_account_id, temp_deleted, temp_running_hash, temp_memo, temp_admin_key, temp_submit_key);
        } 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 Topic 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] - topic_id
        if (data.topicId() != null) fieldLines.add(field(childIndent, "topicId", com.hedera.hapi.node.base.TopicID.JSON, data.topicId()));
        // [2] - sequence_number
        if (data.sequenceNumber() != 0) fieldLines.add(field("sequenceNumber", data.sequenceNumber()));
        // [3] - expiration_second
        if (data.expirationSecond() != 0) fieldLines.add(field("expirationSecond", data.expirationSecond()));
        // [4] - auto_renew_period
        if (data.autoRenewPeriod() != 0) fieldLines.add(field("autoRenewPeriod", data.autoRenewPeriod()));
        // [5] - auto_renew_account_id
        if (data.autoRenewAccountId() != null) fieldLines.add(field(childIndent, "autoRenewAccountId", com.hedera.hapi.node.base.AccountID.JSON, data.autoRenewAccountId()));
        // [6] - deleted
        if (data.deleted() != false) fieldLines.add(field("deleted", data.deleted()));
        // [7] - running_hash
        if (data.runningHash() != Bytes.EMPTY && data.runningHash() != null && data.runningHash().length() > 0) fieldLines.add(field("runningHash", data.runningHash()));
        // [8] - memo
        if (data.memo() != "") fieldLines.add(field("memo", data.memo()));
        // [9] - admin_key
        if (data.adminKey() != null) fieldLines.add(field(childIndent, "adminKey", com.hedera.hapi.node.base.Key.JSON, data.adminKey()));
        // [10] - submit_key
        if (data.submitKey() != null) fieldLines.add(field(childIndent, "submitKey", com.hedera.hapi.node.base.Key.JSON, data.submitKey()));
    
        // 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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy