circular-json vs fast-json-stringify vs fast-stable-stringify vs flatted vs json-stable-stringify vs json-stringify-safe
JSON Stringification Libraries
circular-jsonfast-json-stringifyfast-stable-stringifyflattedjson-stable-stringifyjson-stringify-safeSimilar Packages:

JSON Stringification Libraries

These libraries provide various functionalities for serializing JavaScript objects into JSON format, each with unique features tailored for specific use cases. They address common issues such as circular references, performance optimization, and maintaining the order of object keys, making them essential tools for developers working with JSON data in JavaScript applications.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
circular-json0611-07 years agoMIT
fast-json-stringify03,686393 kB362 months agoMIT
fast-stable-stringify030-09 years agoMIT
flatted01,13238.5 kB18 days agoISC
json-stable-stringify07936.4 kB7a year agoMIT
json-stringify-safe0554-711 years agoISC

Feature Comparison: circular-json vs fast-json-stringify vs fast-stable-stringify vs flatted vs json-stable-stringify vs json-stringify-safe

Circular Reference Handling

  • circular-json:

    Circular JSON can handle circular references seamlessly, allowing you to stringify objects that reference themselves without throwing errors.

  • fast-json-stringify:

    Fast JSON Stringify does not support circular references and will throw an error if encountered, making it unsuitable for such cases.

  • fast-stable-stringify:

    Fast Stable Stringify also does not support circular references, focusing instead on performance and key ordering.

  • flatted:

    Flatted supports circular references, enabling you to serialize complex objects without issues, making it a versatile choice.

  • json-stable-stringify:

    JSON Stable Stringify does not handle circular references and will throw an error if they are present.

  • json-stringify-safe:

    JSON Stringify Safe can handle circular references by replacing them with a placeholder, allowing for safe serialization.

Performance

  • circular-json:

    Circular JSON is not optimized for speed and may be slower than other libraries when handling large datasets due to its focus on circular reference support.

  • fast-json-stringify:

    Fast JSON Stringify is highly optimized for performance, making it one of the fastest options available for JSON serialization, especially with large objects.

  • fast-stable-stringify:

    Fast Stable Stringify is also performance-oriented, providing fast serialization while maintaining key order, though it may not be as fast as fast-json-stringify.

  • flatted:

    Flatted offers a good balance of performance and functionality, though it may not match the speed of fast-json-stringify for large datasets.

  • json-stable-stringify:

    JSON Stable Stringify is slower than some alternatives due to its focus on maintaining key order, which can add overhead during serialization.

  • json-stringify-safe:

    JSON Stringify Safe is not specifically optimized for performance and may be slower than other libraries when handling large datasets.

Key Order Consistency

  • circular-json:

    Circular JSON does not guarantee key order in the output, which may lead to inconsistent results when serializing objects with unordered keys.

  • fast-json-stringify:

    Fast JSON Stringify does not maintain key order, focusing instead on speed, which may be a drawback in scenarios where order matters.

  • fast-stable-stringify:

    Fast Stable Stringify guarantees consistent key ordering in the output, making it ideal for applications requiring deterministic JSON results.

  • flatted:

    Flatted does not guarantee key order, prioritizing circular reference handling over key consistency.

  • json-stable-stringify:

    JSON Stable Stringify ensures that the keys are always output in a consistent order, making it suitable for testing and caching scenarios.

  • json-stringify-safe:

    JSON Stringify Safe does not maintain key order and focuses on safe serialization instead.

Ease of Use

  • circular-json:

    Circular JSON is easy to use with a simple API, making it accessible for developers needing to handle circular references without complex configurations.

  • fast-json-stringify:

    Fast JSON Stringify has a straightforward API, but may require additional setup for schema definitions to achieve optimal performance.

  • fast-stable-stringify:

    Fast Stable Stringify is user-friendly, with a simple API that allows for quick implementation while ensuring key order consistency.

  • flatted:

    Flatted offers a modern API that is easy to use, making it a good choice for developers familiar with JSON serialization.

  • json-stable-stringify:

    JSON Stable Stringify is straightforward to use, but the focus on key order may require developers to consider their data structures more carefully.

  • json-stringify-safe:

    JSON Stringify Safe provides a simple API for safe serialization, making it easy to integrate into projects where circular references are a concern.

Use Cases

  • circular-json:

    Circular JSON is best suited for debugging and logging scenarios where complex objects with circular references need to be serialized without errors.

  • fast-json-stringify:

    Fast JSON Stringify is ideal for high-performance applications, such as APIs or microservices, where speed is critical and data structures are well-defined.

  • fast-stable-stringify:

    Fast Stable Stringify is perfect for applications that require consistent JSON outputs, such as testing frameworks or caching mechanisms.

  • flatted:

    Flatted is versatile and can be used in various applications, especially those that require handling complex objects with circular references.

  • json-stable-stringify:

    JSON Stable Stringify is useful in scenarios where predictable JSON output is necessary, such as versioning or comparing JSON data.

  • json-stringify-safe:

    JSON Stringify Safe is suitable for applications that need to serialize objects with potential circular references without crashing.

