ts-std-lib
Version:
A standard library for typescript
36 lines • 1.36 kB
JavaScript
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferJsonSerializer = void 0;
const IJsonSerializer_1 = require("./IJsonSerializer");
const Type_1 = require("../../Type");
const UnexpectedJsonError_1 = require("./UnexpectedJsonError");
const IsJsonSerializable_1 = require("./IsJsonSerializable");
const Equality_1 = require("../../Equality");
/**
* Json serializer for the native Buffer class
*/
class BufferJsonSerializer {
constructor() {
this[_a] = true;
}
serialize(object) {
return object.toJSON();
}
deserialize(json) {
if (!Type_1.Type.isObject(json) || Type_1.Type.isArray(json) || (0, IsJsonSerializable_1.isJsonSerializable)(json)) {
throw new UnexpectedJsonError_1.UnexpectedJsonError(json);
}
const { type, data } = json;
if (!Type_1.Type.isString(type) || !(0, Equality_1.equalTo)(type, 'Buffer')) {
throw new UnexpectedJsonError_1.UnexpectedJsonError(json);
}
if (!Type_1.Type.isArray(data)) {
throw new UnexpectedJsonError_1.UnexpectedJsonError(json);
}
return Buffer.from(data);
}
}
exports.BufferJsonSerializer = BufferJsonSerializer;
_a = IJsonSerializer_1.jsonSerializer;
//# sourceMappingURL=BufferJsonSerializer.js.map
;