UNPKG

@stryke/json

Version:

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

105 lines (103 loc) 3.83 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_utils_parse = require('./utils/parse.cjs'); const require_utils_parse_error = require('./utils/parse-error.cjs'); const require_utils_stringify = require('./utils/stringify.cjs'); let _stryke_type_checks_is_string = require("@stryke/type-checks/is-string"); let _stryke_type_checks_is_object = require("@stryke/type-checks/is-object"); let jsonc_parser = require("jsonc-parser"); let superjson = require("superjson"); let node_buffer = require("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.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 require_utils_parse.parse(value); } catch {} const errors = []; const result = (0, jsonc_parser.parse)(value, errors, { allowTrailingComma: true, ...options }); if (errors.length > 0 && errors[0]) throw new Error(require_utils_parse_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 require_utils_stringify.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: (0, _stryke_type_checks_is_string.isString)(options) ? options : options?.identifier || classConstructor.name, allowProps: options && (0, _stryke_type_checks_is_object.isObject)(options) && options?.allowProps && Array.isArray(options.allowProps) ? options.allowProps : ["__typename"] }); } constructor() { super({ dedupe: true }); } }; StormJSON.instance.registerCustom({ isApplicable: (v) => typeof node_buffer.Buffer.isBuffer === "function" && node_buffer.Buffer.isBuffer(v), serialize: (v) => v.toString("base64"), deserialize: (v) => node_buffer.Buffer.from(v, "base64") }, "Bytes"); //#endregion exports.StormJSON = StormJSON;