Home > @aws/durable-execution-sdk-js > createClassSerdes
This API is provided as a beta preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
Creates a Serdes for a specific class that preserves the class type. This implementation is a basic class wrapper and does not support any complex class structures. If you need custom serialization, it is recommended to create your own custom serdes.
Signature:
export declare function createClassSerdes<T extends object>(
cls: new () => T,
): Serdes<T>;|
Parameter |
Type |
Description |
|---|---|---|
|
cls |
new () => T |
The class constructor (must have no required parameters) |
Returns:
Serdes<T>
A Serdes that maintains the class type during serialization/deserialization
class User {
name: string = "";
age: number = 0;
greet() {
return `Hello, ${this.name}`;
}
}
const userSerdes = createClassSerdes(User);
// In a durable function:
const user = await context.step(
"create-user",
async () => {
const u = new User();
u.name = "Alice";
u.age = 30;
return u;
},
{ serdes: userSerdes },
);
console.log(user.greet()); // "Hello, Alice" - methods are preservedLimitations: - Class instances becomes plain objects and loses all class information - Constructor must have no parameters - Constructor side-effects will re-run during deserialization - Private fields (#field) cannot be serialized - Getters/setters are not preserved - Nested class instances lose their prototype
For classes with Date properties, use createClassSerdesWithDates instead.