@stryke/json
Version:
A package containing JSON parsing/stringify utilities used by Storm Software.
65 lines (64 loc) • 2.68 kB
TypeScript
import type { ParseOptions } from "jsonc-parser";
export type PrimitiveJsonValue = string | number | boolean | undefined | null;
export interface Class {
new (...args: any[]): any;
}
export type JsonValue = PrimitiveJsonValue | JsonArray | JsonObject;
export type JsonArray = Array<JsonValue>;
export interface JsonObject {
[key: string]: JsonValue;
}
export type ClassInstance = any;
export type SerializableJsonValue = symbol | Set<JsonValue> | Map<JsonValue, JsonValue> | undefined | bigint | Date | ClassInstance | RegExp;
export type Tree<T> = InnerNode<T> | Leaf<T>;
export type Leaf<T> = [T];
export type InnerNode<T> = [T, Record<string, Tree<T>>];
export type PrimitiveTypeAnnotation = "number" | "undefined" | "bigint";
export type LeafTypeAnnotation = PrimitiveTypeAnnotation | "regexp" | "Date" | "Error" | "URL";
export type TypedArrayAnnotation = ["typed-array", string];
export type ClassTypeAnnotation = ["class", string];
export type SymbolTypeAnnotation = ["symbol", string];
export type CustomTypeAnnotation = ["custom", string];
export type SimpleTypeAnnotation = LeafTypeAnnotation | "map" | "set";
export type CompositeTypeAnnotation = TypedArrayAnnotation | ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation;
export type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation;
export interface JsonParseOptions extends ParseOptions {
/**
* Expect JSON with javascript-style
*
* @defaultValue false
*/
expectComments?: boolean;
/**
* Disallow javascript-style
*
* @defaultValue false
*/
disallowComments?: boolean;
/**
* Allow trailing commas in the JSON content
*/
allowTrailingComma?: boolean;
}
export interface JsonSerializeOptions {
/**
* the whitespaces to add as indentation to make the output more readable.
*
* @defaultValue 2
*/
spaces?: number;
}
export interface JsonParserResult {
json: JsonValue;
meta?: {
values?: Tree<TypeAnnotation> | Record<string, Tree<TypeAnnotation>> | undefined;
referentialEqualities?: Record<string, string[]> | [string[]] | [string[], Record<string, string[]>];
};
}
export interface IJsonParser {
parse: <TData = any>(strData: string) => TData;
stringify: <TData = any>(data: TData) => string;
serialize: (object: JsonValue) => JsonParserResult;
deserialize: <TData = any>(payload: JsonParserResult) => TData;
register: <TData = any, TJsonValue extends JsonValue = JsonValue>(name: string, serialize: (object: JsonValue) => TJsonValue, deserialize: (payload: TJsonValue) => TData, isApplicable: (data: any) => data is TData) => void;
}