primitives
Primitives are the basic data types that can be used in the javascript language. These are:
string
, number
, boolean
, null
, undefined
, symbol
, and bigint
.
Each of these data types has its own characteristics and methods; but the sdk and its modules and packages also have their own methods and features that can be used to manipulate these data types. It will be common to find objects that represent system data and that encapsulate these data types, for example, the EthereumAddress
object encapsulates a string
data type that represents an ethereum address and has methods to manipulate or validate this address.
EthereumAddress
is an example of a value object, these objects encapsulate a primitive data type and have methods to manipulate or validate this data type. You can see more information about value objects in the Value Objects section.
Serialization and deserialization class with primitives
Knowing this the sdk has a generic type to extract the primitive data type of a value object in a class. This type is Primitives<T>
and can be used as follows.
Suppose we create a class that has an address
property that is of type EthereumAddress
and another chainId
property that is of type ChainId
:
class MyClass {
builder(
public address: EthereumAddress,
public chainId: ChainId
) {}
}
Now we want to extract the primitives of the MyClass
class into a new data type, for this we can use the Primitives<T>
type as follows:
type MyClassPrimitives = Primitives<MyClass>;
This will create a new data type that is the representation of the primitives of the MyClass
class:
type MyClassPrimitives = {
address: string; // address is now of type string and not of type EthereumAddress
chainId: number; // chainId is now number and not ChainId
}
Now we can make the class MyClass
constructible from the primitives and also return its primitives as follows:
class MyClass {
builder(
public address: EthereumAddress,
public chainId: ChainId
) {}
static from(primitives: MyClassPrimitives): MyClass {
return new MyClass(
new EthereumAddress(primitives.address),
new ChainId(primitives.chainId)
);
}
toPrimitives(): MyClassPrimitives {
return {
address: this.address.value,
chainId: this.chainId.value
}
}
}
const myClass = MyClass.from({
address: '0xa73a3b8ACa335855EeaC2f9Fb505BB0360A1B703',
chainId: 80001
});
Now the MyClass
class can be built from the class primitives and can also be serialized using the class primitives.
const myClassPrimitives = myClass.toPrimitives();
The construction of all inner value objects or subclasses are encapsulated and known only to the MyClass
class. While the class can be serialized and deserialized using the primitives of the class.
Private properties of a class
The private properties of a class cannot be extracted from a class and therefore cannot be serialized using the class's primitives.
class MyPrivateClass {
private owner: EthereumAddress;
builder(
public address: EthereumAddress,
public chainId: ChainId
) {}
}
type MyPrivateClassPrimitives = Primitives<MyPrivateClass>;
// MyPrivateClassPrimitives is equal to:
type MyPrivateClassPrimitives = {
address: string;
chainId: number;
}
In the above example the owner
property is private and cannot be extracted from the MyPrivateClass
class and therefore cannot be serialized using the class's primitives.
Read-only properties of a class
In the case of the read-only properties of a class, these can be extracted from the class and therefore can be serialized using the primitives of the class but they cannot be modified since they will be created as read-only properties in the primitives. of the class.
class MyReadOnlyClass {
builder(
readonly address: EthereumAddress,
readonly chainId: ChainId
) {}
}
type MyReadOnlyClassPrimitives = Primitives<MyReadOnlyClass>;
// MyReadOnlyClassPrimitives is equal to:
type MyReadOnlyClassPrimitives = {
readonly address: string;
readonly chainId: number;
}
Subclass primitives
If a class in addition to having properties with value objects also has properties with complex classes or class hierarchies, the Primitives<T>
type will traverse these as well and extract the primitives of these classes.
class MySubClass {
builder(
public address: EthereumAddress,public chainId: ChainId
) {}
}
class MyClass {
builder(
readonly owner: Owner,
public subClass: MySubClass
) {}
}
type MyClassPrimitives = Primitives<MyClass>;
// MyClassPrimitives is equal to:
type MyClassPrimitives = {
readonly owner: string;
subClass: {
address: string;
chainId: number;
}
}
We hope this section has helped you understand how you can serialize and deserialize classes using class primitives and that you can see that the sdk is designed so that you can serialize and deserialize class and value objects in a simple way.