JSON field values for HTTP parser and serializer.
Compliant with A JSON Encoding for HTTP Field Values.
Parse string into JSON field value.
import { parseJfv } from "https://deno.land/x/jfv_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const result = parseJfv(`"\\u221e", {"date":"2012-08-25"}, [17,42]`);
assertEquals(result, ["∞", { "date": "2012-08-25" }, [17, 42]]);
According to the specification, always return an array.
The conditions for throwing an error are the same as for JSON#parse
.
import { parseJfv } from "https://deno.land/x/jfv_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => parseJfv("<invalid:JSON>"));
Serialize any array into string.
import { stringifyJfv } from "https://deno.land/x/jfv_parser@$VERSION/stringify.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const result = stringifyJfv(["∞", { "date": "2012-08-25" }, [17, 42]]);
assertEquals(result, `"\\u221e", {"date":"2012-08-25"}, [17,42]`);
According to the specification, encoding into octet sequences using the US-ASCII character encoding scheme.
The conditions for throwing an error are the same as for JSON#stringify
.
import { stringifyJfv } from "https://deno.land/x/jfv_parser@$VERSION/stringify.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
const cycle: Record<string, {}> = {};
cycle.deep = { cycle };
assertThrows(() => stringifyJfv([cycle]));
JSON field value is an array of JsonValue
.
type JsonValue =
| { [key: string]: JsonValue }
| JsonValue[]
| string
| number
| boolean
| null;
All APIs can be found in the deno doc.
Copyright © 2023-present httpland.
Released under the MIT license