Serialization
[!TIP]
Location within the framework
Hive-agent-framework/serializer.
Serialization is a process of converting complex data structures or objects into a format that can be easily stored, transmitted, and reconstructed later. Serialization is a difficult task, and JavaScript does not provide a magic tool to serialize and deserialize an arbitrary input. That is why we made such one.
import { Serializer } from "Hive-agent-framework/serializer/serializer";
const original = new Date("2024-01-01T00:00:00.000Z");
const serialized = Serializer.serialize(original);
const deserialized = Serializer.deserialize(serialized);
console.info(deserialized instanceof Date); // true
console.info(original.toISOString() === deserialized.toISOString()); // trueSource: examples/serialization/base.ts
[!NOTE]
Serializer knows how to serialize/deserialize the most well-known JavaScript data structures. Continue reading to see how to register your own.
Being Serializable
Most parts of the framework implement the internal Serializable class, which exposes the following methods.
createSnapshot(returns an object that "snapshots" the current state)loadSnapshot(applies the provided snapshot to the current instance)fromSerialized(static, creates the new instance from the given serialized input)fromSnapshot(static, creates the new instance from the given snapshot)
See the direct usage on the following memory example.
Source: examples/serialization/memory.ts
Serializing unknowns
If you want to serialize a class that the Serializer does not know, it throws the SerializerError error. However, you can tell the Serializer how to work with your class by registering it as a serializable.
Source: examples/serialization/customExternal.ts
or you can extend the Serializable class.
Source: examples/serialization/customInternal.ts
[!TIP]
Most framework components are
Serializable.
Context matters
Source: examples/serialization/context.ts
[!IMPORTANT]
Ensuring that all classes are registered in advance can be annoying, but there's a good reason for that. If we imported all the classes for you, that would significantly increase your application's size and bootstrapping time + you would have to install all peer dependencies that you may not even need.