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
import { serialize } from "@jayoncode/object-diff";
// or: import { serialize, createSerializer } from "@jayoncode/object-diff/formatter";Problem → approach
| Typical pain | serialize() |
|---|---|
console.log(diffResult) is noisy and not PR-ready | serialize(result, "markdown") for changelogs |
| APIs need JSON; humans need tables or prose | Same diff() output, many formats |
| Custom formatters duplicated across tools | Built-ins + createSerializer plugins |
Formats
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| Format | Best for |
|---|---|
json / pretty | APIs, structured logs |
markdown | PRs, docs, changelogs |
table | CLI / quick scans |
html | Email / docs tables (escaped) |
console | Terminal output |
human | Short summaries |
Custom formatters
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 workNo 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):
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.
