Lazily and easily serialize and deserialize variables
const lazycrate = require('laycrate').sync; // Use the synchronous version
function stringifiedClass() {
// Test class
class TestClass {
constructor() {
this.creationTime = new Date();
}
getCreationTime() {
return this.creationTime;
}
}
const test = new TestClass();
return lazycrate.box(test); // Convert our object into a string!
}
const stringClass = stringifiedClass(); // Our class is now converted into a string
console.log(stringClass);
const revivedClass = lazycrate.unbox(stringClass); // Convert the string back into the class!
console.log(revivedClass.getCreationTime().toLocaleDateString()); // This works!
npm install lazycrate
- Serialize and deserialize variables
- Accepts primitives and objects
- Compress serialized objects
- Synchronous and asynchronous boxing/unboxing
object
:<Variable>
Variable to be serialized- Returns:
<String>
Serialized version of the variable. Can be compressed.
string
:<String>
|<Buffer>
Serialized content- Returns:
<Variable>
Deserialized variable created from boxing
const { unbox, box } = require('lazycrate').sync;
let unserialized = {
content: 'Hello World'
}
let serialized = box(unserialized);
console.log(unbox(serialized));
/*
{
content: 'Hello World'
}
*/
For the most part, the asynchronous code is available due to the usage of Brotli compression.
object
:<Variable>
Variable to be serialized- Returns:
<String>
Serialized version of the variable. Can be compressed.
string
:<String>
|<Buffer>
Serialized content- Returns:
<Variable>
Deserialized variable created from boxing
const { unbox, box } = require('lazycrate');
let unserialized = {
content: 'Hello World'
}
(async function(){
let serialized = await box(unserialized);
console.log(await unbox(serialized));
/*
{
content: 'Hello World'
}
*/
})();
-
Object
- Object
- Date
- Map
- Map
- WeakMap
- Set
- Set
- WeakSet
- RegExp
- Boolean
- Number
- String
- Error
- Error
- AggregateError
- EvalError
- RangeError
- ReferenceError
- SyntaxError
- TypeError
- URIError
- InternalError
- DataView
- Array
- ArrayBuffer
- SharedArrayBuffer
- BigInt64Array
- BigUint64Array
- Float32Array
- Float64Array
- Int8Array
- Int16Array
- Int32Array
- Uint8Array
- Uint16Array
- Uint32Array
- Uint8ClampedArray
- Buffer
- Generator
- Proxy
- FinalizationRegistry
- Promise
- WeakRef
-
undefined
-
Boolean
-
Number
-
BigInt
-
String
-
Symbol
-
Function
- Function
- AsyncFunction
- AsyncGeneratorFunction
- GeneratorFunction