IF Commons GitHub

json5

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0001-formats FR-001

JSON5 (https://spec.json5.org/, 1.0.0), read and written with no outside package (commons 0001-formats FR-001): the reader takes the whole grammar (comments, trailing commas, unquoted ECMAScript keys, single-quoted and continued strings, hexadecimal, Infinity, NaN, a leading +, a leading or trailing decimal point) and gives a typed Json value or the line and column where the text stops being JSON5; the writer gives the form a person edits: a // comment at the head, keys bare where they can be, two spaces of indent, a comma after every member, and a short list of plain values on one line. Taken from erga's lib/ts/json5, whose reader was the json5 package.

import { parseJson5, toJson5 } from "./json5.ts";
parseJson5("{ unquoted: 'and you can quote me on that', trailing: [1, 2,], }");
// { kind: "ok", value: { unquoted: "and you can quote me on that", trailing: [1, 2] } }
toJson5({ name: "a", tags: ["x", "y"] }, "What this file is");
// "// What this file is\n{\n  name: \"a\",\n  tags: [\"x\", \"y\"],\n}\n"

Source: commons/formats/src/json5.ts

5 of 8 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

formatJson5Errorfunction
function formatJson5Error(e: Json5Error): string

A JSON5 error as one line: 3:14: expected a value.

Imported by 8 tests of the run (8 passed, 0 failed): the library's results

isBareKeyfunctionno test imports it
function isBareKey(key: string): boolean

Whether a key may stand without quotes: an ECMAScript identifier of ASCII letters, digits, _ and $, not starting with a digit.

isJsonObjectfunction
function isJsonObject(v: Json): v is JsonObject

Whether a value is a JSON object.

Imported by 8 tests of the run (8 passed, 0 failed): the library's results

parseJson5function
function parseJson5(text: string): Result<Json, Json5Error>

JSON5 (or JSON) text read as a JSON value, or where and why it is not JSON5.

Imported by 8 tests of the run (8 passed, 0 failed): the library's results

toJson5function
function toJson5(value: Json, comment?: string, width?: number): string

A value as JSON5 text: comment first, one // line for each of its lines (none where it is empty), then the value, ending in a line break. A list of plain values stays on one line while it fits in width bytes (100 by default).

Imported by 8 tests of the run (8 passed, 0 failed): the library's results

Jsontype
type Json = null | boolean | number | string | readonly Json[] | JsonObject;

A JSON value: what a JSON5 file holds once read. An object keeps its members in the order they were written; a number may be NaN or infinite.

Imported by 19 tests of the run (19 passed, 0 failed): the library's results

Json5Errortypeno test imports it
type Json5Error = { readonly kind: "json5"; readonly line: number; readonly column: number; readonly message: string };

Where a text stops being JSON5, and why. Lines and columns count from 1.

JsonObjecttypeno test imports it
type JsonObject = { readonly [key: string]: Json };

A JSON object.

Titles, headings, exports, tables, columns and requirements. Esc closes.