All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hedera.hapi.node.state.file.codec.FileJsonCodec Maven / Gradle / Ivy
package com.hedera.hapi.node.state.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.state.file.File;
import com.hedera.hapi.node.base.*;
import com.hedera.hapi.node.base.codec.*;
import com.hedera.hapi.node.state.file.*;
import com.hedera.hapi.node.state.file.schema.*;
import com.hedera.pbj.runtime.io.buffer.*;
import com.hedera.pbj.runtime.jsonparser.*;
import static com.hedera.hapi.node.state.file.schema.FileSchema.*;
import static com.hedera.pbj.runtime.JsonTools.*;
/**
* JSON Codec for File model object. Generated based on protobuf schema.
*/
public final class FileJsonCodec 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 File 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 --------------------------------------
FileID temp_file_id = null;
long temp_expiration_second = 0;
KeyList temp_keys = null;
Bytes temp_contents = Bytes.EMPTY;
String temp_memo = "";
boolean temp_deleted = false;
long temp_pre_system_delete_expiration_second = 0;
// -- EXTRACT VALUES FROM PARSE TREE ---------------------------------------------
for (JSONParser.PairContext kvPair : root.pair()) {
switch (kvPair.STRING().getText()) {
case "fileId" /* [1] */ : temp_file_id = FileID.JSON.parse(kvPair.value().getChild(JSONParser.ObjContext.class, 0), false, maxDepth - 1); break;
case "expirationSecond" /* [2] */ : temp_expiration_second = parseLong(kvPair.value()); 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 "memo" /* [5] */ : temp_memo = unescape(kvPair.value().STRING().getText()); break;
case "deleted" /* [6] */ : temp_deleted = parseBoolean(kvPair.value()); break;
case "preSystemDeleteExpirationSecond" /* [7] */ : temp_pre_system_delete_expiration_second = 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 File(temp_file_id, temp_expiration_second, temp_keys, temp_contents, temp_memo, temp_deleted, temp_pre_system_delete_expiration_second);
} 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 File 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] - file_id
if (data.fileId() != null) fieldLines.add(field(childIndent, "fileId", com.hedera.hapi.node.base.FileID.JSON, data.fileId()));
// [2] - expiration_second
if (data.expirationSecond() != 0) fieldLines.add(field("expirationSecond", data.expirationSecond()));
// [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] - memo
if (data.memo() != "") fieldLines.add(field("memo", data.memo()));
// [6] - deleted
if (data.deleted() != false) fieldLines.add(field("deleted", data.deleted()));
// [7] - pre_system_delete_expiration_second
if (data.preSystemDeleteExpirationSecond() != 0) fieldLines.add(field("preSystemDeleteExpirationSecond", data.preSystemDeleteExpirationSecond()));
// 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();
}
}