import { Type, TypeClass, TypeObjectLiteral } from '../reflection/type.js'; import { ValidationErrorItem } from '../validator.js'; import { NamingStrategy } from './naming.js'; import { HandlerRegistry } from './registry.js'; import { BuildOptions, JsonBuildContext, SerializationOptions } from './state.js'; export type SerializeFunction = (data: T, options?: SerializationOptions) => R; export type Guard = (data: any, state?: { errors?: ValidationErrorItem[]; }) => data is T; /** * Main serializer class that coordinates type handlers for serialization, * deserialization, and type guards. * * Uses fn() from jit2 for CSP-compliant code generation with tiered execution. * * @example * ```typescript * const serializer = new Serializer('json'); * * interface User { * name: string; * age: number; * } * * const serialize = serializer.buildSerializer(typeOf()); * const result = serialize({ name: 'John', age: 30 }); * ``` */ export declare class Serializer { name: string; /** Registry for serialization handlers */ readonly serializeRegistry: HandlerRegistry; /** Registry for deserialization handlers */ readonly deserializeRegistry: HandlerRegistry; /** Registry for type guards (unified: fast, strict, and error-collecting all use this) */ readonly typeGuards: HandlerRegistry; /** Caches and building sets for each type guard mode */ private readonly typeGuardCaches; constructor(name?: string); /** * Whether to set explicit undefined for missing optional properties. * Can be overridden in subclasses. */ setExplicitUndefined(type: Type, state: JsonBuildContext): boolean; /** * Register default serializers. Override in subclasses to customize. */ protected registerSerializers(): void; /** * Register default type guards. Override in subclasses to customize. */ protected registerTypeGuards(): void; /** * Clear all registries. */ clear(): void; /** * Build a serializer function for a type. * * @param type - The type to serialize * @returns A function that serializes data of that type */ buildSerializer(type: Type): SerializeFunction; /** * Build a deserializer function for a type. * * @param type - The type to deserialize to * @returns A function that deserializes data to that type */ buildDeserializer(type: Type): SerializeFunction; /** * Build a validator function for a type. * * @param type - The type to validate * @returns A function that validates data and returns errors */ buildValidator(type: Type): (data: any, errors?: ValidationErrorItem[]) => boolean; /** * Build a type guard function. * * @param type - The type to guard * @param withLoose - Whether to include loose guards * @returns A type guard function */ buildTypeGuard(type: Type, withLoose?: boolean): Guard; /** * Internal helper: build a cached type guard with recursion detection. * All three public type guard builders delegate to this method. */ private buildCachedTypeGuard; /** * Build a fast type guard function (pure && chain, no error collection). * Use this for maximum performance when you only need to know if data matches the type. */ buildFastTypeGuard(type: Type): (data: unknown) => data is T; /** * Build a strict type guard function (rejects unknown keys). * Similar to buildFastTypeGuard but also checks for extra/unknown properties. */ buildStrictTypeGuard(type: Type): (data: unknown) => data is T; /** * Build a weak type guard function (skips NaN checks for maximum speed). * Only checks structure/types but skips Number.isNaN() checks. */ buildWeakTypeGuard(type: Type): (data: unknown) => data is T; } /** * Compute a cache key suffix from build options. * Returns empty string for default options (no baked values). */ export declare function computeBuildOptionsKey(buildOptions?: BuildOptions): string; /** * Convert SerializationOptions to BuildOptions for baking into specialized functions. */ export declare function serializationOptionsToBuildOptions(options?: SerializationOptions): BuildOptions | undefined; /** * Get a cached serializer function for a type. * * @param type - The type to serialize * @param registry - The handler registry (serialize or deserialize) * @param namingStrategy - Property naming strategy * @param path - Path prefix for error messages * @param buildOptions - Optional build-time options to bake into the function */ export declare function getSerializeFunction(type: Type, registry: HandlerRegistry, namingStrategy?: NamingStrategy, path?: string, buildOptions?: BuildOptions): SerializeFunction; /** * Create a serializer function for a type (not cached). * * @param type - The type to serialize * @param registry - The handler registry * @param namingStrategy - Property naming strategy * @param path - Path prefix for error messages * @param buildOptions - Optional build-time options to bake into the function */ export declare function createSerializeFunction(type: Type, registry: HandlerRegistry, namingStrategy?: NamingStrategy, path?: string, buildOptions?: BuildOptions): SerializeFunction; /** * Create a type guard function for a type. */ export declare function createTypeGuardFunction(type: Type, serializerToUse?: Serializer, withLoose?: boolean): Guard; /** * Get a Partial type for a class or object literal. */ export declare function getPartialType(type: TypeClass | TypeObjectLiteral): TypeObjectLiteral; /** * Get a cached serializer for Partial. */ export declare function getPartialSerializeFunction(type: TypeClass | TypeObjectLiteral, registry: HandlerRegistry, namingStrategy?: NamingStrategy): SerializeFunction; declare class JSONSerializer extends Serializer { constructor(); } /** * The default JSON serializer instance. */ export declare const serializer: JSONSerializer; export {}; export declare type __ΩSerializeFunction = any[]; export declare type __ΩGuard = any[];