UNPKG

@stryke/json

Version:

A package containing JSON parsing/stringify utilities used by Storm Software.

65 lines (64 loc) 2.67 kB
import type { Class, JsonParseOptions, JsonParserResult, JsonSerializeOptions, JsonValue } from "./types"; import SuperJSON from "superjson"; /** * A static JSON parser class used by Storm Software to serialize and deserialize JSON data * * @remarks * This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood. */ export declare class StormJSON extends SuperJSON { #private; static get instance(): StormJSON; /** * Deserialize the given value with superjson using the given metadata */ static deserialize<TData = unknown>(payload: JsonParserResult): TData; /** * Serialize the given value with superjson */ static serialize(object: JsonValue): JsonParserResult; /** * Parse the given string value with superjson using the given metadata * * @param value - The string value to parse * @returns The parsed data */ static parse<TData = unknown>(value: string): TData; /** * Serializes the given data to a JSON string. * By default the JSON string is formatted with a 2 space indentation to be easy readable. * * @param value - Object which should be serialized to JSON * @param _options - JSON serialize options * @returns the formatted JSON representation of the object */ static stringify<T>(value: T, _options?: JsonSerializeOptions): string; /** * Parses the given JSON string and returns the object the JSON content represents. * By default javascript-style comments and trailing commas are allowed. * * @param strData - JSON content as string * @param options - JSON parse options * @returns Object the JSON content represents */ static parseJson<TData = unknown>(strData: string, options?: JsonParseOptions): TData; /** * Register a custom schema with superjson * * @param name - The name of the schema * @param serialize - The function to serialize the schema * @param deserialize - The function to deserialize the schema * @param isApplicable - The function to check if the schema is applicable */ static register<TData = any, TJsonObject extends JsonValue = JsonValue>(name: string, serialize: (data: TData) => TJsonObject, deserialize: (json: TJsonObject) => TData, isApplicable: (data: any) => data is TData): void; /** * Register a class with superjson * * @param classConstructor - The class constructor to register */ static registerClass(classConstructor: Class, options?: { identifier?: string; allowProps?: string[]; } | string): void; private constructor(); }