简体   繁体   English

console.log是否格式化Json对象有名称吗?

[英]Is there a name for how console.log formats the Json object?

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. 当打印json对象时,例如,在节点使用console.log执行的脚本中,它并不能完全打印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: Node.js中的console.log基本上是这样的:

(value, ...args) => process.stdout.write(util.format(value, ...args) + '\n')

From the documentation on util.format : 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. 如果第一个参数不是字符串,则util.format()返回一个字符串,该字符串是由空格分隔的所有参数的串联。 Each argument is converted to a string using util.inspect() . 使用util.inspect()将每个参数转换为字符串。

- https://nodejs.org/api/util.html#util_util_format_format_args -https://nodejs.org/api/util.html#util_util_format_format_args

Which means that console.log(object) is equivalent to: 这意味着console.log(object)等效于:

> process.stdout.write(util.inspect(object) + '\n');

From the documentation on util.inspect : util.inspect的文档中:

The util.inspect() method returns a string representation of object that is intended for debugging. util.inspect()方法返回用于调试的对象的字符串表示形式。 The output of util.inspect may change at any time and should not be depended upon programmatically. util.inspect的输出可能随时更改,因此不应以编程方式依赖它。 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. util.inspect()将使用构造函数的名称和/或@@ toStringTag为可检查的值创建可识别的标记。

- https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors -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. 据我所知, util.inspect的默认格式设置没有“名称”,除了util.inspect文档中提到的“ compact”(默认为true的布尔选项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM