deno.land / std@0.224.0 / json / json_parse_stream_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { TextDelimiterStream } from "../streams/text_delimiter_stream.ts";import { TextLineStream } from "../streams/text_line_stream.ts";import { JsonParseStream } from "./json_parse_stream.ts";import { assertInvalidParse, assertValidParse } from "./_test_common.ts";
Deno.test({ name: "JsonParseStream", async fn() { await assertValidParse( JsonParseStream, ['{"foo": "bar"}', '["foo"]', '"foo"', "0", "null", "true", "false"], [{ foo: "bar" }, ["foo"], "foo", 0, null, true, false], ); },});
Deno.test({ name: "JsonParseStream handles empty line", async fn() { await assertValidParse( JsonParseStream, [" \t\r\n", ""], [], ); },});
Deno.test({ name: "JsonParseStream handles special character", async fn() { await assertValidParse( JsonParseStream, ['"👪"', '"🦕"', '"\u3053\u3093\u306b\u3061\u306f"'], ["👪", "🦕", "\u3053\u3093\u306b\u3061\u306f"], ); },});
Deno.test({ name: "JsonParseStream handles expect error", async fn() { await assertInvalidParse( JsonParseStream, ['{"foo": "bar"}', '{"foo": '], {}, SyntaxError, `Unexpected end of JSON input (parsing: '{"foo": ')`, ); },});
Deno.test({ name: "parse() handles jsonl testdata", async fn() { // Read the test data file const url = "./testdata/test.jsonl"; const { body } = await fetch(new URL(url, import.meta.url).toString()); const readable = body! .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream()) .pipeThrough(new JsonParseStream());
const result = await Array.fromAsync(readable);
assertEquals(result, [ { "hello": "world" }, ["👋", "👋", "👋"], { "deno": "🦕" }, ]); },});
Deno.test({ name: "parse() handles ndjson testdata", async fn() { // Read the test data file const url = "./testdata/test.ndjson"; const { body } = await fetch(new URL(url, import.meta.url).toString()); const readable = body! .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream()) .pipeThrough(new JsonParseStream());
const result = await Array.fromAsync(readable);
assertEquals(result, [ { "hello": "world" }, ["👋", "👋", "👋"], { "deno": "🦕" }, ]); },});
Deno.test({ name: "parse() handles json-seq testdata", async fn() { // Read the test data file const recordSeparator = "\x1E";
const url = "./testdata/test.json-seq"; const { body } = await fetch(new URL(url, import.meta.url).toString()); const readable = body! .pipeThrough(new TextDecoderStream()) .pipeThrough(new TextDelimiterStream(recordSeparator)) .pipeThrough(new JsonParseStream());
const result = await Array.fromAsync(readable);
assertEquals(result, [ { "hello": "world" }, ["👋", "👋", "👋"], { "deno": "🦕" }, ]); },});
Version Info