to_json: Serialise a 'fitverse' Object to JSON

View source: R/s3_methods.R

to_jsonR Documentation

Serialise a fitverse Object to JSON

Description

Converts a fitverse object to a JSON string. Useful for passing fit results to web front-ends, storing results in document databases, or integrating with REST APIs.

The serialisation uses toJSON() on the output of as.list.fitverse(), which strips non-serialisable internals (raw optimiser fit objects, closures, environments) and returns a flat, portable representation.

Usage

to_json(x, pretty = TRUE, ...)

## S3 method for class 'fitverse'
to_json(x, pretty = TRUE, ...)

Arguments

x

An object with a to_json method; currently "fitverse" objects are supported.

pretty

Logical: if TRUE (default), produce indented, human-readable JSON. If FALSE, produce compact JSON (smaller output, faster parsing).

...

Additional arguments passed to jsonlite::toJSON(), for example digits, na or null.

Details

Requires the jsonlite package (listed in Suggests). An informative error is raised if it is not installed.

The JSON output includes:

  • best_fit: distribution key, method, parameter estimates, loglik, AIC, BIC.

  • ranking: full ranking data frame.

  • data_summary: descriptive statistics.

  • n, xlab, criterion.

The raw data vector is deliberately not included to keep the output compact and privacy-friendly for web contexts.

Value

A single character string containing the JSON representation of x.

Author(s)

Karuna G. Reddy karuna.reddy@auckland.ac.nz
M. G. M. Khan mgm.khan@usp.ac.fj

See Also

as.list.fitverse for the underlying list conversion.
fromJSON to parse the JSON back to R.

Examples


if (requireNamespace("jsonlite", quietly = TRUE)) {
  set.seed(42)
  x <- rgamma(300, shape = 2, rate = 0.5)
  fit <- fitverse(x, plot = FALSE, verbose = FALSE)

  json_str <- to_json(fit)
  cat(substr(json_str, 1, 400))

  # Round-trip back to R list
  lst <- jsonlite::fromJSON(json_str)
  lst$best_fit$params
}


FitVerse documentation built on Dec. 1, 2026, 1:07 a.m.