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

com.hedera.hapi.node.file.codec.FileCreateTransactionBodyJsonCodec Maven / Gradle / Ivy

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

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

import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.node.file.schema.FileCreateTransactionBodySchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;

/**
 * JSON Codec for FileCreateTransactionBody model object. Generated based on protobuf schema.
 */
public final class FileCreateTransactionBodyJsonCodec 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 FileCreateTransactionBody 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 --------------------------------------
                Timestamp temp_expirationTime = null;
        KeyList temp_keys = null;
        Bytes temp_contents = Bytes.EMPTY;
        ShardID temp_shardID = null;
        RealmID temp_realmID = null;
        Key temp_newRealmAdminKey = null;
        String temp_memo = "";
    
            // -- EXTRACT VALUES FROM PARSE TREE ---------------------------------------------
    
            for (JSONParser.PairContext kvPair : root.pair()) {
                switch (kvPair.STRING().getText()) {
                    case "expirationTime" /* [2] */ : temp_expirationTime = Timestamp.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "keys" /* [3] */ : temp_keys = KeyList.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "contents" /* [4] */ : temp_contents = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
    case "shardID" /* [5] */ : temp_shardID = ShardID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "realmID" /* [6] */ : temp_realmID = RealmID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "newRealmAdminKey" /* [7] */ : temp_newRealmAdminKey = Key.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "memo" /* [8] */ : temp_memo = unescape(kvPair.value().STRING().getText()); break;
    
                    default: {
                        if (strictMode) {
                            // Since we are parsing is strict mode, this is an exceptional condition.
                            throw new UnknownFieldException(kvPair.STRING().getText());
                        }
                    }
                }
            }
    
            return new FileCreateTransactionBody(temp_expirationTime, temp_keys, temp_contents, temp_shardID, temp_realmID, temp_newRealmAdminKey, temp_memo);
        } 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 FileCreateTransactionBody 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<>();
            // [2] - expirationTime
        if (data.expirationTime() != null) fieldLines.add(field(childIndent, "expirationTime", com.hedera.hapi.node.base.Timestamp.JSON, data.expirationTime()));
        // [3] - keys
        if (data.keys() != null) fieldLines.add(field(childIndent, "keys", com.hedera.hapi.node.base.KeyList.JSON, data.keys()));
        // [4] - contents
        if (data.contents() != Bytes.EMPTY && data.contents() != null && data.contents().length() > 0) fieldLines.add(field("contents", data.contents()));
        // [5] - shardID
        if (data.shardID() != null) fieldLines.add(field(childIndent, "shardID", com.hedera.hapi.node.base.ShardID.JSON, data.shardID()));
        // [6] - realmID
        if (data.realmID() != null) fieldLines.add(field(childIndent, "realmID", com.hedera.hapi.node.base.RealmID.JSON, data.realmID()));
        // [7] - newRealmAdminKey
        if (data.newRealmAdminKey() != null) fieldLines.add(field(childIndent, "newRealmAdminKey", com.hedera.hapi.node.base.Key.JSON, data.newRealmAdminKey()));
        // [8] - memo
        if (data.memo() != "") fieldLines.add(field("memo", data.memo()));
    
        // 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