forked from i-am-bee/bee-agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomExternal.ts
25 lines (20 loc) · 835 Bytes
/
customExternal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Serializer } from "bee-agent-framework/serializer/serializer";
class MyClass {
constructor(public readonly name: string) {}
}
Serializer.register(MyClass, {
// Defines how to transform a class to a plain object (snapshot)
toPlain: (instance) => ({ name: instance.name }),
// Defines how to transform a plain object (snapshot) a class instance
fromPlain: (snapshot) => new MyClass(snapshot.name),
// optional handlers to support lazy initiation (handling circular dependencies)
createEmpty: () => new MyClass(""),
updateInstance: (instance, update) => {
Object.assign(instance, update);
},
});
const instance = new MyClass("Bee");
const serialized = Serializer.serialize(instance);
const deserialized = Serializer.deserialize<MyClass>(serialized);
console.info(instance);
console.info(deserialized);