What is json-stringify-pretty-compact?
The json-stringify-pretty-compact npm package is designed to provide a more compact and human-readable JSON stringification. It aims to strike a balance between readability and compactness by using a more intelligent formatting approach compared to the default JSON.stringify method.
What are json-stringify-pretty-compact's main functionalities?
Compact and Pretty JSON Stringification
This feature allows you to convert a JavaScript object into a JSON string that is both compact and easy to read. The output is more human-friendly compared to the default JSON.stringify method.
const stringify = require('json-stringify-pretty-compact');
const obj = { foo: 'bar', baz: [1, 2, 3], nested: { a: 1, b: 2 } };
console.log(stringify(obj));
Custom Indentation
This feature allows you to specify the number of spaces to use for indentation, giving you control over the readability of the output.
const stringify = require('json-stringify-pretty-compact');
const obj = { foo: 'bar', baz: [1, 2, 3], nested: { a: 1, b: 2 } };
console.log(stringify(obj, { indent: 4 }));
Maximum Line Length
This feature allows you to set a maximum line length for the output, ensuring that the JSON string does not exceed a specified width, which can be useful for maintaining readability in narrow spaces.
const stringify = require('json-stringify-pretty-compact');
const obj = { foo: 'bar', baz: [1, 2, 3], nested: { a: 1, b: 2 } };
console.log(stringify(obj, { maxLength: 80 }));
Other packages similar to json-stringify-pretty-compact
prettyjson
The prettyjson package is another alternative that focuses on making JSON data more readable. It offers more customization options for colors and indentation but may not be as compact as json-stringify-pretty-compact.
json-pretty-print
The json-pretty-print package provides basic pretty-printing capabilities for JSON data. It is simpler and may not offer the same level of compactness and customization as json-stringify-pretty-compact.
Overview
The output of JSON.stringify
comes in two flavors: compact and pretty. The
former is usually too compact to be read by humans, while the latter sometimes
is too spacious. This module trades performance (and the “replacer” argument)
for a compromise between the two. The result is a pretty compact string, where
“pretty” means both “kind of” and “nice”.
{
"bool": true,
"short array": [1, 2, 3],
"long array": [
{"x": 1, "y": 2},
{"x": 2, "y": 1},
{"x": 1, "y": 1},
{"x": 2, "y": 2}
]
}
While the “pretty” mode of JSON.stringify
puts every item of arrays and
objects on its own line, this module puts the whole array or object on a single
line, unless the line becomes too long (the default maximum is 80 characters).
Making arrays and objects multi-line is the only attempt made to enforce the
maximum line length; if that doesn’t help then so be it.
Installation
npm install json-stringify-pretty-compact
var stringify = require("json-stringify-pretty-compact")
Usage
stringify(obj, options)
It’s like JSON.stringify(obj, null, options.indent)
, except that objects and
arrays are on one line if they fit (according to options.maxLength
).
options
:
- indent: Defaults to 2. Works exactly like the third parameter of
JSON.stringify
. - maxLength: Defaults to 80. Lines will be tried to be kept at maximum this many
characters long.
stringify(obj, {maxLength: 0, indent: indent})
gives the exact same result as
JSON.stringify(obj, null, indent)
.
stringify(obj, {maxLength: Infinity})
gives the exact same result as
JSON.stringify(obj)
, except that there are spaces after colons and commas.
License
The X11 (“MIT”) License.