'use strict'; var _classPrivateMethodInitSpec = require("@babel/runtime-corejs3/helpers/classPrivateMethodInitSpec")["default"]; var _classPrivateMethodGet = require("@babel/runtime-corejs3/helpers/classPrivateMethodGet")["default"]; var _objectSpread = require("@babel/runtime-corejs3/helpers/objectSpread2")["default"]; var _typeof = require("@babel/runtime-corejs3/helpers/typeof")["default"]; var _concatInstanceProperty = require("@babel/runtime-corejs3/core-js-stable/instance/concat"); var _JSON$stringify = require("@babel/runtime-corejs3/core-js-stable/json/stringify"); var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map"); var _WeakSet = require("@babel/runtime-corejs3/core-js-stable/weak-set"); var _inherits = require("@babel/runtime-corejs3/helpers/inherits")["default"]; var _createSuper = require("@babel/runtime-corejs3/helpers/createSuper")["default"]; var _wrapNativeSuper = require("@babel/runtime-corejs3/helpers/wrapNativeSuper")["default"]; var _createForOfIteratorHelper = require("@babel/runtime-corejs3/helpers/createForOfIteratorHelper")["default"]; var _classCallCheck = require("@babel/runtime-corejs3/helpers/classCallCheck")["default"]; var _createClass = require("@babel/runtime-corejs3/helpers/createClass")["default"]; var _classPrivateFieldInitSpec = require("@babel/runtime-corejs3/helpers/classPrivateFieldInitSpec")["default"]; var _classPrivateFieldGet = require("@babel/runtime-corejs3/helpers/classPrivateFieldGet")["default"]; var _classPrivateFieldSet = require("@babel/runtime-corejs3/helpers/classPrivateFieldSet")["default"]; var _validators = new _WeakMap(); var ValidatorHandler = function () { function ValidatorHandler(validators) { _classCallCheck(this, ValidatorHandler); _classPrivateFieldInitSpec(this, _validators, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _validators, validators); } _createClass(ValidatorHandler, [{ key: "validate", value: function validate(data) { var validators = _classPrivateFieldGet(this, _validators); var _iterator = _createForOfIteratorHelper(validators), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var validator = _step.value; validator.validate(data); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }]); return ValidatorHandler; }(); var InvalidTypeError = function (_Error) { _inherits(InvalidTypeError, _Error); var _super = _createSuper(InvalidTypeError); function InvalidTypeError(message) { var _this; _classCallCheck(this, InvalidTypeError); _this = _super.call(this, message); _this.name = 'Invalid Type'; return _this; } return _createClass(InvalidTypeError); }(_wrapNativeSuper(Error)); var DoesNotContainPropertyError = function (_Error2) { _inherits(DoesNotContainPropertyError, _Error2); var _super2 = _createSuper(DoesNotContainPropertyError); function DoesNotContainPropertyError(message) { var _this2; _classCallCheck(this, DoesNotContainPropertyError); _this2 = _super2.call(this, message); _this2.name = 'Does not contain property'; return _this2; } return _createClass(DoesNotContainPropertyError); }(_wrapNativeSuper(Error)); var _requiredPropertyName = new _WeakMap(); var ContainsPropertyValidator = function () { function ContainsPropertyValidator(requiredPropertyName) { _classCallCheck(this, ContainsPropertyValidator); _classPrivateFieldInitSpec(this, _requiredPropertyName, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _requiredPropertyName, requiredPropertyName); } _createClass(ContainsPropertyValidator, [{ key: "getRequiredPropertyName", value: function getRequiredPropertyName() { return _classPrivateFieldGet(this, _requiredPropertyName); } }, { key: "validate", value: function validate(data) { var requiredPropertyName = _classPrivateFieldGet(this, _requiredPropertyName); if (!(requiredPropertyName in data)) { var _context; var message = _concatInstanceProperty(_context = "".concat(data, " should include the ")).call(_context, requiredPropertyName, " property"); throw new DoesNotContainPropertyError(message); } } }]); return ContainsPropertyValidator; }(); var _validType = new _WeakMap(); var IsTypeValidator = function () { function IsTypeValidator(validType) { _classCallCheck(this, IsTypeValidator); _classPrivateFieldInitSpec(this, _validType, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _validType, validType); } _createClass(IsTypeValidator, [{ key: "validate", value: function validate(data) { var validType = _classPrivateFieldGet(this, _validType); if (_typeof(data) !== validType) { var _context2; var message = _concatInstanceProperty(_context2 = "".concat(data, " should be ")).call(_context2, validType); throw new InvalidTypeError(message); } } }]); return IsTypeValidator; }(); var _containsPropertyValidator = new _WeakMap(); var _isTypeValidator = new _WeakMap(); var PropertyContainsValidTypeValidator = function () { function PropertyContainsValidTypeValidator(containsPropertyValidator, isTypeValidator) { _classCallCheck(this, PropertyContainsValidTypeValidator); _classPrivateFieldInitSpec(this, _containsPropertyValidator, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _isTypeValidator, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _containsPropertyValidator, containsPropertyValidator); _classPrivateFieldSet(this, _isTypeValidator, isTypeValidator); } _createClass(PropertyContainsValidTypeValidator, [{ key: "validate", value: function validate(data) { var containsPropertyValidator = _classPrivateFieldGet(this, _containsPropertyValidator); containsPropertyValidator.validate(data); var requiredPropertyName = containsPropertyValidator.getRequiredPropertyName(); var requiredValue = data[requiredPropertyName]; _classPrivateFieldGet(this, _isTypeValidator).validate(requiredValue); } }]); return PropertyContainsValidTypeValidator; }(); var _serializers = new _WeakMap(); var ReplacerBuilder = function () { function ReplacerBuilder() { _classCallCheck(this, ReplacerBuilder); _classPrivateFieldInitSpec(this, _serializers, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _serializers, {}); } _createClass(ReplacerBuilder, [{ key: "setSerializers", value: function setSerializers(serializers) { _classPrivateFieldSet(this, _serializers, serializers); } }, { key: "build", value: function build() { var serializers = _classPrivateFieldGet(this, _serializers); var getSerializerType = function getSerializerType(unserializedData) { var type = _typeof(unserializedData); return type === 'object' ? unserializedData.constructor.name : type; }; return function replacer(key, value) { var unserializedData = this[key]; var serializerType = getSerializerType(unserializedData); var serializer = serializers[serializerType]; if (serializer) { var serializedData = serializer.serialize(unserializedData); return _objectSpread({ __typeof__: serializerType }, serializedData); } return value; }; } }]); return ReplacerBuilder; }(); var _serializers2 = new _WeakMap(); var ReviverBuilder = function () { function ReviverBuilder() { _classCallCheck(this, ReviverBuilder); _classPrivateFieldInitSpec(this, _serializers2, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _serializers2, {}); } _createClass(ReviverBuilder, [{ key: "setSerializers", value: function setSerializers(serializers) { _classPrivateFieldSet(this, _serializers2, serializers); } }, { key: "build", value: function build() { var serializers = _classPrivateFieldGet(this, _serializers2); return function reviver(key, value) { var serializerType = value.__typeof__; var serializer = serializers[serializerType]; if (serializer) { return serializer.parse(value); } return value; }; } }]); return ReviverBuilder; }(); var _serializers3 = new _WeakMap(); var _serializerValidatorHandler = new _WeakMap(); var _validate = new _WeakSet(); var SerializerHandler = function () { function SerializerHandler(_serializerValidatorHandler2) { _classCallCheck(this, SerializerHandler); _classPrivateMethodInitSpec(this, _validate); _classPrivateFieldInitSpec(this, _serializers3, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _serializerValidatorHandler, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _serializers3, {}); _classPrivateFieldSet(this, _serializerValidatorHandler, _serializerValidatorHandler2); } _createClass(SerializerHandler, [{ key: "getSerializers", value: function getSerializers() { var serializers = _classPrivateFieldGet(this, _serializers3); return _objectSpread({}, serializers); } }, { key: "addSerializer", value: function addSerializer(serializer) { _classPrivateMethodGet(this, _validate, _validate2).call(this, serializer); var serializerType = serializer.getSerializerType(); _classPrivateFieldGet(this, _serializers3)[serializerType] = serializer; } }]); return SerializerHandler; }(); function _validate2(serializer) { var serializerValidatorHandler = _classPrivateFieldGet(this, _serializerValidatorHandler); if (serializerValidatorHandler) { serializerValidatorHandler.validate(serializer); } } var _replacerBuilder = new _WeakMap(); var _reviverBuilder = new _WeakMap(); var _serializerHandler = new _WeakMap(); var _replacer = new _WeakMap(); var _reviver = new _WeakMap(); var _refreshJsonSerializer = new _WeakSet(); var _refreshReplacer = new _WeakSet(); var _refreshReviver = new _WeakSet(); var _installSerializers = new _WeakSet(); var _addSerializer = new _WeakSet(); var JsonSerializer = function () { function JsonSerializer(_replacerBuilder2, _reviverBuilder2, _serializerHandler2) { _classCallCheck(this, JsonSerializer); _classPrivateMethodInitSpec(this, _addSerializer); _classPrivateMethodInitSpec(this, _installSerializers); _classPrivateMethodInitSpec(this, _refreshReviver); _classPrivateMethodInitSpec(this, _refreshReplacer); _classPrivateMethodInitSpec(this, _refreshJsonSerializer); _classPrivateFieldInitSpec(this, _replacerBuilder, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _reviverBuilder, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _serializerHandler, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _replacer, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(this, _reviver, { writable: true, value: void 0 }); _classPrivateFieldSet(this, _replacerBuilder, _replacerBuilder2); _classPrivateFieldSet(this, _reviverBuilder, _reviverBuilder2); _classPrivateFieldSet(this, _serializerHandler, _serializerHandler2); } _createClass(JsonSerializer, [{ key: "getSerializers", value: function getSerializers() { var serializerHandler = _classPrivateFieldGet(this, _serializerHandler); return serializerHandler.getSerializers(); } }, { key: "installSerializersAndRefreshJsonSerializer", value: function installSerializersAndRefreshJsonSerializer(serializersInstaller) { var installOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; _classPrivateMethodGet(this, _installSerializers, _installSerializers2).call(this, serializersInstaller, installOptions); _classPrivateMethodGet(this, _refreshJsonSerializer, _refreshJsonSerializer2).call(this); } }, { key: "addSerializerAndRefreshJsonSerializer", value: function addSerializerAndRefreshJsonSerializer(serializer) { _classPrivateMethodGet(this, _addSerializer, _addSerializer2).call(this, serializer); _classPrivateMethodGet(this, _refreshJsonSerializer, _refreshJsonSerializer2).call(this); } }, { key: "serialize", value: function serialize(unserializedData, space) { var replacer = _classPrivateFieldGet(this, _replacer); return _JSON$stringify(unserializedData, replacer, space); } }, { key: "parse", value: function parse(serializedData) { var reviver = _classPrivateFieldGet(this, _reviver); return JSON.parse(serializedData, reviver); } }]); return JsonSerializer; }(); function _refreshJsonSerializer2() { var serializerHandler = _classPrivateFieldGet(this, _serializerHandler); var serializers = serializerHandler.getSerializers(); _classPrivateMethodGet(this, _refreshReplacer, _refreshReplacer2).call(this, serializers); _classPrivateMethodGet(this, _refreshReviver, _refreshReviver2).call(this, serializers); } function _refreshReplacer2(serializers) { var replacerBuilder = _classPrivateFieldGet(this, _replacerBuilder); replacerBuilder.setSerializers(serializers); _classPrivateFieldSet(this, _replacer, replacerBuilder.build()); } function _refreshReviver2(serializers) { var reviverBuilder = _classPrivateFieldGet(this, _reviverBuilder); reviverBuilder.setSerializers(serializers); _classPrivateFieldSet(this, _reviver, reviverBuilder.build()); } function _installSerializers2(serializersInstaller, installOptions) { var serializerHandler = _classPrivateFieldGet(this, _serializerHandler); serializersInstaller.install(serializerHandler, installOptions); } function _addSerializer2(serializer) { var serializerHandler = _classPrivateFieldGet(this, _serializerHandler); serializerHandler.addSerializer(serializer); } var JsonSerializerFactory = function () { function JsonSerializerFactory() { _classCallCheck(this, JsonSerializerFactory); } _createClass(JsonSerializerFactory, null, [{ key: "createJsonSerializer", value: function createJsonSerializer() { var serializerValidators = [new PropertyContainsValidTypeValidator(new ContainsPropertyValidator('getSerializerType'), new IsTypeValidator('function')), new PropertyContainsValidTypeValidator(new ContainsPropertyValidator('serialize'), new IsTypeValidator('function')), new PropertyContainsValidTypeValidator(new ContainsPropertyValidator('parse'), new IsTypeValidator('function'))]; return new JsonSerializer(new ReplacerBuilder(), new ReviverBuilder(), new SerializerHandler(new ValidatorHandler(serializerValidators))); } }]); return JsonSerializerFactory; }(); exports.JsonSerializerFactory = JsonSerializerFactory; //# sourceMappingURL=index.cjs.map