node-json-db
Version:
Database using JSON file as storage for Node.JS
104 lines (103 loc) • 2.92 kB
TypeScript
import { ISerializer } from "./ISerializer";
/**
* Serializer for JavaScript Date objects.
* Serializes a Date as an ISO 8601 string.
*
* @example
* ```json
* { "__type": "Date", "__value": "2023-01-01T00:00:00.000Z" }
* ```
*/
export declare class DateSerializer implements ISerializer {
readonly type = "Date";
serialize(value: Date): string;
deserialize(value: string): Date;
test(value: any): boolean;
}
/**
* Serializer for JavaScript Set objects.
* Serializes a Set as an array of its values.
*
* @example
* ```json
* { "__type": "Set", "__value": [1, 2, 3] }
* ```
*/
export declare class SetSerializer implements ISerializer {
readonly type = "Set";
serialize(value: Set<any>): any[];
deserialize(value: any[]): Set<any>;
test(value: any): boolean;
}
/**
* Serializer for JavaScript Map objects.
* Serializes a Map as an array of key-value pairs.
*
* @example
* ```json
* { "__type": "Map", "__value": [["key1", "value1"], ["key2", "value2"]] }
* ```
*/
export declare class MapSerializer implements ISerializer {
readonly type = "Map";
serialize(value: Map<any, any>): [any, any][];
deserialize(value: [any, any][]): Map<any, any>;
test(value: any): boolean;
}
/**
* Serializer for JavaScript RegExp objects.
* Serializes a RegExp as an object with source and flags.
*
* @example
* ```json
* { "__type": "RegExp", "__value": { "source": "hello\\s+world", "flags": "gi" } }
* ```
*/
export declare class RegExpSerializer implements ISerializer {
readonly type = "RegExp";
serialize(value: RegExp): {
source: string;
flags: string;
};
deserialize(value: {
source: string;
flags: string;
}): RegExp;
test(value: any): boolean;
}
/**
* Serializer for JavaScript BigInt values.
* Serializes a BigInt as a string representation.
*
* @example
* ```json
* { "__type": "BigInt", "__value": "9007199254740993" }
* ```
*/
export declare class BigIntSerializer implements ISerializer {
readonly type = "BigInt";
serialize(value: bigint): string;
deserialize(value: string): bigint;
test(value: any): boolean;
}
/**
* Default serializers included with the JsonAdapter.
* Provides built-in support for Date, Set, Map, RegExp, and BigInt types.
*
* This array is frozen (immutable). To extend it with your own serializers,
* spread it into a new array:
* ```typescript
* import { defaultSerializers, ISerializer } from 'node-json-db';
*
* const mySerializer: ISerializer = {
* type: "MyType",
* serialize: (value) => value.toJSON(),
* deserialize: (value) => MyType.fromJSON(value),
* test: (value) => value instanceof MyType,
* };
*
* // Use with Config.addSerializer() or spread into a custom list:
* const serializers = [...defaultSerializers, mySerializer];
* ```
*/
export declare const defaultSerializers: readonly ISerializer[];