Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@macfja/serializer

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@macfja/serializer

Transform any object, class, array, primitive to a serialized string and vice-versa

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
decreased by-18.07%
Maintainers
1
Weekly downloads
 
Created
Source

Javascript data serializer

Transform any object, class, array, primitive to a serialized string and vice-versa

NPM bundle size Download per week License NPM version Snyk Vulnerabilities for npm package

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" });
// serialized is a string

const myData = deserialize(serialized);
// myData is now `{any: 'data', you: 'want'}`
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);
// serialized is a string

const myData = deserialize(serialized);
console.log(myData.getData()); // "john"

Utilities usage

import { serialize, getCollectedClasses } from "@macfja/serializer";

const serialized = serialize(myComplexData);
// serialized is a string
const usedClasses = getCollectedClasses(true);
// usedClasses contains the list of classes found during the serialization
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;
  }
);

// Now  `serialize` and `deserialize` will use your custom serializer / deserializer
// for any instance of `MyClass`

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

Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the Contributing file

License

The MIT License (MIT). Please see License File for more information.

Keywords

FAQs

Package last updated on 03 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc