% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/serialization.R
\name{write_dependency_graph}
\alias{write_dependency_graph}
\alias{read_dependency_graph}
\title{Serialize a Dependency Graph to JSON}
\usage{
write_dependency_graph(graph, path, pretty = TRUE)

read_dependency_graph(path)
}
\arguments{
\item{graph}{A \code{dependency_graph} produced by
\code{build_dependency_graph()} or \code{graph_from_metadata()}.}

\item{path}{Path to write to or read from.}

\item{pretty}{If \code{TRUE} (default), the JSON is indented for human
inspection. Set \code{FALSE} for a compact representation.}
}
\value{
\code{write_dependency_graph()} invisibly returns \code{path}.
  \code{read_dependency_graph()} returns a validated
  \code{dependency_graph}.
}
\description{
Write a \code{dependency_graph} to a JSON file and read it back. The on-disk
format is intentionally simple and stable: it captures the canonical node
table, the canonical edge table (each with their list-column of
attributes), the graph metadata (including \code{validation_overrides}),
and the data-model \code{schema_version}. The internal \code{igraph}
representation is not stored; it is rebuilt on read via
\code{dependency_graph()}.
}
\details{
This makes \code{split_spec}/\code{dependency_graph} objects portable
across R sessions, and across language boundaries (any consumer that can
read JSON can interpret the format).
}
\section{JSON format}{

\preformatted{
{
  "$schema": "https://.../inst/schema/dependency_graph.schema.json",
  "splitGraph_object": "dependency_graph",
  "schema_version": "0.2.0",
  "metadata": {
    "graph_name": "...",
    "dataset_name": "...",
    "created_at": "2026-04-29T10:11:12.000000+0000",
    "schema_version": "0.2.0",
    "validation_overrides": { ... }
  },
  "nodes": [
    { "node_id": "sample:S1", "node_type": "Sample",
      "node_key": "S1", "label": "S1", "attrs": { ... } },
    ...
  ],
  "edges": [
    { "edge_id": "sample_belongs_to_subject:1",
      "from": "sample:S1", "to": "subject:P1",
      "edge_type": "sample_belongs_to_subject", "attrs": { ... } },
    ...
  ]
}
}
Reading a file whose \code{schema_version} shares the installed major
version loads silently (additive-only differences); a differing major
version loads with a warning suggesting \code{migrate_dependency_graph_json()}.
The written JSON also carries a \code{$schema} reference to the formal JSON
Schema shipped in \code{inst/schema/}; validate a file against it with
\code{validate_graph_json()}.
}

\examples{
if (requireNamespace("jsonlite", quietly = TRUE)) {
  meta <- data.frame(
    sample_id  = c("S1", "S2"),
    subject_id = c("P1", "P2")
  )
  g <- graph_from_metadata(meta, graph_name = "demo")

  tmp <- tempfile(fileext = ".json")
  write_dependency_graph(g, tmp)
  g2 <- read_dependency_graph(tmp)
  identical(g$nodes$data$node_id, g2$nodes$data$node_id)
  unlink(tmp)
}
}
