When a json object is printed, for ex in a script executed by node using console.log, it does not fully pretty print the json. It kind of takes middle ground and prints as few lines as possible without losing much in terms of readability.
I was wondering if there is any name for this formatting or an algorithm. I want to implement this formatting in other languages.
console.log
in Node.js is basically this:
(value, ...args) => process.stdout.write(util.format(value, ...args) + '\n')
From the documentation on util.format
:
If the first argument is not a string then
util.format()
returns a string that is the concatenation of all arguments separated by spaces. Each argument is converted to a string usingutil.inspect()
.- https://nodejs.org/api/util.html#util_util_format_format_args
Which means that console.log(object)
is equivalent to:
> process.stdout.write(util.inspect(object) + '\n');
From the documentation on util.inspect
:
The
util.inspect()
method returns a string representation of object that is intended for debugging. The output ofutil.inspect
may change at any time and should not be depended upon programmatically. Additional options may be passed that alter certain aspects of the formatted string.util.inspect()
will use the constructor's name and/or @@toStringTag to make an identifiable tag for an inspected value.- https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
As far as I can tell there is no "name" for the default formatting settings of util.inspect
, other than "compact" as mentioned (a boolean option that is set to true by default) by the util.inspect
documentation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.