Skip to content
Snippets Groups Projects
Commit d67e9fb6 authored by Gjalt-Jorn Peters's avatar Gjalt-Jorn Peters
Browse files

fixed JSON import

parent f5ff92af
No related branches found
No related tags found
1 merge request!5Version 0.2.3
Pipeline #365446420 passed
......@@ -41,6 +41,7 @@ export(get_workspace_id)
export(idRef)
export(idSlug)
export(ifelseObj)
export(import_from_json)
export(jstf)
export(justify)
export(load_justifications)
......
File moved
#' Import a structured justifier object from JSON
#'
#' @param x Either a path to an existing file, or a character vector with the
#' JSON to import.
#'
#' @return The justifier object.
#' @export
#'
#' @examples ### Programmatically create a justification with two assertions
#' ### but without sources; flatten it; and show the json
#' justifier::justify(
#' "Icecream will make me feel less fit",
#' assertion = c(
#' justifier::assert('Icecream is rich in energy'),
#' justifier::assert('Consuming high-energy foods makes me feel less fit')
#' ),
#' weight = -.5
#' ) |>
#' justifier::flatten() -> originalObject;
#'
#' originalObject |>
#' justifier::export_to_json() ->
#' exportedJSON;
#'
#' ### And import it again
#' importedFromJSON <-
#' justifier::import_from_json(
#' exportedJSON
#' );
import_from_json <- function(x) {
if (!requireNamespace('jsonlite', quietly=TRUE)) {
stop("You need to have 'jsonlite' installed to convert to JSON!");
}
if ((is.character(x)) && (length(x) == 1) && (file.exists(x))) {
res <- jsonlite::read_json(path = x);
} else if ((is.character(x))) {
res <- jsonlite::fromJSON(x);
} else {
stop("As `x`, you have to pass either a character vector containing ",
"the JSON, or the path to an existing file to import.");
}
### Sources
if (is.null(res$sources) || (length(res$sources) == 0)) {
res$sources <- structure(
list(),
class = c("justifier", "justifierStructured", "justifierSource", "list")
);
} else {
res$sources <-
lapply(res$sources,
`class<-`,
c("justifierSource",
"singleJustifierElement",
"justifierElement",
"justifier"));
class(res$sources) <- c("justifier", "justifierStructured", "justifierSource", "list");
}
### Assertions
if (is.null(res$assertions) || (length(res$assertions) == 0)) {
res$assertions <- structure(
list(),
class = c("justifier", "justifierStructured", "justifierAssertion", "list")
);
} else {
res$assertions <-
lapply(
res$assertions,
replace_empty_lists_with_null,
select = "source"
);
res$assertions <-
lapply(
res$assertions,
set_class_selectively,
select = "source",
class = c("justifier", "justifierIdRef", "justifierSource", "character")
);
res$assertions <-
lapply(res$assertions,
`class<-`,
c("justifierAssertion", "singleJustifierElement", "justifierElement", "justifier"));
class(res$assertions) <- c("justifier", "justifierStructured", "justifierAssertion", "list");
}
### Justifications
if (is.null(res$justifications) || (length(res$justifications) == 0)) {
res$justifications <- structure(
list(),
class = c("justifier", "justifierStructured", "justifierJustification", "list")
);
} else {
res$justifications <-
lapply(
res$justifications,
replace_empty_lists_with_null,
select = "assertion"
);
res$justifications <-
lapply(
res$justifications,
set_class_selectively,
select = "assertion",
class = c("justifier", "justifierIdRef", "justifierAssertion", "character")
);
res$justifications <-
lapply(res$justifications,
`class<-`,
c("justifierJustification", "singleJustifierElement", "justifierElement", "justifier"));
class(res$justifications) <- c("justifier", "justifierStructured", "justifierJustification", "list");
}
### Decisions
if (is.null(res$decisions) || (length(res$decisions) == 0)) {
res$decisions <- structure(
list(),
class = c("justifier", "justifierStructured", "justifierDecision", "list")
);
} else {
res$decisions <-
lapply(
res$decisions,
replace_empty_lists_with_null,
select = "justification"
);
res$decisions <-
lapply(
res$decisions,
set_class_selectively,
select = "justification",
class = c("justifier", "justifierIdRef", "justifierJustification", "character")
);
res$decisions <-
lapply(res$decisions,
`class<-`,
c("justifierDecision",
"singleJustifierElement",
"justifierElement",
"justifier"));
class(res$decisions) <- c("justifier", "justifierStructured", "justifierDecision", "list");
}
### Justifier config
if (is.null(res$justifier)) {
res$justifier <- structure(
list(),
class = c("justifier", "justifierStructured", "justifierJustifier", "list")
);
} else {
res$justifier <-
lapply(res$justifier,
`class<-`,
c("justifierJustifier", "singleJustifierElement", "justifierElement", "justifier"));
class(res$justifier) <- c("justifier", "justifierStructured", "justifierJustifier", "list");
}
class(res) <-
c("justifier", "justifierStructuredObject", "list");
return(res);
}
replace_empty_lists_with_null <- function(x,
select) {
if (is.null(x) || (length(x) == 0)) {
return(x);
}
if (is.list(x[[select]]) &&
(length(x[[select]]) == 0)) {
### This removes `select` from x
x[[select]] <- NULL;
### Add it again
x <-
c(x,
stats::setNames(list(NULL), nm = select)
);
}
return(x);
}
set_class_selectively <- function(x,
select,
class) {
if (is.null(x) || (length(x) == 0)) {
return(x);
}
if (is.character(x[[select]]) &&
(length(x[[select]]) > 0)) {
class(x[[select]]) <- class;
}
return(x);
}
......@@ -215,6 +215,12 @@ decide <-
#' @export
#' @method print singleJustifierElement
print.singleJustifierElement <- function(x, ...) {
if (is.null(x) || (length(x) == 0)) {
cat("You passed an empty justifier element.");
return(invisible(x));
}
cat0("Justifier element of type '",
class(x)[1], "' and with id '",
x$id,
......@@ -226,6 +232,11 @@ print.singleJustifierElement <- function(x, ...) {
#' @export
#' @method print multipleJustifierElements
print.multipleJustifierElements <- function(x, ...) {
if (is.null(x) || (length(x) == 0)) {
cat("You passed an empty list of justifier elements.");
}
cat0("A list of ", length(x), " justifier elements of ",
"type ", class(x[[1]])[1], " and with identifiers ",
vecTxtQ(unlist(lapply(x, function(y) return(y$id)))));
......@@ -237,6 +248,10 @@ print.multipleJustifierElements <- function(x, ...) {
#' @method plot singleJustifierElement
plot.singleJustifierElement <- function(x, ...) {
if (is.null(x) || (length(x) == 0)) {
return(invisible(x));
}
tree <-
create_justifierTree(
x
......@@ -296,10 +311,12 @@ justifierObjectConstructor <-
S = "justifierSource");
class(res) <-
unname(
c(justifierClasses[justifierType],
"singleJustifierElement",
"justifierElement",
"justifier");
"justifier")
);
return(res);
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/justifierStructured_to_json.R
% Please edit documentation in R/export_to_json.R
\name{export_to_json}
\alias{export_to_json}
\alias{export_to_json.justifierStructuredObject}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/import_from_json.R
\name{import_from_json}
\alias{import_from_json}
\title{Import a structured justifier object from JSON}
\usage{
import_from_json(x)
}
\arguments{
\item{x}{Either a path to an existing file, or a character vector with the
JSON to import.}
}
\value{
The justifier object.
}
\description{
Import a structured justifier object from JSON
}
\examples{
### Programmatically create a justification with two assertions
### but without sources; flatten it; and show the json
justifier::justify(
"Icecream will make me feel less fit",
assertion = c(
justifier::assert('Icecream is rich in energy'),
justifier::assert('Consuming high-energy foods makes me feel less fit')
),
weight = -.5
) |>
justifier::flatten() -> originalObject;
originalObject |>
justifier::export_to_json() ->
exportedJSON;
### And import it again
importedJSON <-
justifier::import_from_json(
exportedJSON
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment