Class Codec


  • public class Codec
    extends Object
    Codec providers the utility for encoding and decoding the data between java native types to the target types such as substrate specific types.
    • Constructor Detail

      • Codec

        public Codec()
    • Method Detail

      • encode

        public static void encode​(Object v,
                                  Encoder enc)
                           throws IllegalArgumentException
        encode function encodes the Object v into the given encoder
        Parameters:
        v - the Object to be encoded
        enc - the encoder used to encode the value. Every Encoder is supposed to hold the encoded value in their own way. For example: ListEncoder can have the List/Arrays and the MapEncoder can have the Map to save the encoded value
        Throws:
        IllegalArgumentException - if something is off.
      • encodeMap

        public static void encodeMap​(Map<?,​?> map,
                                     Encoder enc)
      • encodeObject

        public static void encodeObject​(Object obj,
                                        FieldCache cache,
                                        Encoder enc)
        Encodes the fields of the provided object of the user defined class using an encoder. This method uses reflection to access the object's fields, encode their values, and write them to the Encoder in a map format.

        The method retrieves the field information from the FieldCache, iterates through the fields, and encodes each field value. If the field is a custom class, it recursively encodes the nested object. Primitive or simple types are encoded directly. The field names are used as the map keys.

        Parameters:
        obj - the object whose fields will be encoded. This object must not be null.
        cache - the FieldCache containing cached field information for the object's class. This cache provides the metadata needed to encode the fields. Must not be null.
        enc - the Encoder responsible for encoding the object into a map. The encoder creates key-value pairs where field names are used as keys, and the encoded field values are used as the values. Must not be null.
        Throws:
        NullPointerException - if any of the arguments are null.
        RuntimeException - if reflection fails during field access or if an object field is not accessible.
      • decodeToClass

        public static void decodeToClass​(Object obj,
                                         FieldCache cache,
                                         Decoder dec)
        Decodes the value in the decoder which is expected to have Map, updating the fields of the Object obj with the decoded values. This method uses reflection to retrieve and decode fields dynamically based on the metadata cached in the FieldCache and the map of key-value pairs decoded by the Decoder.

        For each field, the decoder processes a key-value pair, finds the corresponding field by its name, and decodes the value. If the field is a custom class, it recursively decodes its fields. Primitive or non-user defined types are decoded directly.

        This method also ensures that any null custom objects encountered during decoding are instantiated before proceeding with recursive decoding.

        Parameters:
        obj - the object where to put the decoded fields. This object must not be null but values don't matter.
        cache - the FieldCache containing cached fields information for the object's class. This cache provides the mapping of fields that need to be decoded. Must not be null.
        dec - the Decoder responsible for decoding key-value pairs and passing them into the field updates. Must not be null.
        Throws:
        NullPointerException - if any of the arguments are null.
        InvalidArgumentException - if a field corresponding to a key is not found in the class's metadata.
        RuntimeException - if reflection fails during field access or instantiation (e.g., when creating a new instance of a custom class).
      • decode

        public static Object decode​(Class<?> target,
                                    Object v,
                                    Decoder decoder)
        decode function decodes the value in the decoder to target object.
        Parameters:
        target - the class/type where to put the decoded result
        v - the object where to put the decoded result
        decoder - the decoder used to decode the value. Every Decoder is supposed to hold the value to be decoded in their own way. For example: ListDecoder can have the List/Arrays and the MapDecoder can have the Map to save the value to be decoded.
      • decodeMap

        public static void decodeMap​(Map<String,​Object> v,
                                     Decoder d)
        decodeMap decodes the value in decoder to the Map v
        Parameters:
        v - the target map object to hold the decoded value
        d - the decoder holding the initial value