json-type-cli
Version:
High-performance JSON Pointer implementation
29 lines (28 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliCodecJson4 = void 0;
const JsonDecoder_1 = require("@jsonjoy.com/json-pack/lib/json/JsonDecoder");
const JsonEncoder_1 = require("@jsonjoy.com/json-pack/lib/json/JsonEncoder");
const bufferToUint8Array_1 = require("@jsonjoy.com/util/lib/buffers/bufferToUint8Array");
/**
* JSON codec with 4 space pretty-printing.
*/
class CliCodecJson4 {
constructor(writer) {
this.writer = writer;
this.id = 'json4';
this.description = 'JSON codec with 4 space pretty-printing';
this.encoder = new JsonEncoder_1.JsonEncoder(writer);
this.decoder = new JsonDecoder_1.JsonDecoder();
}
encode(value) {
const uint8 = this.encoder.encode(value);
const pojo = JSON.parse(Buffer.from(uint8).toString('utf8'));
const json = JSON.stringify(pojo, null, 4) + '\n';
return (0, bufferToUint8Array_1.bufferToUint8Array)(Buffer.from(json, 'utf8'));
}
decode(bytes) {
return this.decoder.read(bytes);
}
}
exports.CliCodecJson4 = CliCodecJson4;