R/form_to_json.R

Defines functions form_to_json

Documented in form_to_json

#' @export
#' @rdname export_to_yaml_or_json
form_to_json <- function(x,
                         file = NULL) {

  x <- retrieve_form(x);

  x <-
    list(
      form =
        list(
          instructions = serialize_df(x$instructions, idCol = "heading"),
          metadata = serialize_df(x$metadata, idCol = "field"),
          items = serialize_df(x$items, idCol = "item_id"),
          sections = serialize_df(x$sections, idCol = "section_id"),
          valueTemplates = serialize_df(x$valueTemplates, idCol = "identifier")
        )
    );

  if (is.null(file)) {
    res <-
      jsonlite::toJSON(
        x,
        pretty = TRUE,
        force = TRUE
      );
    class(res) <- c("preregr_json", class(res));
    return(
      res
    );
  } else {
    jsonlite::write_json(
      x,
      file,
      pretty = TRUE,
      force = TRUE
    );
    return(invisible(x));
  }
}

Try the preregr package in your browser

Any scripts or data that you put into this service are public.

preregr documentation built on May 31, 2023, 7:10 p.m.