5

GitHub - gildas-lormeau/yabson: Binary-encoded serialization of JavaScript objec...

 2 years ago
source link: https://github.com/gildas-lormeau/YaBSON
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

YaBSON

Binary-encoded serialization of JavaScript objects with generator-based parser and serializer

This library is designed to transfer large arbitrary amounts of data into chunks. The main goal is to provide a very simple and easily extensible API and implementation. This also illustrates pedagogically the interest of iterators and generators in JavaScript. Note that, like the JSON API, this library does not support circular references.

Example

import { getParser, getSerializer } from "yabson";
// Deno: import { getParser, getSerializer } from "https://deno.land/x/[email protected]";

const object = {
  array: [
    1,
    2,
    3.1415927,
    true,
    undefined,
    null,
    NaN,
    42n,
  ],
  typedArray: new Uint8Array([1, 2, 3]),
  misc: {
    date: new Date(),
    error: new Error("error"),
    regExp: /test/gi,
  },
  map: new Map([["key", "value"], [42, { value: "result" }]]),
  set: new Set([1, 2, 3]),
};

// `chunkSize` (optional) is the max. size in bytes of `chunk` in the for-of loop below
const serializer = getSerializer(object, { chunkSize: 16 });
const parser = getParser();

let result;
for (const chunk of serializer) {
  // `chunk` is a Uint8array of binary encoded data
  result = parser.next(chunk);
}
// displays a deep clone of `object`
console.log(result.value);

Install

npm install https://www.npmjs.com/package/yabson

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK