"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toJson = toJson;
exports.fromJson = fromJson;
exports.safeJsonStringify = safeJsonStringify;
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
/*@ts-ignore*/
var { __ΩSerializable } = require('../../types');
function toJson(data) {
    return JSON.stringify(data);
}
toJson.__type = [() => __ΩSerializable, 'data', 'toJson', 'Pn!2""/#'];
function fromJson(serialized) {
    return JSON.parse(serialized);
}
fromJson.__type = ['serialized', 'fromJson', 'P&2!"/"'];
/**
 * JSON.stringify that replaces true circular references with '[Circular]'.
 * Shared (non-circular) object references are serialized normally.
 */
function safeJsonStringify(data) {
    const ancestors = [];
    return JSON.stringify(data, __assignType(function (_key, value) {
        if (typeof value !== 'object' || value === null)
            return value;
        // Walk up from the end; `this` is the parent object holding `_key`
        while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) {
            ancestors.pop();
        }
        if (ancestors.includes(value))
            return '[Circular]';
        ancestors.push(value);
        return value;
    }, ['_key', 'value', '', 'P"2!"2""/#']));
}
safeJsonStringify.__type = ['data', 'safeJsonStringify', 'P#2!&/"'];
//# sourceMappingURL=serialization.js.map