UNPKG

@jsonjoy.com/json-type

Version:

High-performance JSON Pointer implementation

47 lines (46 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnyType = void 0; const CborEncoderCodegenContext_1 = require("../../codegen/binary/CborEncoderCodegenContext"); const MessagePackEncoderCodegenContext_1 = require("../../codegen/binary/MessagePackEncoderCodegenContext"); const AbsType_1 = require("./AbsType"); class AnyType extends AbsType_1.AbsType { constructor(schema) { super(); this.schema = schema; } codegenValidator(ctx, path, r) { ctx.emitCustomValidators(this, path, r); } codegenJsonTextEncoder(ctx, value) { ctx.js(/* js */ `s += stringify(${value.use()});`); } codegenBinaryEncoder(ctx, value) { ctx.codegen.link('Value'); const r = ctx.codegen.var(value.use()); ctx.codegen.if(`${r} instanceof Value`, () => { ctx.codegen.if(`${r}.type`, () => { const type = ctx instanceof CborEncoderCodegenContext_1.CborEncoderCodegenContext ? 0 /* EncodingFormat.Cbor */ : ctx instanceof MessagePackEncoderCodegenContext_1.MessagePackEncoderCodegenContext ? 1 /* EncodingFormat.MsgPack */ : 2 /* EncodingFormat.Json */; ctx.js(`${r}.type.encoder(${type})(${r}.data, encoder);`); }, () => { ctx.js(/* js */ `encoder.writeAny(${r}.data);`); }); }, () => { ctx.js(/* js */ `encoder.writeAny(${r});`); }); } codegenCborEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenMessagePackEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } codegenJsonEncoder(ctx, value) { this.codegenBinaryEncoder(ctx, value); } } exports.AnyType = AnyType;