JOYSON: Revolutionizing JSON Serialization with Advanced Data Handling
SOURCE: https://www.npmjs.com/package/joyson
Introduction
JOYSON
is a transformative JavaScript module, redefining the landscape of JSON object encoding and decoding. It's especially proficient with TypedArrays, offering memory-efficient handling of large data structures and binary data serialization.
Practical Usage in JavaScript
Encoding/Decoding an Object
Using JOYSON
, complex objects including those with TypedArrays, regular expressions, and other complex types can be efficiently encoded and decoded. Here's an example:
const object = {test: "hello", data: [1, 23, 5, 6, {"##": undefined, "##test": /regex/i, date: new Date(), table: [-0, 111111111n, -666.777, new Set([1, 2, "blue", {}]), new Map()], arr: Int16Array.of(-6, 777, 12), arr2: new Uint8Array(9)}, "hello here is asaitama I love JS"]};
const encoded = JOYSON.stringify(object);
// `{"test":"hello","data":[1,23,5,6,{"#$IyM=":"data:joyson/undefined;","#$IyN0ZXN0":"data:joyson/regexp;cmVnZXg=:aQ==","##date":"data:joyson/date;2023-12-25T00:33:37.935Z","table":["data:joyson/number;-0","data:joyson/bigint;111111111",-666.777,"data:joyson/set;WzEsMiwiYmx1ZSIse31d","data:joyson/map;W10="],"##arr":"data:joyson/int16array;base64,+v8JAwwA","##arr2":"data:joyson/uint8array;base64,AAAAAAAAAAAA"},"hello here is asaitama I love JS"]}`
const decoded = JOYSON.parse(encoded);
console.log(object, encoded, decoded);
Packing/Unpacking Data
For efficient binary serialization, JOYSON
's pack and unpack methods can be used as follows:
const yourObject = {test: "hello", data: [1, 23, 5, 6, {arr: Int16Array.of(-6, 777, 12), arr2: new Uint8Array(9)}]};
const packedData = JOYSON.pack(yourObject); // Uint8Array of 151 Bytes
const unpackedData = JOYSON.unpack(packedData);
console.log(packedData, unpackedData);
Conclusion
JOYSON
represents a significant advancement in the field of data serialization, offering unparalleled efficiency, speed, and versatility. Its development marks a key milestone in addressing the limitations of traditional JSON methods, making it an essential tool for modern web developers. It is yet faster than MessagePacker and CBOR most of the time, also more extensive than those.
Top comments (0)