Class Codec
- java.lang.Object
-
- com.salesforce.multicloudj.docstore.driver.codec.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 Summary
Constructors Constructor Description Codec()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object
decode(Class<?> target, Object v, Decoder decoder)
decode function decodes the value in the decoder to target object.static void
decodeMap(Map<String,Object> v, Decoder d)
decodeMap decodes the value in decoder to the Map vstatic 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.static void
encode(Object v, Encoder enc)
encode function encodes the Object v into the given encoderstatic void
encodeMap(Map<?,?> map, Encoder enc)
static void
encodeObject(Object obj, FieldCache cache, Encoder enc)
Encodes the fields of the provided object of the user defined class using an encoder.static String
stringifyMapKey(Object key)
-
-
-
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 encodedenc
- 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.
-
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 theEncoder
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 benull
.cache
- theFieldCache
containing cached field information for the object's class. This cache provides the metadata needed to encode the fields. Must not benull
.enc
- theEncoder
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 benull
.- Throws:
NullPointerException
- if any of the arguments arenull
.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 theFieldCache
and the map of key-value pairs decoded by theDecoder
.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 benull
but values don't matter.cache
- theFieldCache
containing cached fields information for the object's class. This cache provides the mapping of fields that need to be decoded. Must not benull
.dec
- theDecoder
responsible for decoding key-value pairs and passing them into the field updates. Must not benull
.- Throws:
NullPointerException
- if any of the arguments arenull
.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).
-
stringifyMapKey
public static String stringifyMapKey(Object key) throws IllegalArgumentException
- Throws:
IllegalArgumentException
-
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 resultv
- the object where to put the decoded resultdecoder
- 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.
-
-