@hiddentao/clockwork-engine
Version:
A TypeScript/PIXI.js game engine for deterministic, replayable games with built-in rendering
57 lines • 2.24 kB
TypeScript
export interface SerializableClass {
new (...args: any[]): Serializable;
deserialize(data: any): Serializable;
}
export interface Serializable {
serialize(): any;
}
export interface SerializedWrapper {
__type: string;
__data: any;
}
export declare class Serializer {
protected typeRegistry: Map<string, SerializableClass>;
/**
* Register a custom class for serialization and deserialization
* Classes must implement Serializable interface with serialize() method
* and provide static deserialize() method for reconstruction
* @param typeName Unique identifier for this type in serialized data
* @param classConstructor Class constructor implementing SerializableClass interface
*/
registerType(typeName: string, classConstructor: SerializableClass): void;
/**
* Serialize any value to a JSON-safe format with type information
* Handles primitives, arrays, objects, and registered custom classes
* @param value The value to serialize (primitive, array, object, or Serializable)
* @returns Serialized data that can be JSON.stringify'd and later deserialized
*/
serialize(value: any): any;
/**
* Deserialize a value from serialized format with security validation
* Reconstructs primitives, arrays, objects, and registered custom classes
* Only allows deserialization of registered types for security
* @param value The serialized value created by serialize()
* @returns Reconstructed original value
* @throws Error if type name contains unsafe patterns or is not registered
*/
deserialize(value: any): any;
/**
* Get the type name for a serializable object
* Attempts to find the type name in the registry by constructor match
*/
protected getTypeName(obj: Serializable): string;
/**
* Get all registered type names
*/
getRegisteredTypes(): string[];
/**
* Clear all registered types
*/
clearRegistry(): void;
}
/**
* Singleton instance for global type registration and serialization
* Use this for most common cases where you need a shared type registry
*/
export declare const serializer: Serializer;
//# sourceMappingURL=Serializer.d.ts.map