jsonlines

a Node.js library to parse, stringify jsonlines files as streams
Install
npm install @jsonlines/core
yarn add @jsonlines/core
Usage
stringify
const { stringify } = require("@jsonlines/core");
const { stringify } = require("@jsonlines/core/stringify");
import { stringify } from "@jsonlines/core";
import { stringify } from "@jsonlines/core/stringify";
require("stream")
.Readable.from([
{ v: "object1" },
{ name: "Lady Gaga", records: ["Chromatica"] },
[1, 2, 3, 4],
true,
false,
2020,
-1,
require("@jsonlines/core").nullValue,
require("@jsonlines/core/null-value").nullValue,
{ value: null },
[null, null],
])
.pipe(
stringify(),
)
.pipe(process.stdout);
the output will be:
{"v":"object1"}
{"name":"Lady Gaga","records":["Chromatica"]}
[1,2,3,4]
true
false
2020
-1
null
null
{"value":null}
[null,null]
stringify
API
function stringify(options?: JsonLinesStringifyOptions): JsonLinesStringifyStream;
stringify
function accepts an optional object as options and returns an instance of JsonLinesStringifyStream
.
Note that JsonLinesStringifyStream
extends Duplex
.
options:
export interface JsonLinesStringifyOptions<V> {
encoding?: BufferEncoding;
stringify?: (v: V) => string | Promise<string>;
gzip?: JsonLinesGzipOption;
lineSep?: "lf" | "\n" | "crlf" | "\r\n";
}
parse
const { parse } = require("@jsonlines/core");
const { parse } = require("@jsonlines/core/parse");
import { parse } from "@jsonlines/core";
import { parse } from "@jsonlines/core/parse";
(async () => {
const source = require("stream").Readable.from(`{"v":"object1"}
{"name":"Lady Gaga","records":["Chromatica"]}
[1,2,3,4]
true
false
2020
-1
null
null
{"value":null}
[null,null]
`);
const parseStream = parse();
source.pipe(parseStream);
for await (const value of parseStream) {
if (value === require("@jsonlines/core/null-value").nullValue)
console.log(`--- The following value is nullValue ---`);
console.log(value);
}
})();
the output will be:
{ v: 'object1' }
{ name: 'Lady Gaga', records: [ 'Chromatica' ] }
[ 1, 2, 3, 4 ]
true
false
2020
-1
--- The following value is nullValue ---
[Object: null prototype] [Null] {}
--- The following value is nullValue ---
[Object: null prototype] [Null] {}
{ value: null }
[ null, null ]
parse
API
function parse(options?: JsonLinesParseOptions<V>): JsonLinesParseStream;
parse
function accepts an optional object as options
and returns an instance of JsonLinesParseStream
.
Note that JsonLinesParseStream
extends Duplex
.
options:
export interface JsonLinesParseOptions<V> {
encoding?: BufferEncoding;
parse?: (line: string) => V | Promise<V>;
gzip?: JsonLinesGzipOption;
lineSep?: "lf" | "\n" | "crlf" | "\r\n" | "auto";
}