温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/bun/JSONL
点击访问原文链接

Bun.JSONL object | API Reference | Bun

Bun.JSONL object | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/Bun/JSONLFparseFparseChunk

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/Bun/JSONLFparseFparseChunk

namespace

JSONLnamespace JSONL

JSONL (JSON Lines) related APIs.

Each line in the input is expected to be a valid JSON value separated by newlines.

interface ParseChunkResult

The result of Bun.JSONL.parseChunk.

done: boolean

true if all input was consumed successfully. false if the input ends with an incomplete value or a parse error occurred.

error: null | SyntaxError

A SyntaxError if a parse error occurred, otherwise null. Values parsed before the error are still available in values.

read: number

How far into the input was consumed. When the input is a string, this is a character offset. When the input is a TypedArray, this is a byte offset. Use input.slice(read) or input.subarray(read) to get the unconsumed remainder.

values: unknown[]

The successfully parsed JSON values.

function parse(input: string | ArrayBufferLike | TypedArrayArrayBufferLike> | DataViewArrayBuffer>): unknown[];

Parse a JSONL (JSON Lines) string into an array of JavaScript values.

If a parse error occurs and no values were successfully parsed, throws a SyntaxError. If values were parsed before the error, returns the successfully parsed values without throwing.

Incomplete trailing values (e.g. from a partial chunk) are silently ignored and not included in the result.

When a TypedArray is passed, the bytes are parsed directly without copying if the content is ASCII.

@param input

The JSONL string or typed array to parse

@returns

An array of parsed values

const items = Bun.JSONL.parse('{"a":1}\n{"b":2}\n');
// [{ a: 1 }, { b: 2 }]

// From a Uint8Array (zero-copy for ASCII):
const buf = new TextEncoder().encode('{"a":1}\n{"b":2}\n');
const items = Bun.JSONL.parse(buf);
// [{ a: 1 }, { b: 2 }]

// Partial results on error after valid values:
const partial = Bun.JSONL.parse('{"a":1}\n{bad}\n');
// [{ a: 1 }]

// Throws when no valid values precede the error:
Bun.JSONL.parse('{bad}\n'); // throws SyntaxError
function parseChunk(input: string | ArrayBufferLike | TypedArrayArrayBufferLike> | DataViewArrayBuffer>,start?: number,end?: number): ParseChunkResult;

Parse a JSONL chunk, designed for streaming use.

Never throws on parse errors. Instead, returns whatever values were successfully parsed along with an error property containing the SyntaxError (or null on success). Use read to determine how much input was consumed and done to check if all input was parsed.

When a TypedArray is passed, the bytes are parsed directly without copying if the content is ASCII. Optional start and end parameters select a window of the input without copying. For typed arrays these are byte offsets and read will be a byte offset into the original typed array. For strings these are character offsets and read will be a character offset into the original string.

@param input

The JSONL string or typed array to parse

@param start

Offset to start parsing from (bytes for typed arrays, characters for strings, default: 0)

@param end

Offset to stop parsing at (bytes for typed arrays, characters for strings, default: input length)

@returns

An object with values, read, done, and error properties

let buffer = new Uint8Array(0);
for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]);
const { values, read, error } = Bun.JSONL.parseChunk(buffer);
if (error) throw error;
for (const value of values) handle(value);
buffer = buffer.subarray(read);
}

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Bun.JSONL object | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    JSONL (JSON Lines) related APIs. Each line in the input is expected to be a valid JSON value separated by newlines.