UNPKG

@proddata/node-cratedb

Version:
88 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Serializer = void 0; const ColumnTypes_js_1 = require("./utils/ColumnTypes.js"); const Error_js_1 = require("./utils/Error.js"); class Serializer { static serialize(obj) { return JSON.stringify(obj, this.replacer); } static replacer(_, value) { if (typeof value === 'bigint') { return JSON.rawJSON(value.toString()); } if (value instanceof Map) { return Object.fromEntries(value); } if (value instanceof Set) { return Array.from(value); } return value; } static deserialize(str, config) { try { return this._deserialize(str, config); } catch { throw new Error_js_1.DeserializationError('Deserialization of response body failed'); } } static _deserialize(str, config) { const obj = config.long === 'bigint' ? JSON.parse(str, this.reviver) : JSON.parse(str); obj.col_types?.forEach((type, index) => { // Extract the base type even from nested arrays const baseType = this.extractBaseType(type); switch (baseType) { case ColumnTypes_js_1.ColumnTypes.BIGINT: if (config.long === 'bigint') { obj.rows?.forEach((row) => { row[index] = this.recursiveConvert(row[index], (val) => BigInt(String(val))); }); } break; case ColumnTypes_js_1.ColumnTypes.DATE: if (config.date === 'date') { obj.rows?.forEach((row) => { row[index] = this.recursiveConvert(row[index], (val) => new Date(val)); }); } break; case ColumnTypes_js_1.ColumnTypes.TIMESTAMP_WITH_TIME_ZONE: case ColumnTypes_js_1.ColumnTypes.TIMESTAMP_WITHOUT_TIME_ZONE: if (config.timestamp === 'date') { obj.rows?.forEach((row) => { row[index] = this.recursiveConvert(row[index], (val) => new Date(val)); }); } break; default: // No special handling for other types } }); return obj; } static extractBaseType(type) { return Array.isArray(type) ? this.extractBaseType(type[1]) : type; } static recursiveConvert(cell, converter) { if (Array.isArray(cell)) { return cell.map((item) => this.recursiveConvert(item, converter)); } if (typeof cell === 'number') { return converter(cell); } return cell; } static reviver(_, value, context = null) { //if number is greater than Number.MAX_SAFE_INTEGER and not a float if (typeof value === 'number' && value > Number.MAX_SAFE_INTEGER && context !== null && !context.source.includes('.')) { return BigInt(context.source); } return value; } } exports.Serializer = Serializer; //# sourceMappingURL=Serializer.js.map