🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

node-serialization

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-serialization

A serialization library for node. Serialize/Deserialize a huge object to/from a file.

1.0.1
latest
Source
npm
Version published
Weekly downloads
31
63.16%
Maintainers
1
Weekly downloads
 
Created
Source

node-serialization

NPM Version LICENSE Build Status code style: prettier

A serialization library for node. Serialize/Deserialize a huge object to/from a file.

Using v8 Serialization API as default. You can provide custom serialization functions, like JSON(stringify/parse), json-stream-stringify, etc.

Usage

const { readFile, writeFile } = require("node-serialization");

Default serialize/deserialize method

Write a object to a file and read the file restore a object.

var path = "cache.data";
var data = {
  foo: "bar",
};

writeFile(path, data).then(() => console.log("saved"));

readFile(path).then((data) => console.log(data));

Custom serialize/deserialize method

Use a custom serialization method.

const { serialization } = require("node-serialization");

const serialize = function (object) {
  return JSON.stringify(object);
};
const deserialize = function (string) {
  return JSON.parse(string);
};

const { readFile: readJsonFile, writeFile: writeJsonFile } = serialization(
  serialize,
  deserialize
);

var path = "cache.json";
var data = {
  foo: "bar",
};

writeJsonFile(path, data).then(() => console.log("saved"));

readJsonFile(path).then((data) => console.log(data));

Convert Format

Deserialize a file then serialize into another file with different serialization.

const {
  convert,
  serializeJson,
  deserialize: deserializeV8,
} = require("node-serialization");

convert(
  "cache.data",
  deserializeV8,
  "cache.data.json",
  serializeJson
).catch((err) => console.error(err));

Methods

Async

  • readFile
  • writeFile
  • readJson
  • writeJson
  • convert

Sync

  • readFileSync
  • writeFileSync
  • readJsonSync
  • writeJsonSync
  • convertSync
  • serialize
  • deserialize
  • serializeJson
  • deserializeJson

If file parameter is a file path (type is string, not a buffer or a file descriptor), the writeXxx functions will write content to a temporary file while writing, and rename to the path of file parameter when success. So even if an exception(such as OOM) occurs, the file will not be a file with content loss.

License

Copyright (c) 2020 dailyrandomphoto. Licensed under the MIT license.

Keywords

serialization

FAQs

Package last updated on 04 Aug 2020

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