JsonValuetypeno test imports ittype JsonValue = null | boolean | number | string | readonly JsonValue[] | JsonObject;A JSON value: what every tree codec reads and writes.
The codec of a document: a pair of functions between a value and the bytes of an object, with the media type of the bytes. A codec never throws: it ends with a result, and the bytes it makes are canonical, so that one value is always the same bytes and therefore the same revision on a store that names revisions by content. The codecs here are the ones that need nothing: bytes, text and JSON (./formats has the codecs that need the file formats of commons).
import { jsonTree, canonicalJson } from "./codec.ts";
canonicalJson({ b: 1, a: [true, null] }); // '{\n "a": [\n true,\n null\n ],\n "b": 1\n}\n'
jsonTree.mediaType; // "application/json"JsonValuetypeno test imports ittype JsonValue = null | boolean | number | string | readonly JsonValue[] | JsonObject;A JSON value: what every tree codec reads and writes.
JsonObjecttypeno test imports ittype JsonObject = { readonly [key: string]: JsonValue };A JSON object.
CODEC_REASONSconstno test imports itconst CODEC_REASONS: readonly ["malformed", "invalid", "newer-version", "unencodable"]Why a codec could not make or read a document: a closed list.
CodecReasontypeno test imports ittype CodecReason = (typeof CODEC_REASONS)[number];One reason a codec could not make or read a document.
CodecProblemtypeno test imports ittype CodecProblem = { readonly reason: CodecReason; readonly detail: string };What a codec says when it does not: the reason and one sentence.
Decodedtypeno test imports ittype Decoded<T> = { readonly value: T; readonly stale: boolean };What a codec read: the value and whether the bytes were written at an older version than the current one.
Codectypeno test imports ittype Codec<T> = {
readonly mediaType: string;
encode(value: T): Result<Uint8Array, CodecProblem>;
decode(bytes: Uint8Array): Result<Decoded<T>, CodecProblem>;
};Bytes to a value and back, with the media type of the bytes.
problemconstno test imports itconst problem: (reason: "invalid" | "malformed" | "newer-version" | "unencodable", detail: string) => CodecProblemA problem of one reason.
toJsonValuefunctionfunction toJsonValue(value: unknown, depth?: number): Result<JsonValue, string>A value as a JSON value, or why it is not one: only null, booleans, finite numbers, well-formed strings, arrays and plain objects are, to a depth of 64.
isJsonObjectconstno test imports itconst isJsonObject: (value: JsonValue) => value is JsonObjectWhether a JSON value is an object.
canonicalJsonfunctionfunction canonicalJson(value: JsonValue): stringA JSON value as canonical text: keys in order, two-space indentation, one newline at the end, so that one value is one text.
bytesCodecconstconst bytesCodec: Codec<Uint8Array<ArrayBufferLike>>The bytes as they are.
textCodecconstconst textCodec: Codec<string>The bytes as UTF-8 text: bytes that are not UTF-8 are malformed.
jsonTreeconstconst jsonTree: Codec<JsonValue>The bytes as canonical JSON of any value.