DEV Community

ViperT
ViperT

Posted on

JOYSON - Advanced Data Serialization In JavaScript

JOYSON: Revolutionizing JSON Serialization with Advanced Data Handling

JOYSON LOGO

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.

SECTION

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

SECTION

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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️