ProductPromotion
Logo

Node.JS

made by https://0x3d.site

GitHub - pigulla/json-strictify: Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.
Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references. - pigulla/json-strictify
Visit Site

GitHub - pigulla/json-strictify: Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.

GitHub - pigulla/json-strictify: Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.

Typescript npm GitHub Workflow Status GitHub Issues libraries.io Codecov npm bundle size

json-strictify

Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.

Why

The native JSON.stringify function drops or silently modifies all values that are not supported by the JSON specification:

JSON.stringify({ a: 42, b: undefined })
// returns '{"a":42}'

JSON.parse(JSON.stringify(NaN))
// returns null

JSON.stringify([1, NaN, 3])
// returns '[1,null,3]'

In many cases this is not the behaviour you want: relying on the serialization method to clean up your data is error prone and can lead to subtle bugs that are annoying to find. json-strictify helps you to easily avoid these issues with literally a single line of code.

Unlike json-stringify-safe it does not attempt to "fix" its input but always bails out when it encounters something that would prevent it from being serialized properly.


Installation

Simply install via npm:

npm install json-strictify

Usage

json-strictify exposes three methods: stringify, parse and enable, so it can be used as a drop-in replacement for the native JSON object:

import JSON from 'json-strictify'

JSON.stringify(someObject)

The parse method is simply a reference to the native JSON.parse function.


Examples

The stringify function throws an error if the input to be serialized contains invalid values:

import JSONs from 'json-strictify'

JSONs.stringify({ x: 42, y: NaN })
// InvalidValueError: Invalid value at /y (NaN is not JSON-serializable)

Also, if the data you want to stringify contains circular references a CircularReferenceError is thrown:

const data = []
data.push(data)
JSONs.stringify(data)
// CircularReferenceError: Circular reference found at "/0"

The location of the value that caused the error is given as a JSON Pointer reference.


ESLint integration

If you want to ensure that all serialization is done through json-strictify you can disable the global JSON variable like so:

"globals": {
    "JSON": "off"
}

See the ESLint documentation on configuring globals for details.


Disabling json-strictify

In production you may not want to have the additional overhead introduced by json-strictify. This can easily be avoided by calling the enabled method:

import JSONs from 'json-strictify'
const JSON = JSONs.enabled(config.debug)

// or for older versions of Javascript:
const JSON = require('json-strictify').enabled(config.debug)

If called with a falsy parameter, enabled will return an object that delegates directly to the native JSON object so there will be no performance penalty whatsoever.

Note: json-strictify is disabled by default if NODE_ENV is set to production (you may still enable it manually, of course).

Articles
to learn more about the nodejs concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about NodeJS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory