npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

als-object-serializer

v1.0.0

Published

An advanced JavaScript library for serializing and deserializing complex objects including those with circular references, functions, and special object types like Date, Set, and Map. Ideal for applications needing robust handling of object serialization

Downloads

37

Readme

als-object-serializer

als-object-serializer is an advanced JavaScript library designed for serializing and deserializing objects, including those with complex structures such as circular references, functions, Date, RegExp, Set, and Map objects. It uniquely preserves function definitions and manages circular dependencies within objects, making it ideal for scenarios where traditional JSON serialization is insufficient.

Where it can be used? With als-object-serializer you can save js objects in database, localStorage (it's can be used in browser too), as script binded from backend to frontend and more.

Installation and import

Install als-object-serializer using npm:

npm install als-object-serializer

NodeJS usage

const Obj = require('als-object-serializer');

Browser usage

<script src="/node_modules/als-object-serializer/index.js"></script>

Usage

Here’s how to use als-object-serializer to serialize and deserialize objects:

const myObject = {
  name: "Alice",
  details: { age: 25, hobbies: ["reading", "gaming"] }
};

const serialized = Obj.stringify(myObject);
//serialized = {"name":"Alice","details":{"age":25,"hobbies":["reading","gaming"]}}

const parsed = Obj.parse(serializedData); 
// parsed should return an object identical to myObject

Complex Example

const organizers = [
   {name:'Alice',phone:'565-5654545'}
]
const complexObject = {
   date: new Date(),
   events: new Set(['conference', 'meeting', 'webinar']),
   organizers,
   details: {
      organizer: organizers[0],
      contact: function () {
         return `Contact Organizer: ${this.organizer.name}: ${this.organizer.phone}`;
      },
   }
};

const serialized = Obj.stringify(complexObject);
const parsed = Obj.parse(serialized)

console.log(serialized);
// {"date":new Date("2024-07-03T06:52:48.901Z"),"events":new Set(["conference","meeting","webinar"]),"organizers":[{"name":"Alice","phone":"565-5654545"}],"details":{"organizer":function recursiveReference(self) {return self['organizers'][0]},"contact":function () {\n         return `Contact Organizer: ${this.organizer.name}: ${this.organizer.phone}`;\n      }}}

console.log(parsed); // should return similar to complexObject object 
console.log(parsed.organizers[0] === parsed.details.organizer) // true
console.log(parsed.details.contact()) // Contact Organizer: Alice: 565-5654545

In example above, cyclyc reference replaced with recursiveReference function which had run in Obj.parse method to get the reference. The name of recursiveReference available in Obj.recursiveName and can be changed.

Handling Circular References

als-object-serializer uniquely handles circular references by converting them into a string representation that includes a custom function, recursiveReference. This function is designed to reconstruct the circular reference when parsed back into an object using the objParse method. This approach ensures that complex object graphs with circular references can be safely converted to a string and back without losing their structure.

Note: The string output should be used with understanding that it includes JavaScript code that reconstructs circular relationships. When deserializing, ensure that the input string is from a trusted source as the parsing mechanism executes the code within the string.

Limitations

  • Error objects are created without a stack trace; only the message is preserved.
  • Buffer objects are not supported.

Security Considerations

Serialization of Functions

Serialization of functions with objStringify converts functions to their string representation using toString(). This process captures the function's code but not its execution context, closures, or external references. Thus, while the function's syntax is preserved, it might not be restored to its original functional state upon deserialization. Use this feature with the understanding that some functions may require additional context to operate as intended.

Use of new Function()

Due to the use of new Function() in the objParse method for executing string representation, it is crucial to ensure that the serialized string passed to objParse is from a trusted and verified source. The execution of untrusted code can lead to serious security vulnerabilities similar to those associated with the use of eval(). Always validate or sanitize inputs rigorously when dealing with serialization and deserialization to prevent security issues.

General Security Note

When using objParse, ensure that the input string is from a trusted source. The function utilizes new Function() to evaluate the string representation of an object, which can execute arbitrary code. As such, passing unverified or untrusted data to objParse can lead to significant security risks similar to those associated with the eval() function. Always validate or sanitize inputs to prevent security vulnerabilities.