UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

52 lines (51 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConType = void 0; const constants_1 = require("../../constants"); const AbsType_1 = require("./AbsType"); const deepEqualCodegen_1 = require("@jsonjoy.com/util/lib/json-equal/deepEqualCodegen"); class ConType extends AbsType_1.AbsType { constructor(schema) { super(); this.schema = schema; this.__json = JSON.stringify(schema.value); } value() { return this.schema.value; } getOptions() { const { kind, value, ...options } = this.schema; return options; } codegenValidator(ctx, path, r) { const value = this.schema.value; const equals = (0, deepEqualCodegen_1.deepEqualCodegen)(value); const fn = ctx.codegen.addConstant(equals); ctx.js(`if (!${fn}(${r})) return ${ctx.err(constants_1.ValidationError.CONST, path)}`); ctx.emitCustomValidators(this, path, r); } codegenJsonTextEncoder(ctx, value) { ctx.writeText(JSON.stringify(this.schema.value)); } codegenBinaryEncoder(ctx, value) { ctx.blob(ctx.gen((encoder) => { encoder.writeAny(this.schema.value); })); } codegenCborEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenMessagePackEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenJsonEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } toJson(value, system = this.system) { return this.__json; } toString(tab = '') { return `${super.toString(tab)}${JSON.stringify(this.schema.value)}`; } } exports.ConType = ConType;