% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/serialization.R
\name{write_split_spec}
\alias{write_split_spec}
\alias{read_split_spec}
\title{Serialize a Split Specification to JSON}
\usage{
write_split_spec(spec, path, pretty = TRUE)

read_split_spec(path)
}
\arguments{
\item{spec}{A \code{split_spec} produced by \code{as_split_spec()}.}

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

\item{pretty}{If \code{TRUE} (default), the JSON is indented.}
}
\value{
\code{write_split_spec()} invisibly returns \code{path}.
  \code{read_split_spec()} returns a \code{split_spec}.
}
\description{
Write a \code{split_spec} to a JSON file and read it back. The on-disk
format captures the canonical sample-level table (\code{sample_data}) plus
all spec-level fields needed by a downstream resampling adapter
(\code{group_var}, \code{block_vars}, \code{time_var},
\code{ordering_required}, \code{constraint_mode},
\code{constraint_strategy}, \code{recommended_resampling}) and the spec
metadata.
}
\details{
\code{NA} values in \code{sample_data} are written as JSON \code{null} and
read back as \code{NA}.
}
\section{JSON format}{

\preformatted{
{
  "$schema": "https://.../inst/schema/split_spec.schema.json",
  "splitGraph_object": "split_spec",
  "schema_version": "0.2.0",
  "group_var": "group_id",
  "block_vars": ["batch_group", "study_group"],
  "time_var": "order_rank",
  "ordering_required": false,
  "constraint_mode": "subject",
  "constraint_strategy": "subject",
  "recommended_resampling": "grouped_cv",
  "metadata": { ... },
  "sample_data": [
    { "sample_id": "S1", "group_id": "subject:P1", ... },
    ...
  ]
}
}
}

\examples{
if (requireNamespace("jsonlite", quietly = TRUE)) {
  meta <- data.frame(
    sample_id  = c("S1", "S2"),
    subject_id = c("P1", "P2")
  )
  g <- graph_from_metadata(meta)
  constraint <- derive_split_constraints(g, mode = "subject")
  spec <- as_split_spec(constraint, graph = g)

  tmp <- tempfile(fileext = ".json")
  write_split_spec(spec, tmp)
  spec2 <- read_split_spec(tmp)
  identical(spec$sample_data$group_id, spec2$sample_data$group_id)
  unlink(tmp)
}
}
