/jfv-parser

JSON field values for HTTP parser and serialize

Primary LanguageTypeScriptMIT LicenseMIT

jfv-parser

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

JSON field values for HTTP parser and serializer.

Compliant with A JSON Encoding for HTTP Field Values.

Parsing

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.

Syntax error

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>"));

Serialization

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.

TypeError

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

JSON field value is an array of JsonValue.

type JsonValue =
  | { [key: string]: JsonValue }
  | JsonValue[]
  | string
  | number
  | boolean
  | null;

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license