{"version":3,"file":"json.mjs","names":[],"sources":["../src/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 { StormJSON } from \"@stryke/json/storm-json\";\nimport type {\n  JsonParseOptions,\n  JsonSerializeOptions\n} from \"@stryke/json/types\";\nimport { findFileExtension } from \"@stryke/path\";\nimport { isSetString } from \"@stryke/type-checks\";\nimport { isError } from \"@stryke/type-checks/is-error\";\nimport { readFile, readFileSync } from \"./read-file\";\nimport { writeFile, writeFileSync } from \"./write-file\";\n\n/**\n * Reads a JSON file and returns the object the JSON content represents.\n *\n * @param path - A path to a file.\n * @param options - JSON parse options\n * @returns Object the JSON content of the file represents\n */\nexport function readJsonFileSync<T extends object = any>(\n  path: string,\n  options?: JsonParseOptions\n): T {\n  const content = readFileSync(path);\n\n  try {\n    return StormJSON.parse<T>(isSetString(content) ? content.trim() : \"{}\", {\n      expectComments: findFileExtension(path) === \"jsonc\",\n      ...options\n    });\n  } catch (error) {\n    if (isError(error)) {\n      error.message = error.message.replace(\"JSON\", path);\n      throw error;\n    }\n\n    throw new Error(`Failed to parse JSON: ${path}`);\n  }\n}\n\n/**\n * Reads a JSON file and returns the object the JSON content represents.\n *\n * @param path - A path to a file.\n * @param options - JSON parse options\n * @returns Object the JSON content of the file represents\n */\nexport async function readJsonFile<T extends object = any>(\n  path: string,\n  options: JsonParseOptions = {}\n): Promise<T> {\n  const content = await readFile(path);\n\n  try {\n    return StormJSON.parse<T>(isSetString(content) ? content.trim() : \"{}\", {\n      expectComments: findFileExtension(path) === \"jsonc\",\n      ...options\n    });\n  } catch (error) {\n    if (isError(error)) {\n      error.message = error.message.replace(\"JSON\", path);\n      throw error;\n    }\n\n    throw new Error(`Failed to parse JSON: ${path}`);\n  }\n}\n\nexport interface JsonWriteOptions extends JsonSerializeOptions {\n  /**\n   * whether to append new line at the end of JSON file\n   *\n   * @defaultValue false\n   */\n  appendNewLine?: boolean;\n}\n\n/**\n * Serializes the given data to JSON and writes it to a file.\n *\n * @param path - A path to a file.\n * @param data - data which should be serialized to JSON and written to the file\n * @param options - JSON serialize options\n */\nexport function writeJsonFileSync<T extends object = object>(\n  path: string,\n  data: T,\n  options: JsonWriteOptions = {}\n): void {\n  const serializedJson = StormJSON.stringify(data, { spaces: 2, ...options });\n\n  return writeFileSync(\n    path,\n    options?.appendNewLine ? `${serializedJson}\\n` : serializedJson\n  );\n}\n\n/**\n * Serializes the given data to JSON and writes it to a file asynchronously.\n *\n * @param path - A path to a file.\n * @param data - data which should be serialized to JSON and written to the file\n * @param options - JSON serialize options\n */\nexport async function writeJsonFile<T extends object = object>(\n  path: string,\n  data: T,\n  options: JsonWriteOptions = {}\n): Promise<void> {\n  const serializedJson = StormJSON.stringify(data, { spaces: 2, ...options });\n\n  return writeFile(\n    path,\n    options?.appendNewLine ? `${serializedJson}\\n` : serializedJson\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAoCA,SAAgB,iBACd,MACA,SACG;CACH,MAAM,UAAU,aAAa,KAAK;AAElC,KAAI;AACF,SAAO,UAAU,MAAS,YAAY,QAAQ,GAAG,QAAQ,MAAM,GAAG,MAAM;GACtE,gBAAgB,kBAAkB,KAAK,KAAK;GAC5C,GAAG;GACJ,CAAC;UACK,OAAO;AACd,MAAI,QAAQ,MAAM,EAAE;AAClB,SAAM,UAAU,MAAM,QAAQ,QAAQ,QAAQ,KAAK;AACnD,SAAM;;AAGR,QAAM,IAAI,MAAM,yBAAyB,OAAO;;;;;;;;;;AAWpD,eAAsB,aACpB,MACA,UAA4B,EAAE,EAClB;CACZ,MAAM,UAAU,MAAM,SAAS,KAAK;AAEpC,KAAI;AACF,SAAO,UAAU,MAAS,YAAY,QAAQ,GAAG,QAAQ,MAAM,GAAG,MAAM;GACtE,gBAAgB,kBAAkB,KAAK,KAAK;GAC5C,GAAG;GACJ,CAAC;UACK,OAAO;AACd,MAAI,QAAQ,MAAM,EAAE;AAClB,SAAM,UAAU,MAAM,QAAQ,QAAQ,QAAQ,KAAK;AACnD,SAAM;;AAGR,QAAM,IAAI,MAAM,yBAAyB,OAAO;;;;;;;;;;AAoBpD,SAAgB,kBACd,MACA,MACA,UAA4B,EAAE,EACxB;CACN,MAAM,iBAAiB,UAAU,UAAU,MAAM;EAAE,QAAQ;EAAG,GAAG;EAAS,CAAC;AAE3E,QAAO,cACL,MACA,SAAS,gBAAgB,GAAG,eAAe,MAAM,eAClD;;;;;;;;;AAUH,eAAsB,cACpB,MACA,MACA,UAA4B,EAAE,EACf;CACf,MAAM,iBAAiB,UAAU,UAAU,MAAM;EAAE,QAAQ;EAAG,GAAG;EAAS,CAAC;AAE3E,QAAO,UACL,MACA,SAAS,gBAAgB,GAAG,eAAe,MAAM,eAClD"}