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

als-object-serializer

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

als-object-serializer

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

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
0
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 03 Jul 2024

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