@triviality/serializer
Version:
Typescript serializer with an interface
75 lines • 3.13 kB
JavaScript
;
/// <reference path="./transit-immutable-js.d.ts" />
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var transit = __importStar(require("transit-immutable-js"));
var immutable_1 = require("immutable");
var __1 = require("..");
var TransitJSSerializer = /** @class */ (function () {
function TransitJSSerializer(records, extraHandlers) {
var e_1, _a;
if (extraHandlers === void 0) { extraHandlers = []; }
/**
* Simple map to verify you cannot give a record with the same name.
*/
var recordsTypes = {};
try {
for (var records_1 = __values(records), records_1_1 = records_1.next(); !records_1_1.done; records_1_1 = records_1.next()) {
var record = records_1_1.value;
var descriptiveName = immutable_1.Record.getDescriptiveName(new record());
if (descriptiveName === '' || descriptiveName.toLowerCase() === 'record') {
throw new __1.SerializerError("wrong descriptiveName record name " + descriptiveName);
}
if (typeof recordsTypes[descriptiveName] !== 'undefined') {
throw new __1.SerializerError("Records with this name already given " + descriptiveName);
}
recordsTypes[descriptiveName] = record;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (records_1_1 && !records_1_1.done && (_a = records_1.return)) _a.call(records_1);
}
finally { if (e_1) throw e_1.error; }
}
var withRecords = transit.withRecords(records);
this.recordTransit = withRecords.withExtraHandlers(Array.from(extraHandlers));
}
TransitJSSerializer.prototype.serialize = function (data) {
try {
return this.recordTransit.toJSON(data);
}
catch (e) {
throw __1.SerializerError.fromError(e);
}
};
TransitJSSerializer.prototype.deserialize = function (json) {
try {
return this.recordTransit.fromJSON(json);
}
catch (e) {
throw __1.SerializerError.fromError(e);
}
};
return TransitJSSerializer;
}());
exports.TransitJSSerializer = TransitJSSerializer;
//# sourceMappingURL=TransitJSSerializer.js.map