How to Choose: circular-json vs fast-json-stringify vs fast-stable-stringify vs flatted vs json-stable-stringify vs json-stringify-safe

  • circular-json:

    Choose Circular JSON if you need to serialize objects that may contain circular references. It allows you to safely stringify such objects without throwing errors, making it ideal for debugging or logging complex data structures.

  • fast-json-stringify:

    Opt for Fast JSON Stringify when performance is a priority. It is designed for speed and can significantly reduce serialization time, making it suitable for high-performance applications where JSON serialization is a bottleneck.

  • fast-stable-stringify:

    Select Fast Stable Stringify if you require consistent key ordering in your JSON output. This is particularly useful when the order of keys matters, such as when generating deterministic outputs for testing or caching.

  • flatted:

    Use Flatted for a balance of performance and the ability to handle circular references. It is a modern alternative that supports both serialization and deserialization of complex objects, making it versatile for various applications.

  • json-stable-stringify:

    Choose JSON Stable Stringify when you need to ensure that the output is always in a consistent order. This is useful for generating predictable JSON outputs, especially in scenarios where the output is compared or stored.

  • json-stringify-safe:

    Select JSON Stringify Safe if you want to avoid errors from circular references while still maintaining a straightforward JSON serialization process. It provides a simple API for safe stringification without additional features.

README for circular-json

CircularJSON

donate Downloads Build Status Coverage Status

Serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.


The future of this module is called flatted

Smaller, faster, and able to produce on average a reduced output too, flatted is the new, bloatless, ESM and CJS compatible, circular JSON parser.

It has now reached V1 and it implements the exact same JSON API.

Please note CircularJSON is in maintenance only and flatted is its successor.


A Working Solution To A Common Problem

A usage example:

var object = {};
object.arr = [
  object, object
];
object.arr.push(object.arr);
object.obj = object;

var serialized = CircularJSON.stringify(object);
// '{"arr":["~","~","~arr"],"obj":"~"}'
// NOTE: CircularJSON DOES NOT parse JS
// it handles receiver and reviver callbacks

var unserialized = CircularJSON.parse(serialized);
// { arr: [ [Circular], [Circular] ],
// obj: [Circular] }

unserialized.obj === unserialized;
unserialized.arr[0] === unserialized;
unserialized.arr.pop() === unserialized.arr;

A quick summary:

  • new in version 0.5, you can specify a JSON parser different from JSON itself. CircularJSON.parser = ABetterJSON; is all you need.
  • uses ~ as a special prefix symbol to denote which parent the reference belongs to (i.e. ~root~child1~child2)
  • reasonably fast in both serialization and deserialization
  • compact serialization for easier and slimmer transportation across environments
  • tested and covered over nasty structures too
  • compatible with all JavaScript engines

Node Installation & Usage

npm install --save circular-json
'use strict';

var
  CircularJSON = require('circular-json'),
  obj = { foo: 'bar' },
  str
;
  
obj.self = obj;
str = CircularJSON.stringify(obj);

There are no dependencies.

Browser Installation & Usage

  • Global: <build/circular-json.js>
  • AMD: <build/circular-json.amd.js>
  • CommonJS: <build/circular-json.node.js>

(generated via gitstrap)

<script src="build/circular-json.js"></script>
'use strict';

var CircularJSON = window.CircularJSON
  , obj = { foo: 'bar' }
  , str
  ;
  
obj.self = obj;
str = CircularJSON.stringify(obj);

NOTE: Platforms without native JSON (i.e. MSIE <= 8) requires json3.js or similar.

It is also a bad idea to CircularJSON.parse(JSON.stringify(object)) because of those manipulation used in CircularJSON.stringify() able to make parsing safe and secure.

As summary: CircularJSON.parse(CircularJSON.stringify(object)) is the way to go, same is for JSON.parse(JSON.stringify(object)).

API

It's the same as native JSON, except the fourth parameter placeholder, which circular references to be replaced with "[Circular]" (i.e. for logging).

  • CircularJSON.stringify(object, replacer, spacer, placeholder)
  • CircularJSON.parse(string, reviver)

Bear in mind JSON.parse(CircularJSON.stringify(object)) will work but not produce the expected output.

Similar Libraries

Why Not the @izs One

The module json-stringify-safe seems to be for console.log() but it's completely pointless for JSON.parse(), being latter one unable to retrieve back the initial structure. Here an example:

// a logged object with circular references
{
  "circularRef": "[Circular]",
  "list": [
    "[Circular]",
    "[Circular]"
  ]
}
// what do we do with above output ?

Just type this in your node console: var o = {}; o.a = o; console.log(o);. The output will be { a: [Circular] } ... good, but that ain't really solving the problem.

However, if that's all you need, the function used to create that kind of output is probably faster than CircularJSON and surely fits in less lines of code.

Why Not {{put random name}} Solution

So here the thing: circular references can be wrong but, if there is a need for them, any attempt to ignore them or remove them can be considered just a failure.

Not because the method is bad or it's not working, simply because the circular info, the one we needed and used in the first place, is lost!

In this case, CircularJSON does even more than just solve circular and recursions: it maps all same objects so that less memory is used as well on deserialization as less bandwidth too! It's able to redefine those references back later on so the way we store is the way we retrieve and in a reasonably performant way, also trusting the snappy and native JSON methods to iterate.