Javascript data serializer
Transform any object, class, array, primitive to a serialized string and vice-versa
Installation
npm install @macfja/serializer
# or
yarn add @macfja/serializer
# or
pnpm add @macfja/serializer
Usage
import { serialize, deserialize } from "@macfja/serializer";
const serialized = serialize({ any: "data", you: "want" });
const myData = deserialize(serialized);
import {
serialize,
deserialize,
addGlobalAllowedClass,
} from "@macfja/serializer";
function MyClass(data) {
this.data = data;
}
MyClass.prototype.getData = function () {
return this.data;
};
addGlobalAllowedClass(MyClass);
let myInstance = new MyData("john");
const serialized = serialize(myInstance);
const myData = deserialize(serialized);
console.log(myData.getData());
Utilities usage
import { serialize, getCollectedClasses } from "@macfja/serializer";
const serialized = serialize(myComplexData);
const usedClasses = getCollectedClasses(true);
import { addClassHandler } from "@macfja/serializer";
function MyClass() {
this.name = "doe";
}
MyClass.prototype.setName = function (name) {
this.name = name;
};
MyClass.prototype.getName = function () {
return this.name;
};
addClassHandler(
"MyClass",
(data) => ({ name: data.getName() }),
(plain) => {
const value = new MyClass();
value.setName(plain.name);
return value;
}
);
Feature
- Serialize any data, primitive, array, object, class, and any combination
- Handle Javascript native classes (
Date
, BigInt
, String
, RegExp
, Number
, Map
, Set
, ArrayBuffer
, DataView
, Error
, EvalError
, RangeError
, AggregateError
, ReferenceError
, SyntaxError
, TypeError
, URIError
, Int8Array
, Uint8Array
, Uint8ClampedArray
, Int16Array
, Uint16Array
, Int32Array
, Uint32Array
, Float32Array
, Float64Array
, BigInt64Array
, BigUint64Array
) - Handle data recursion