formatJson5Errorfunctionfunction formatJson5Error(e: Json5Error): stringA JSON5 error as one line: 3:14: expected a value.
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"formatJson5Errorfunctionfunction formatJson5Error(e: Json5Error): stringA JSON5 error as one line: 3:14: expected a value.
isBareKeyfunctionno test imports itfunction isBareKey(key: string): booleanWhether a key may stand without quotes: an ECMAScript identifier of ASCII letters, digits, _ and $, not starting with a digit.
isJsonObjectfunctionfunction isJsonObject(v: Json): v is JsonObjectWhether a value is a JSON object.
parseJson5functionfunction parseJson5(text: string): Result<Json, Json5Error>JSON5 (or JSON) text read as a JSON value, or where and why it is not JSON5.
toJson5functionfunction toJson5(value: Json, comment?: string, width?: number): stringA 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).
Jsontypetype 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.
Json5Errortypeno test imports ittype 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 ittype JsonObject = { readonly [key: string]: Json };A JSON object.