All Projects → pigulla → json-strictify

pigulla / json-strictify

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

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to json-strictify

EndianBinaryIO
A C# library that can read and write primitives, enums, arrays, and strings to streams and byte arrays with specified endianness, string encoding, and boolean sizes.
Stars: ✭ 20 (+42.86%)
Mutual labels:  serialization
drf-action-serializer
A serializer for the Django Rest Framework that supports per-action serialization of fields.
Stars: ✭ 48 (+242.86%)
Mutual labels:  serialization
succinct-binary
Succinct binary serialization
Stars: ✭ 16 (+14.29%)
Mutual labels:  serialization
tjson.js
JavaScript-compatible implementation of Tagged JSON (TJSON), written in TypeScript.
Stars: ✭ 53 (+278.57%)
Mutual labels:  serialization
doku
fn(Code) -> Docs
Stars: ✭ 65 (+364.29%)
Mutual labels:  serialization
Bois
Salar.Bois is a compact, fast and powerful binary serializer for .NET Framework. With Bois you can serialize your existing objects with almost no change.
Stars: ✭ 53 (+278.57%)
Mutual labels:  serialization
GroBuf
Fast binary serializer
Stars: ✭ 56 (+300%)
Mutual labels:  serialization
SwiftCBOR
A CBOR implementation for Swift
Stars: ✭ 95 (+578.57%)
Mutual labels:  serialization
kafka-protobuf-serde
Serializer/Deserializer for Kafka to serialize/deserialize Protocol Buffers messages
Stars: ✭ 52 (+271.43%)
Mutual labels:  serialization
MetaCPP
C++ Reflection & Serialization using Clang's LibTooling
Stars: ✭ 44 (+214.29%)
Mutual labels:  serialization
CodableWrapper
@codec("encoder", "decoder") var cool: Bool = true
Stars: ✭ 143 (+921.43%)
Mutual labels:  serialization
dataclasses-jsonschema
JSON schema generation from dataclasses
Stars: ✭ 145 (+935.71%)
Mutual labels:  serialization
desert
Deserialize to objects while staying DRY
Stars: ✭ 136 (+871.43%)
Mutual labels:  serialization
kafka-serialization
Lego bricks to build Apache Kafka serializers and deserializers
Stars: ✭ 122 (+771.43%)
Mutual labels:  serialization
typical
Data interchange with algebraic data types.
Stars: ✭ 114 (+714.29%)
Mutual labels:  serialization
msgpack-perl
MessagePack serializer implementation for Perl / msgpack.org[Perl]
Stars: ✭ 48 (+242.86%)
Mutual labels:  serialization
baltar
Example graphics editor using MobX
Stars: ✭ 42 (+200%)
Mutual labels:  serialization
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (+85.71%)
Mutual labels:  serialization
ikeapack
Compact data serializer/packer written in Go, intended to produce a cross-language usable format.
Stars: ✭ 18 (+28.57%)
Mutual labels:  serialization
vue-query-builder
A Vue-Query-Builder
Stars: ✭ 71 (+407.14%)
Mutual labels:  serialization

Typescript npm GitHub Workflow Status GitHub Issues libraries.io Codecov npm bundle size Snyk Vulnerabilities for npm package

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).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].