-
-
Notifications
You must be signed in to change notification settings - Fork 54
jsonc Stringer
Signature: tokens → text
This is a JSONC stringer that converts a token stream (including whitespace, comment, and comma tokens from the JSONC Parser) back into JSONC text.
Base tokens are handled identically to Stringer. Whitespace and comment tokens are output verbatim — comments from their commentChunks by default, or from commentValue with useCommentValues; with useCommas, comma tokens are rendered too.
import chain from 'stream-chain';
import {parser as jsoncParser} from 'stream-json/jsonc/parser.js';
import {stringer as jsoncStringer} from 'stream-json/jsonc/stringer.js';
import fs from 'node:fs';
const pipeline = chain([fs.createReadStream('settings.jsonc'), jsoncParser(), jsoncStringer()]);
let result = '';
pipeline.on('data', chunk => (result += chunk));
pipeline.on('end', () => console.log(result));The factory function. Returns a flushable function for use in chain():
import chain from 'stream-chain';
import {parser as jsoncParser} from 'stream-json/jsonc/parser.js';
import {stringer as jsoncStringer} from 'stream-json/jsonc/stringer.js';
import fs from 'node:fs';
const pipeline = chain([fs.createReadStream('settings.jsonc'), jsoncParser(), jsoncStringer()]);
let result = '';
pipeline.on('data', chunk => (result += chunk));
pipeline.on('end', () => console.log(result));All standard Stringer options are supported:
-
useValues,useKeyValues,useStringValues,useNumberValues makeArray
Plus JSONC-specific options:
-
useCommentValues— render comments from packedcommentValuetokens instead ofcommentChunks (requires the parser'spackComments). Default:false. -
useCommas— render streamedcommatokens (from the parser'sstreamCommas) as,instead of auto-generating separators. Default:false. A separator is still auto-inserted before a value when nocommatoken preceded it, so the output stays valid even if a comma was dropped by an upstream transform.
Alias of the factory function.
Returns a Duplex stream (object-mode writable, text-mode readable):
import jsoncParser from 'stream-json/jsonc/parser.js';
import jsoncStringer from 'stream-json/jsonc/stringer.js';
import fs from 'node:fs';
const pipeline = fs.createReadStream('settings.jsonc').pipe(jsoncParser.asStream()).pipe(jsoncStringer.asStream());
let result = '';
pipeline.on('data', chunk => (result += chunk));
pipeline.on('end', () => console.log(result));When used with the JSONC parser, the stringer preserves whitespace and comments. By default, commas are auto-inserted by the stringer, so their placement may differ from the original and trailing commas are normalized away. For byte-faithful round-trips — e.g. streaming edits of large JSONC files that don't fit in memory — parse with streamCommas: true and stringify with useCommas: true; commas (including trailing commas) are then reproduced exactly at their original positions. Colons are always auto-inserted.
import chain from 'stream-chain';
import {parser as jsoncParser} from 'stream-json/jsonc/parser.js';
import {stringer as jsoncStringer} from 'stream-json/jsonc/stringer.js';
import {Readable} from 'node:stream';
const input = '{\n // name\n "a": 1\n}';
const pipeline = chain([Readable.from([input]), jsoncParser(), jsoncStringer()]);
let result = '';
pipeline.on('data', chunk => (result += chunk));
pipeline.on('end', () => console.log(result));
// Comments and whitespace are preserved in the outputjsoncStringer ships in two substrate-specific entries with the same factory shape:
-
Node —
stream-json/jsonc/stringer.js. HasasStream(Node Duplex) andasWebStream(Web{readable, writable}pair). -
Web —
stream-json/web/jsonc/stringer.js. HasasWebStreamonly. Pulls in no Node-stream imports.
Both factories return the same flushable, so chain on either substrate auto-wraps it.
// Web
import {chain} from 'stream-chain/web';
import {parser as jsoncParser} from 'stream-json/web/jsonc/parser.js';
import {stringer as jsoncStringer} from 'stream-json/web/jsonc/stringer.js';
const pipeline = chain([source, jsoncParser(), jsoncStringer()]);
let result = '';
for await (const chunk of pipeline.readable) result += chunk;
console.log(result);Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain