@stryke/json
Version:
A package containing JSON parsing/stringify utilities used by Storm Software.
105 lines (103 loc) • 3.51 kB
JavaScript
import { parse as parse$1 } from "./utils/parse.mjs";
import { formatParseError } from "./utils/parse-error.mjs";
import { stringify } from "./utils/stringify.mjs";
import { isString } from "@stryke/type-checks/is-string";
import { isObject } from "@stryke/type-checks/is-object";
import { parse } from "jsonc-parser";
import { SuperJSON } from "superjson";
import { Buffer } from "node:buffer";
//#region src/storm-json.ts
/**
* 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.
*/
var StormJSON = class StormJSON extends SuperJSON {
static #instance;
static get instance() {
if (!StormJSON.#instance) StormJSON.#instance = new StormJSON();
return StormJSON.#instance;
}
/**
* Deserialize the given value with superjson using the given metadata
*/
static deserialize(payload) {
return StormJSON.instance.deserialize(payload);
}
/**
* Serialize the given value with superjson
*/
static serialize(object) {
return StormJSON.instance.serialize(object);
}
/**
* Parse the given string value with superjson using the given metadata
*
* @param value - The string value to parse
* @returns The parsed data
*/
static parse(value, options = {}) {
try {
if (options.expectComments === false) return parse$1(value);
} catch {}
const errors = [];
const result = parse(value, errors, {
allowTrailingComma: true,
...options
});
if (errors.length > 0 && errors[0]) throw new Error(formatParseError(value, errors[0]));
return result;
}
/**
* 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(value, options) {
const customTransformer = StormJSON.instance.customTransformerRegistry.findApplicable(value);
let result = value;
if (customTransformer && customTransformer.isApplicable(value)) result = customTransformer.serialize(result);
return stringify(result, options?.spaces ?? 2);
}
/**
* 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(name, serialize, deserialize, isApplicable) {
StormJSON.instance.registerCustom({
isApplicable,
serialize,
deserialize
}, name);
}
/**
* Register a class with superjson
*
* @param classConstructor - The class constructor to register
*/
static registerClass(classConstructor, options) {
StormJSON.instance.registerClass(classConstructor, {
identifier: isString(options) ? options : options?.identifier || classConstructor.name,
allowProps: options && isObject(options) && options?.allowProps && Array.isArray(options.allowProps) ? options.allowProps : ["__typename"]
});
}
constructor() {
super({ dedupe: true });
}
};
StormJSON.instance.registerCustom({
isApplicable: (v) => typeof Buffer.isBuffer === "function" && Buffer.isBuffer(v),
serialize: (v) => v.toString("base64"),
deserialize: (v) => Buffer.from(v, "base64")
}, "Bytes");
//#endregion
export { StormJSON };
//# sourceMappingURL=storm-json.mjs.map