Skip to content

Serialization

Export diff results for humans, logs, or downstream tools.

Previous: Patching · Next: Engines & entrypoints

Playground

Open JSON viewer → — inspect serialized output formats.

Prefer @jayoncode/object-diff/formatter for formatting-focused apps (root still re-exports serialize).

Import path

ts
import { serialize } from "@jayoncode/object-diff";
// or: import { serialize, createSerializer } from "@jayoncode/object-diff/formatter";

Engines.

Problem → approach

Typical painserialize()
console.log(diffResult) is noisy and not PR-readyserialize(result, "markdown") for changelogs
APIs need JSON; humans need tables or proseSame diff() output, many formats
Custom formatters duplicated across toolsBuilt-ins + createSerializer plugins

Formats

ts
import { diff, serialize } from "@jayoncode/object-diff";
// or: import { serialize, createSerializer } from "@jayoncode/object-diff/formatter";

const result = diff(before, after);

serialize(result, "json");
serialize(result, "pretty");
serialize(result, "markdown", { title: "Form changes" });
serialize(result, "table");
serialize(result, "html", { title: "Audit" }); // HTML-escaped
serialize(result, "console"); // ANSI colors; { color: false } to disable
serialize(result, "human"); // short prose + bullets
FormatBest for
json / prettyAPIs, structured logs
markdownPRs, docs, changelogs
tableCLI / quick scans
htmlEmail / docs tables (escaped)
consoleTerminal output
humanShort summaries

Custom formatters

ts
import { createSerializer } from "@jayoncode/object-diff/formatter";

const serializeWith = createSerializer([
  {
    name: "csv",
    format: (result) => result.changes.map((c) => `${c.type},${c.path}`).join("\n"),
  },
]);

serializeWith(result, "csv");
serializeWith(result, "human"); // built-ins still work

No import side effects — pass plugins explicitly. Plugin names must not collide with built-ins.

Named format helpers

Each built-in format is also exported as a standalone named function from @jayoncode/object-diff/formatter, in case you want to call one directly without going through serialize(result, format):

ts
import {
  serializeJson,
  serializeMarkdown,
  serializeTable,
  serializeHtml,
  serializeConsole,
  serializeHuman,
} from "@jayoncode/object-diff/formatter";

serializeJson(result, /* pretty */ false);
serializeMarkdown(result, { title: "Form changes" });
serializeTable(result);
serializeHtml(result, { title: "Audit" });
serializeConsole(result, { color: false });
serializeHuman(result);

serializeJson takes a pretty: boolean second argument (not SerializeOptions) — serialize(result, "json") / serialize(result, "pretty") cover the same two cases without needing the named helper. The other named helpers take the same SerializeOptions as their serialize(result, format, options) equivalent.

Next: Engines — merge, query, stats, plugins, view, and slim /core.

An ecosystem of independent, headless TypeScript libraries engineered for modern web development. Every package includes interactive playgrounds and documentation that evolves alongside the code.