basant307/AI_Governance_Project
048
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.JsonEncoder = void 0;4const toBase64Bin_1 = require("@jsonjoy.com/base64/lib/toBase64Bin");5class JsonEncoder {6 constructor(writer) {7 this.writer = writer;8 }9 encode(value) {10 const writer = this.writer;11 writer.reset();12 this.writeAny(value);13 return writer.flush();14 }15 writeUnknown(value) {16 this.writeNull();17 }18 writeAny(value) {19 switch (typeof value) {20 case 'boolean':21 return this.writeBoolean(value);22 case 'number':23 return this.writeNumber(value);24 case 'string':25 return this.writeStr(value);26 case 'object': {27 if (value === null)28 return this.writeNull();29 const constructor = value.constructor;30 switch (constructor) {31 case Object:32 return this.writeObj(value);33 case Array:34 return this.writeArr(value);35 case Uint8Array:36 return this.writeBin(value);37 default:38 if (value instanceof Uint8Array)39 return this.writeBin(value);40 if (Array.isArray(value))41 return this.writeArr(value);42 return this.writeUnknown(value);43 }44 }45 case 'undefined': {46 return this.writeUndef();47 }48 default:49 return this.writeUnknown(value);50 }51 }52 writeNull() {53 this.writer.u32(0x6e756c6c);54 }55 writeUndef() {56 const writer = this.writer;57 const length = 35;58 writer.ensureCapacity(length);59 const view = writer.view;60 let x = writer.x;61 view.setUint32(x, 577003892);62 x += 4;63 view.setUint32(x, 1631215984);64 x += 4;65 view.setUint32(x, 1886153059);66 x += 4;67 view.setUint32(x, 1635019119);68 x += 4;69 view.setUint32(x, 1848599394);70 x += 4;71 view.setUint32(x, 1869753442);72 x += 4;73 view.setUint32(x, 1634952502);74 x += 4;75 view.setUint32(x, 876296567);76 x += 4;77 view.setUint16(x, 15677);78 x += 2;79 writer.uint8[x++] = 0x22;80 writer.x = x;81 }82 writeBoolean(bool) {83 if (bool)84 this.writer.u32(0x74727565);85 else86 this.writer.u8u32(0x66, 0x616c7365);87 }88 writeNumber(num) {89 const str = num.toString();90 this.writer.ascii(str);91 }92 writeInteger(int) {93 this.writeNumber(int >> 0 === int ? int : Math.trunc(int));94 }95 writeUInteger(uint) {96 this.writeInteger(uint < 0 ? -uint : uint);97 }98 writeFloat(float) {99 this.writeNumber(float);100 }101 writeBin(buf) {102 const writer = this.writer;103 const length = buf.length;104 writer.ensureCapacity(38 + 3 + (length << 1));105 const view = writer.view;106 let x = writer.x;107 view.setUint32(x, 577003892);108 x += 4;109 view.setUint32(x, 1631215984);110 x += 4;111 view.setUint32(x, 1886153059);112 x += 4;113 view.setUint32(x, 1635019119);114 x += 4;115 view.setUint32(x, 1848602467);116 x += 4;117 view.setUint32(x, 1952805933);118 x += 4;119 view.setUint32(x, 1937011301);120 x += 4;121 view.setUint32(x, 1634548578);122 x += 4;123 view.setUint32(x, 1634952502);124 x += 4;125 view.setUint16(x, 13356);126 x += 2;127 x = (0, toBase64Bin_1.toBase64Bin)(buf, 0, length, view, x);128 writer.uint8[x++] = 0x22;129 writer.x = x;130 }131 writeStr(str) {132 const writer = this.writer;133 const length = str.length;134 writer.ensureCapacity(length * 4 + 2);135 if (length < 256) {136 let x = writer.x;137 const uint8 = writer.uint8;138 uint8[x++] = 0x22;139 for (let i = 0; i < length; i++) {140 const code = str.charCodeAt(i);141 switch (code) {142 case 34:143 case 92:144 uint8[x++] = 0x5c;145 break;146 }147 if (code < 32 || code > 126) {148 writer.utf8(JSON.stringify(str));149 return;150 }151 else152 uint8[x++] = code;153 }154 uint8[x++] = 0x22;155 writer.x = x;156 return;157 }158 writer.utf8(JSON.stringify(str));159 }160 writeAsciiStr(str) {161 const length = str.length;162 const writer = this.writer;163 writer.ensureCapacity(length * 2 + 2);164 const uint8 = writer.uint8;165 let x = writer.x;166 uint8[x++] = 0x22;167 for (let i = 0; i < length; i++) {168 const code = str.charCodeAt(i);169 switch (code) {170 case 34:171 case 92:172 uint8[x++] = 0x5c;173 break;174 }175 uint8[x++] = code;176 }177 uint8[x++] = 0x22;178 writer.x = x;179 }180 writeArr(arr) {181 const writer = this.writer;182 writer.u8(0x5b);183 const length = arr.length;184 const last = length - 1;185 for (let i = 0; i < last; i++) {186 this.writeAny(arr[i]);187 writer.u8(0x2c);188 }189 if (last >= 0)190 this.writeAny(arr[last]);191 writer.u8(0x5d);192 }193 writeArrSeparator() {194 this.writer.u8(0x2c);195 }196 writeObj(obj) {197 const writer = this.writer;198 const keys = Object.keys(obj);199 const length = keys.length;200 if (!length)201 return writer.u16(0x7b7d);202 writer.u8(0x7b);203 for (let i = 0; i < length; i++) {204 const key = keys[i];205 const value = obj[key];206 this.writeStr(key);207 writer.u8(0x3a);208 this.writeAny(value);209 writer.u8(0x2c);210 }211 writer.uint8[writer.x - 1] = 0x7d;212 }213 writeObjSeparator() {214 this.writer.u8(0x2c);215 }216 writeObjKeySeparator() {217 this.writer.u8(0x3a);218 }219 writeStartStr() {220 throw new Error('Method not implemented.');221 }222 writeStrChunk(str) {223 throw new Error('Method not implemented.');224 }225 writeEndStr() {226 throw new Error('Method not implemented.');227 }228 writeStartBin() {229 throw new Error('Method not implemented.');230 }231 writeBinChunk(buf) {232 throw new Error('Method not implemented.');233 }234 writeEndBin() {235 throw new Error('Method not implemented.');236 }237 writeStartArr() {238 this.writer.u8(0x5b);239 }240 writeArrChunk(item) {241 throw new Error('Method not implemented.');242 }243 writeEndArr() {244 this.writer.u8(0x5d);245 }246 writeStartObj() {247 this.writer.u8(0x7b);248 }249 writeObjChunk(key, value) {250 throw new Error('Method not implemented.');251 }252 writeEndObj() {253 this.writer.u8(0x7d);254 }255}256exports.JsonEncoder = JsonEncoder;257//# sourceMappingURL=JsonEncoder.js.map