@stryke/json
Version:
A package containing JSON parsing/stringify utilities used by Storm Software.
2 lines (1 loc) • 1.46 kB
JavaScript
import{isObject as o}from"@stryke/type-checks/is-object";import{isString as l}from"@stryke/type-checks/is-string";import{parse as c}from"jsonc-parser";import{Buffer as a}from"node:buffer";import p from"superjson";import{parse as u}from"./utils/parse";import{formatParseError as f}from"./utils/parse-error";import{stringify as m}from"./utils/stringify";export class StormJSON extends p{static#r;static get instance(){return StormJSON.#r||(StormJSON.#r=new StormJSON),StormJSON.#r}static deserialize(r){return StormJSON.instance.deserialize(r)}static serialize(r){return StormJSON.instance.serialize(r)}static parse(r){return u(r)}static stringify(r,e){const s=StormJSON.instance.customTransformerRegistry.findApplicable(r);let t=r;return s&&s.isApplicable(r)&&(t=s.serialize(t)),m(t)}static parseJson(r,e){try{if(e?.expectComments===!1)return StormJSON.instance.parse(r)}catch{}const s=[],t={allowTrailingComma:!0,...e},n=c(r,s,t);if(s.length>0&&s[0])throw new Error(f(r,s[0]));return n}static register(r,e,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:e,deserialize:s},r)}static registerClass(r,e){StormJSON.instance.registerClass(r,{identifier:l(e)?e:e?.identifier||r.name,allowProps:e&&o(e)&&e?.allowProps&&Array.isArray(e.allowProps)?e.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");