基于标准构建的简单、快速的 JSON 解析器。
逐步解析 JSON 数据,例如从网络请求或语言模型中流入的数据。提供一系列日益完整的值。 jsonriver 体积小,速度快,不依赖任何其他库,仅使用 JavaScript 的标准功能,因此可在任何 JS 环境中运行。 用法:
// Richer example at examples/fetch.js and live demos at
// https://rictic.github.io/jsonriver/
import {parse} from 'jsonriver';
const response = await fetch(`https://jsonplaceholder.typicode.com/posts`);
const postsStream = parse(response.body.pipeThrough(new TextDecoderStream()));
for await (const posts of postsStream) {
console.log(posts);
}我们给你提供了一系列日益完整的值,这意味着什么? 请考虑以下 JSON:
{"name": "Alex", "keys": [1, 20, 300]}如果你一次一个字节地将其传递给 jsonriver,则会得到以下值序列:
{}
{"name": ""}
{"name": "A"}
{"name": "Al"}
{"name": "Ale"}
{"name": "Alex"}
{"name": "Alex", "keys": []}
{"name": "Alex", "keys": [1]}
{"name": "Alex", "keys": [1, 20]}
{"name": "Alex", "keys": [1, 20, 300]}parse 返回的最终值与在整个字符串上调用 JSON.parse 相同。这项测试与 JSONTestSuite 进行比较,以匹配 JSON.parse 在正确、不正确和模糊情况的测试中所表现出的行为。parse 函数还匹配 JSON.parse 的行为,对于无效输入。如果输入流无法解析为有效 JSON 文档的开始,则解析停止,并抛出错误。更准确地说,AsyncIterable 上的 next 方法返回的 promise 会以 Error 形式拒绝。同样,如果输入流过早关闭。
暂无开放 Issues,或尚未同步最近议题。