///
/**
* jda (jsdata): JData annotations for JavaScript, in browsers and Node.js
* (https://neurojson.org/jdata, https://github.com/NeuroJSON/jsdata)
*
* const jd = require('jda'); // or import jd from 'jda', or the jdata global
* const text = jd.stringify({vol: new Float32Array(64)}, {compression: 'zlib'});
* const data = jd.parse(text); // NDArray, Map, ... again
*
* Copyright (c) 2019-2026 Qianqian Fang . BSD 3-clause license.
*/
import { decode, DecodeOptions, JDataDecoder } from './decode';
import { encode, EncodeOptions, JDataEncoder } from './encode';
import { registerCodec, getCodec, codecNames, base64ToBytes, bytesToBase64, lz4Decode, lz4Encode, shuffle, unshuffle, Codec } from './codecs';
import { NDArray, SparseArray, nest } from './ndarray';
import { zipBytes } from './decode';
import { unzipPayloads, zipPayloads, ZipJob } from './payloads';
import * as zlibmt from './zlibmt';
import { ThreadOptions, adler32, closeWorkers } from './zlibmt';
export {
decode, encode, DecodeOptions, EncodeOptions, JDataDecoder, JDataEncoder, registerCodec, codecNames, Codec,
base64ToBytes, bytesToBase64, lz4Decode, lz4Encode, shuffle, unshuffle, NDArray, SparseArray, nest,
ThreadOptions, zlibmt, adler32, closeWorkers
};
export const version = '1.0.0';
/** Values of JSON text with JData annotations. */
export function parse(text: string, opts: DecodeOptions = {}): any {
return decode(JSON.parse(text), opts);
}
/** JSON replacer for the values JSON.stringify cannot write itself. */
function replacer(this: any, _k: string, v: any): any {
if (typeof v === 'bigint') {
return v >= -9007199254740991 && v <= 9007199254740991 ? Number(v) : v.toString();
}
if (ArrayBuffer.isView(v) && !(v instanceof DataView)) {
return Array.from(v as any, (x: any) => (typeof x === 'bigint' ? Number(x) : x));
}
if (v instanceof Map) {
return Array.from(v.entries());
}
if (v && typeof v === 'object' && v.selection && typeof v.tolist === 'function') {
return v.tolist(); // (numjs)
}
return v;
}
/** JSON text with JData annotations of a value. */
export function stringify(value: any, opts: EncodeOptions = {}, space?: string | number): string {
// (encode gives JSON values only: no replacer, which JSON.stringify would call for every number)
return JSON.stringify(encode(value, Object.assign({}, opts, { target: 'json' })), null, space);
}
declare const require: any;
function bjdModule(m: any): any {
if (m) {
return m;
}
const g: any = typeof globalThis !== 'undefined' ? globalThis : {};
if (g.bjdata && typeof g.bjdata.loadb === 'function') {
return g.bjdata;
}
try {
if (typeof require === 'function') {
return require('bjd');
}
} catch (e) {
// (none installed)
}
throw new Error('jdata: BJData needs bjd (npm install bjd, the bjdata global, or the bjd option)');
}
/** Values of BJData bytes with JData annotations (needs bjd: installed, the bjdata global, or
* the bjd option). */
export function loadbj(bytes: any, opts: DecodeOptions & { bjd?: any; bjdOptions?: any } = {}): any {
return decode(bjdModule(opts.bjd).loadb(bytes, opts.bjdOptions || {}), opts);
}
/** BJData bytes with JData annotations of a value (needs bjd). */
export function savebj(value: any, opts: EncodeOptions & { bjd?: any; bjdOptions?: any } = {}): Uint8Array {
return bjdModule(opts.bjd).dumpb(encode(value, Object.assign({}, opts, { target: 'bjdata' })), opts.bjdOptions || {});
}
// ---- async: zlib and gzip payloads on several threads (zlibmt) -------------------------------
/** decode(), with the zlib and gzip payloads inflated on threads. */
export async function decodeAsync(value: any, opts: DecodeOptions & ThreadOptions = {}): Promise {
const done = await unzipPayloads(value, opts.codecs, (z) => zipBytes(z, opts.base64 !== false, false), opts);
return new JDataDecoder(opts, done).value(value);
}
/** encode(), with the zlib and gzip payloads deflated on threads (zlib in zlibmt blocks). */
export async function encodeAsync(value: any, opts: EncodeOptions & ThreadOptions = {}): Promise {
const jobs: ZipJob[] = [];
const out = new JDataEncoder(Object.assign({ zlibmt: true }, opts), jobs).value(value);
await zipPayloads(jobs, opts);
return out;
}
/** parse(), with the payloads inflated on threads. */
export function parseAsync(text: string, opts: DecodeOptions & ThreadOptions = {}): Promise {
return decodeAsync(JSON.parse(text), opts);
}
/** stringify(), with the payloads deflated on threads. */
export async function stringifyAsync(value: any, opts: EncodeOptions & ThreadOptions = {}, space?: string | number): Promise {
return JSON.stringify(await encodeAsync(value, Object.assign({}, opts, { target: 'json' })), null, space);
}
/** loadbj(), with the payloads inflated on threads. */
export function loadbjAsync(bytes: any, opts: DecodeOptions & ThreadOptions & { bjd?: any; bjdOptions?: any } = {}): Promise {
return decodeAsync(bjdModule(opts.bjd).loadb(bytes, opts.bjdOptions || {}), opts);
}
/** savebj(), with the payloads deflated on threads. */
export async function savebjAsync(value: any, opts: EncodeOptions & ThreadOptions & { bjd?: any; bjdOptions?: any } = {}): Promise {
const m = bjdModule(opts.bjd);
return m.dumpb(await encodeAsync(value, Object.assign({}, opts, { target: 'bjdata' })), opts.bjdOptions || {});
}
/**
* The jda 0.x class: new jdata(data, options).encode().tojson(), new jdata(obj).decode().data;
* options: {compression, base64, ...} as for encode() and decode().
*/
export default class jdata {
data: any;
opt: any;
base64: boolean;
constructor(data: any = {}, options: any = {}) {
this.data = data;
this.opt = options;
this.base64 = options.base64 !== undefined ? options.base64 : true;
}
encode(): this {
this.data = encode(this.data, this.opt);
return this;
}
decode(): this {
this.data = decode(this.data, this.opt);
return this;
}
tojson(space: string | number = '\t'): string {
return JSON.stringify(this.data, replacer, space);
}
zip(buf: any, method: string = 'zlib'): any {
const b = buf instanceof Uint8Array ? buf : ArrayBuffer.isView(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : new Uint8Array(buf);
const z = (getCodec(method) as any).encode(b, {});
return this.base64 ? bytesToBase64(z) : z;
}
unzip(data: any, method: string = 'zlib'): Uint8Array {
const b = typeof data === 'string' ? (this.base64 ? base64ToBytes(data) : Uint8Array.from(data, (c: string) => c.charCodeAt(0))) : data;
return getCodec(method).decode(b, undefined, {});
}
base64ToBytes(s: string): Uint8Array {
return base64ToBytes(s);
}
bytesToBase64(b: Uint8Array): string {
return bytesToBase64(b);
}
static decode = decode;
static encode = encode;
static parse = parse;
static stringify = stringify;
static loadbj = loadbj;
static savebj = savebj;
static decodeAsync = decodeAsync;
static encodeAsync = encodeAsync;
static parseAsync = parseAsync;
static stringifyAsync = stringifyAsync;
static loadbjAsync = loadbjAsync;
static savebjAsync = savebjAsync;
static zlibmt = zlibmt;
static adler32 = adler32;
static closeWorkers = closeWorkers;
static registerCodec = registerCodec;
static codecNames = codecNames;
static NDArray = NDArray;
static SparseArray = SparseArray;
static base64ToBytes = base64ToBytes;
static bytesToBase64 = bytesToBase64;
static version = version;
}