{"version":3,"file":"storm-json-MaHpNbZy.mjs","names":["#instance","parseValue","errors: ParseError[]","stringifyValue"],"sources":["../src/storm-json.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { ParseError } from \"jsonc-parser\";\nimport type {\n  Class,\n  JsonParseOptions,\n  JsonParserResult,\n  JsonSerializeOptions,\n  JsonValue\n} from \"./types\";\n// import { Decimal } from \"decimal.js\";\nimport { isObject } from \"@stryke/type-checks/is-object\";\nimport { isString } from \"@stryke/type-checks/is-string\";\nimport { parse } from \"jsonc-parser\";\nimport { Buffer } from \"node:buffer\";\nimport SuperJSON from \"superjson\";\nimport { parse as parseValue } from \"./utils/parse\";\nimport { formatParseError } from \"./utils/parse-error\";\nimport { stringify as stringifyValue } from \"./utils/stringify\";\n\n/**\n * A static JSON parser class used by Storm Software to serialize and deserialize JSON data\n *\n * @remarks\n * This class uses the [SuperJSON](https://github.com/blitz-js/superjson) library under the hood.\n */\nexport class StormJSON extends SuperJSON {\n  static #instance: StormJSON;\n\n  public static get instance(): StormJSON {\n    if (!StormJSON.#instance) {\n      StormJSON.#instance = new StormJSON();\n    }\n\n    return StormJSON.#instance;\n  }\n\n  /**\n   * Deserialize the given value with superjson using the given metadata\n   */\n  public static override deserialize<TData = unknown>(\n    payload: JsonParserResult\n  ): TData {\n    return StormJSON.instance.deserialize(payload);\n  }\n\n  /**\n   * Serialize the given value with superjson\n   */\n  public static override serialize(object: JsonValue): JsonParserResult {\n    return StormJSON.instance.serialize(object);\n  }\n\n  /**\n   * Parse the given string value with superjson using the given metadata\n   *\n   * @param value - The string value to parse\n   * @returns The parsed data\n   */\n  public static override parse<TData = unknown>(\n    value: string,\n    options: JsonParseOptions = {}\n  ): TData {\n    try {\n      if (options.expectComments === false) {\n        return parseValue<TData>(value);\n      }\n    } catch {\n      // Do nothing\n    }\n\n    const errors: ParseError[] = [];\n    const opts = {\n      allowTrailingComma: true,\n      ...options\n    };\n    const result = parse(value, errors, opts) as TData;\n\n    if (errors.length > 0 && errors[0]) {\n      throw new Error(formatParseError(value, errors[0]));\n    }\n\n    return result;\n  }\n\n  /**\n   * Serializes the given data to a JSON string.\n   * By default the JSON string is formatted with a 2 space indentation to be easy readable.\n   *\n   * @param value - Object which should be serialized to JSON\n   * @param options - JSON serialize options\n   * @returns the formatted JSON representation of the object\n   */\n  public static override stringify<T>(\n    value: T,\n    options?: JsonSerializeOptions\n  ): string {\n    const customTransformer =\n      StormJSON.instance.customTransformerRegistry.findApplicable(value);\n\n    let result = value;\n    if (customTransformer && customTransformer.isApplicable(value)) {\n      result = customTransformer.serialize(result) as T;\n    }\n\n    return stringifyValue(result, options?.spaces ?? 2);\n  }\n\n  /**\n   * Register a custom schema with superjson\n   *\n   * @param name - The name of the schema\n   * @param serialize - The function to serialize the schema\n   * @param deserialize - The function to deserialize the schema\n   * @param isApplicable - The function to check if the schema is applicable\n   */\n  public static register<\n    TData = any,\n    TJsonObject extends JsonValue = JsonValue\n  >(\n    name: string,\n    serialize: (data: TData) => TJsonObject,\n    deserialize: (json: TJsonObject) => TData,\n    isApplicable: (data: any) => data is TData\n  ) {\n    StormJSON.instance.registerCustom<TData, TJsonObject>(\n      {\n        isApplicable,\n        serialize,\n        deserialize\n      },\n      name\n    );\n  }\n\n  /**\n   * Register a class with superjson\n   *\n   * @param classConstructor - The class constructor to register\n   */\n  public static override registerClass(\n    classConstructor: Class,\n    options?: { identifier?: string; allowProps?: string[] } | string\n  ) {\n    StormJSON.instance.registerClass(classConstructor, {\n      identifier: isString(options)\n        ? options\n        : options?.identifier || classConstructor.name,\n      allowProps:\n        options &&\n        isObject(options) &&\n        options?.allowProps &&\n        Array.isArray(options.allowProps)\n          ? options.allowProps\n          : [\"__typename\"]\n    });\n  }\n\n  private constructor() {\n    super({ dedupe: true });\n  }\n}\n\nStormJSON.instance.registerCustom<Buffer, string>(\n  {\n    isApplicable: (v): v is Buffer =>\n      typeof Buffer.isBuffer === \"function\" && Buffer.isBuffer(v),\n    serialize: v => v.toString(\"base64\"),\n    deserialize: v => Buffer.from(v, \"base64\")\n  },\n  \"Bytes\"\n);\n"],"mappings":";;;;;;;;;;;;;;;;AA0CA,IAAa,YAAb,MAAa,kBAAkB,UAAU;CACvC,QAAOA;CAEP,WAAkB,WAAsB;AACtC,MAAI,CAAC,WAAUA,SACb,YAAUA,WAAY,IAAI,WAAW;AAGvC,SAAO,WAAUA;;;;;CAMnB,OAAuB,YACrB,SACO;AACP,SAAO,UAAU,SAAS,YAAY,QAAQ;;;;;CAMhD,OAAuB,UAAU,QAAqC;AACpE,SAAO,UAAU,SAAS,UAAU,OAAO;;;;;;;;CAS7C,OAAuB,MACrB,OACA,UAA4B,EAAE,EACvB;AACP,MAAI;AACF,OAAI,QAAQ,mBAAmB,MAC7B,QAAOC,QAAkB,MAAM;UAE3B;EAIR,MAAMC,SAAuB,EAAE;EAK/B,MAAM,SAAS,MAAM,OAAO,QAJf;GACX,oBAAoB;GACpB,GAAG;GACJ,CACwC;AAEzC,MAAI,OAAO,SAAS,KAAK,OAAO,GAC9B,OAAM,IAAI,MAAM,iBAAiB,OAAO,OAAO,GAAG,CAAC;AAGrD,SAAO;;;;;;;;;;CAWT,OAAuB,UACrB,OACA,SACQ;EACR,MAAM,oBACJ,UAAU,SAAS,0BAA0B,eAAe,MAAM;EAEpE,IAAI,SAAS;AACb,MAAI,qBAAqB,kBAAkB,aAAa,MAAM,CAC5D,UAAS,kBAAkB,UAAU,OAAO;AAG9C,SAAOC,UAAe,QAAQ,SAAS,UAAU,EAAE;;;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