You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
joyson is a JavaScript module designed for efficient encoding and decoding of JSON objects, particularly adept at handling TypedArrays. Unlike standard JSON methods, joyson provides additional pack and unpack methods for more memory-efficient handling of large data structures, including support for binary data serialization.
Key Differences
Standard JSON Methods: stringify and parse convert objects to/from JSON strings.
Extended Binary Methods: pack and unpack handle serialization/deserialization of data into/from a compact binary format, beneficial for performance-intensive applications.
Features
TypedArray Support: Seamless encoding and decoding of TypedArrays within JSON objects.
Memory Efficiency: Optimized for minimal memory footprint during processing.
Binary Serialization: The pack method allows for compact binary serialization of data, while unpack restores it, ensuring efficient data handling.
Installation
Install joyson using npm:
npm install joyson
Usage
Import JOYSON from joyson:
importJOYSONfrom'joyson';
Using /dist/browser.min.js` enable you to directly use JOYSON like JSON
Encoding/Decoding an Object
constobject={test: "hello",data: [1,23,5,6,{"##": undefined,"##test": /regex/i,date: newDate(),table: [-0,111111111n,-666.777,newSet([1,2,"blue",{}]),newMap()],arr: Int16Array.of(-6,777,12),arr2: newUint8Array(9)},"hello here is asaitama I love JS"]};constencoded=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"]}`constdecoded=JOYSON.parse(encoded);console.log(object,encoded,decoded);
Packing/Unpacking Data
constyourObject={test: "hello",data: [1,23,5,6,{arr: Int16Array.of(-6,777,12),arr2: newUint8Array(9)}]};constpackedData=JOYSON.pack(yourObject);// Uint8Array of 151 BytesconstunpackedData=JOYSON.unpack(packedData);console.log(packedData,unpackedData);