UNPKG

tupleson

Version:

A hackable JSON serializer/deserializer

111 lines 3.75 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var serialize_exports = {}; __export(serialize_exports, { createTsonSerialize: () => createTsonSerialize, createTsonStringify: () => createTsonStringify }); module.exports = __toCommonJS(serialize_exports); var import_errors = require("../errors.js"); var import_getNonce = require("../internals/getNonce.js"); var import_mapOrReturn = require("../internals/mapOrReturn.js"); function getHandlers(opts) { const byPrimitive = {}; const nonPrimitives = []; for (const handler of opts.types) { if (handler.primitive) { if (byPrimitive[handler.primitive]) { throw new Error( `Multiple handlers for primitive ${handler.primitive} found` ); } byPrimitive[handler.primitive] = handler; } else { nonPrimitives.push(handler); } } const getNonce = opts.nonce ? opts.nonce : import_getNonce.getDefaultNonce; return [getNonce, nonPrimitives, byPrimitive]; } function createTsonStringify(opts) { const serializer = createTsonSerialize(opts); return (obj, space) => JSON.stringify(serializer(obj), null, space); } function createTsonSerialize(opts) { const [getNonce, nonPrimitive, byPrimitive] = getHandlers(opts); const walker = (nonce) => { const seen = /* @__PURE__ */ new WeakSet(); const cache = /* @__PURE__ */ new WeakMap(); const walk = (value) => { const type = typeof value; const isComplex = !!value && type === "object"; if (isComplex) { if (seen.has(value)) { const cached = cache.get(value); if (!cached) { throw new import_errors.TsonCircularReferenceError(value); } return cached; } seen.add(value); } const cacheAndReturn = (result) => { if (isComplex) { cache.set(value, result); } return result; }; const primitiveHandler = byPrimitive[type]; if (primitiveHandler && (!primitiveHandler.test || primitiveHandler.test(value))) { return cacheAndReturn([ primitiveHandler.key, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion walk(primitiveHandler.serialize(value)), nonce ]); } for (const handler of nonPrimitive) { if (handler.test(value)) { return cacheAndReturn([ handler.key, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion walk(handler.serialize(value)), nonce ]); } } return cacheAndReturn((0, import_mapOrReturn.mapOrReturn)(value, walk)); }; return walk; }; return (obj) => { const nonce = getNonce(); const json = walker(nonce)(obj); return { json, nonce }; }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createTsonSerialize, createTsonStringify }); //# sourceMappingURL=serialize.js.map