ts-std-lib
Version:
A standard library for typescript
32 lines • 1.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonConverter = void 0;
const os_1 = require("os");
const Type_1 = require("../../Type");
/**
* Json converter to interchange JSON and objects using a serializer
*/
class JsonConverter {
constructor(_reviver, _replacer) {
this._reviver = _reviver;
this._replacer = _replacer;
}
parse(serializable, jsonString) {
try {
const json = JSON.parse(jsonString, this._reviver);
return serializable.fromJSON(json);
}
catch (error) {
if (Type_1.Type.isInstanceOf(SyntaxError, error)) {
throw new SyntaxError(`${error.message}${os_1.EOL}JSON: "${jsonString}"`);
}
throw error;
}
}
stringify(serializable) {
const json = serializable.toJSON();
return JSON.stringify(json, this._replacer);
}
}
exports.JsonConverter = JsonConverter;
//# sourceMappingURL=JsonConverter.js.map
;