Skip to main content
Module

std/json/json_parse_stream_test.ts

The Deno Standard Library
Go to Latest
File
// 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": "πŸ¦•" }, ]); },});