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

com.hedera.hapi.platform.state.codec.AddressJsonCodec Maven / Gradle / Ivy

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

import com.hedera.hapi.platform.state.*;
import com.hedera.hapi.platform.state.schema.*;
import com.hedera.pbj.runtime.io.buffer.*;

import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.platform.state.schema.AddressSchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;

/**
 * JSON Codec for Address model object. Generated based on protobuf schema.
 */
public final class AddressJsonCodec 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 Address 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 -------------------------------------- NodeId temp_id = null; String temp_nickname = ""; String temp_self_name = ""; long temp_weight = 0; String temp_hostname_internal = ""; int temp_port_internal = 0; String temp_hostname_external = ""; int temp_port_external = 0; Bytes temp_signing_certificate = Bytes.EMPTY; Bytes temp_agreement_certificate = Bytes.EMPTY; String temp_memo = ""; // -- EXTRACT VALUES FROM PARSE TREE --------------------------------------------- for (JSONParser.PairContext kvPair : root.pair()) { switch (kvPair.STRING().getText()) { case "id" /* [1] */ : temp_id = NodeId.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break; case "nickname" /* [2] */ : temp_nickname = unescape(kvPair.value().STRING().getText()); break; case "selfName" /* [3] */ : temp_self_name = unescape(kvPair.value().STRING().getText()); break; case "weight" /* [4] */ : temp_weight = parseLong(kvPair.value()); break; case "hostnameInternal" /* [5] */ : temp_hostname_internal = unescape(kvPair.value().STRING().getText()); break; case "portInternal" /* [6] */ : temp_port_internal = parseInteger(kvPair.value()); break; case "hostnameExternal" /* [7] */ : temp_hostname_external = unescape(kvPair.value().STRING().getText()); break; case "portExternal" /* [8] */ : temp_port_external = parseInteger(kvPair.value()); break; case "signingCertificate" /* [9] */ : temp_signing_certificate = Bytes.fromBase64(kvPair.value().STRING().getText()); break; case "agreementCertificate" /* [10] */ : temp_agreement_certificate = Bytes.fromBase64(kvPair.value().STRING().getText()); break; case "memo" /* [11] */ : 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 Address(temp_id, temp_nickname, temp_self_name, temp_weight, temp_hostname_internal, temp_port_internal, temp_hostname_external, temp_port_external, temp_signing_certificate, temp_agreement_certificate, 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 Address 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] - id if (data.id() != null) fieldLines.add(field(childIndent, "id", com.hedera.hapi.platform.state.NodeId.JSON, data.id())); // [2] - nickname if (data.nickname() != "") fieldLines.add(field("nickname", data.nickname())); // [3] - self_name if (data.selfName() != "") fieldLines.add(field("selfName", data.selfName())); // [4] - weight if (data.weight() != 0) fieldLines.add(field("weight", data.weight())); // [5] - hostname_internal if (data.hostnameInternal() != "") fieldLines.add(field("hostnameInternal", data.hostnameInternal())); // [6] - port_internal if (data.portInternal() != 0) fieldLines.add(field("portInternal", data.portInternal())); // [7] - hostname_external if (data.hostnameExternal() != "") fieldLines.add(field("hostnameExternal", data.hostnameExternal())); // [8] - port_external if (data.portExternal() != 0) fieldLines.add(field("portExternal", data.portExternal())); // [9] - signing_certificate if (data.signingCertificate() != Bytes.EMPTY && data.signingCertificate() != null && data.signingCertificate().length() > 0) fieldLines.add(field("signingCertificate", data.signingCertificate())); // [10] - agreement_certificate if (data.agreementCertificate() != Bytes.EMPTY && data.agreementCertificate() != null && data.agreementCertificate().length() > 0) fieldLines.add(field("agreementCertificate", data.agreementCertificate())); // [11] - 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