Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,39 @@ added: v0.1.100

The `console.info()` function is an alias for [`console.log()`][].

### console.json(value[, replacer[, space]])
<!-- YAML
added: REPLACEME
-->
* `value` {any}
* `replacer` {Function|Array|null}
* `space` {number|string}

Prints `JSON.stringify(value, replacer, space)` to `stdout`. Arguments are
passed directly to [`JSON.stringify()`][]. This is useful for inspecting
objects without the annotations added by [`util.inspect()`][] (such as
`[Object: null prototype]`).

```js
console.json({ foo: 'bar' });
// Prints: {"foo":"bar"}

console.json({ foo: 'bar' }, null, 2);
// Prints:
// {
// "foo": "bar"
// }

// Works cleanly with null-prototype objects
const obj = Object.assign(Object.create(null), { foo: 'bar' });
console.json(obj);
// Prints: {"foo":"bar"}
```

Throws `TypeError` for non-serializable values such as circular references.
Use [`console.log()`][] or [`console.dir()`][] for values that may not be
JSON-serializable.

### console.log([data][, ...args])
<!-- YAML
added: v0.1.100
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// The Console constructor is not actually used to construct the global
// console. It's exported for backwards compatibility.

const { Object, ObjectPrototype, Reflect } = primordials;
const { JSON, Object, ObjectPrototype, Reflect } = primordials;

const { trace } = internalBinding('trace_events');
const {
Expand Down Expand Up @@ -297,6 +297,10 @@ const consoleMethods = {
}));
},

json(value, replacer, space) {
this[kWriteToConsole](kUseStdout, JSON.stringify(value, replacer, space));
},

time(label = 'default') {
// Coerces everything other than Symbol to a string
label = `${label}`;
Expand Down
58 changes: 58 additions & 0 deletions test/parallel/test-console-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';
require('../common');

const assert = require('assert');
const { Console } = require('console');

let output = '';
const mockStdout = {
write(chunk) { output += chunk; },
isTTY: false,
};

const c = new Console(mockStdout);

function captured(fn) {
output = '';
fn();
return output;
}

// Basic object
assert.strictEqual(
captured(() => c.json({ foo: 'bar' })),
'{"foo":"bar"}\n'
);

// Null-prototype object — the motivating case
const nullProto = Object.assign(Object.create(null), { foo: 'bar' });
assert.strictEqual(
captured(() => c.json(nullProto)),
'{"foo":"bar"}\n'
);

// With space for pretty-printing
assert.strictEqual(
captured(() => c.json({ foo: 'bar' }, null, 2)),
'{\n "foo": "bar"\n}\n'
);

// With replacer array
assert.strictEqual(
captured(() => c.json({ foo: 'bar', baz: 1 }, ['foo'])),
'{"foo":"bar"}\n'
);

// Array
assert.strictEqual(captured(() => c.json([1, 2, 3])), '[1,2,3]\n');

// Primitives
assert.strictEqual(captured(() => c.json(42)), '42\n');
assert.strictEqual(captured(() => c.json('hello')), '"hello"\n');
assert.strictEqual(captured(() => c.json(null)), 'null\n');
assert.strictEqual(captured(() => c.json(true)), 'true\n');

// Circular reference throws TypeError
const circular = {};
circular.self = circular;
assert.throws(() => c.json(circular), TypeError);
1 change: 1 addition & 0 deletions test/parallel/test-console-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const methods = [
'log',
'warn',
'dir',
'json',
'time',
'timeEnd',
'timeLog',
Expand Down