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

com.hedera.hapi.node.state.addressbook.codec.NodeJsonCodec Maven / Gradle / Ivy

The newest version!
package com.hedera.hapi.node.state.addressbook.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.addressbook.Node;

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

import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.node.state.addressbook.schema.NodeSchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;

/**
 * JSON Codec for Node model object. Generated based on protobuf schema.
 */
public final class NodeJsonCodec 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 Node 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 --------------------------------------
                long temp_node_id = 0;
        AccountID temp_account_id = null;
        String temp_description = "";
        List temp_gossip_endpoint = Collections.emptyList();
        List temp_service_endpoint = Collections.emptyList();
        Bytes temp_gossip_ca_certificate = Bytes.EMPTY;
        Bytes temp_grpc_certificate_hash = Bytes.EMPTY;
        long temp_weight = 0;
        boolean temp_deleted = false;
        Key temp_admin_key = null;
    
            // -- EXTRACT VALUES FROM PARSE TREE ---------------------------------------------
    
            for (JSONParser.PairContext kvPair : root.pair()) {
                switch (kvPair.STRING().getText()) {
                    case "nodeId" /* [1] */ : temp_node_id = parseLong(kvPair.value()); break;
    case "accountId" /* [2] */ : temp_account_id = AccountID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
    case "description" /* [3] */ : temp_description = unescape(kvPair.value().STRING().getText()); break;
    case "gossipEndpoint" /* [4] */ : temp_gossip_endpoint = parseObjArray(kvPair.value().arr(), ServiceEndpoint.JSON, maxDepth - 1); break;
    case "serviceEndpoint" /* [5] */ : temp_service_endpoint = parseObjArray(kvPair.value().arr(), ServiceEndpoint.JSON, maxDepth - 1); break;
    case "gossipCaCertificate" /* [6] */ : temp_gossip_ca_certificate = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
    case "grpcCertificateHash" /* [7] */ : temp_grpc_certificate_hash = Bytes.fromBase64(kvPair.value().STRING().getText()); break;
    case "weight" /* [8] */ : temp_weight = parseLong(kvPair.value()); break;
    case "deleted" /* [9] */ : temp_deleted = parseBoolean(kvPair.value()); break;
    case "adminKey" /* [10] */ : temp_admin_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 Node(temp_node_id, temp_account_id, temp_description, temp_gossip_endpoint, temp_service_endpoint, temp_gossip_ca_certificate, temp_grpc_certificate_hash, temp_weight, temp_deleted, temp_admin_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 Node 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] - node_id
        if (data.nodeId() != 0) fieldLines.add(field("nodeId", data.nodeId()));
        // [2] - account_id
        if (data.accountId() != null) fieldLines.add(field(childIndent, "accountId", com.hedera.hapi.node.base.AccountID.JSON, data.accountId()));
        // [3] - description
        if (data.description() != "") fieldLines.add(field("description", data.description()));
        // [4] - gossip_endpoint
        if (!data.gossipEndpoint().isEmpty()) fieldLines.add(arrayField(childIndent, "gossipEndpoint", com.hedera.hapi.node.base.ServiceEndpoint.JSON, data.gossipEndpoint()));
        // [5] - service_endpoint
        if (!data.serviceEndpoint().isEmpty()) fieldLines.add(arrayField(childIndent, "serviceEndpoint", com.hedera.hapi.node.base.ServiceEndpoint.JSON, data.serviceEndpoint()));
        // [6] - gossip_ca_certificate
        if (data.gossipCaCertificate() != Bytes.EMPTY && data.gossipCaCertificate() != null && data.gossipCaCertificate().length() > 0) fieldLines.add(field("gossipCaCertificate", data.gossipCaCertificate()));
        // [7] - grpc_certificate_hash
        if (data.grpcCertificateHash() != Bytes.EMPTY && data.grpcCertificateHash() != null && data.grpcCertificateHash().length() > 0) fieldLines.add(field("grpcCertificateHash", data.grpcCertificateHash()));
        // [8] - weight
        if (data.weight() != 0) fieldLines.add(field("weight", data.weight()));
        // [9] - deleted
        if (data.deleted() != false) fieldLines.add(field("deleted", data.deleted()));
        // [10] - admin_key
        if (data.adminKey() != null) fieldLines.add(field(childIndent, "adminKey", com.hedera.hapi.node.base.Key.JSON, data.adminKey()));
    
        // 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