-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allows serialization and deserialization to json
- Loading branch information
1 parent
58c8d8c
commit 051a6b5
Showing
13 changed files
with
222 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' @title Serialize the graph into a JSON string | ||
#' @param graph A graph object | ||
#' @param pretty If the JSON string should be prettified | ||
#' @return A JSON string | ||
#' @export | ||
graph_to_json <- function(graph, pretty = FALSE) { | ||
UseMethod("graph_to_json") | ||
} | ||
|
||
#' @export | ||
graph_to_json.DirectedGraph <- function(graph, pretty = FALSE) { | ||
rs_directed_graph_to_json(graph, pretty) | ||
} | ||
|
||
#' @export | ||
graph_to_json.DirectedAcyclicGraph <- function(graph, pretty = FALSE) { | ||
rs_dag_to_json(graph, pretty) | ||
} | ||
|
||
#' @title Get a graph from a JSON string | ||
#' @param text The JSON string to be deserialized | ||
#' @param type The type of graph the JSON represents | ||
#' @return A graph object | ||
#' @export | ||
graph_from_json <- function(text, type = c("directed", "dag")) { | ||
if (!type %in% c("directed", "dag")) { | ||
rlang::abort("Invalid argument `type`") | ||
} | ||
switch( | ||
type[1], | ||
"directed" = rs_directed_graph_from_json(text), | ||
"dag" = rs_dag_from_json(text) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::{to_r_error, DirectedAcyclicGraph, DirectedGraph}; | ||
use extendr_api::prelude::*; | ||
use orbweaver::prelude as ow; | ||
|
||
#[extendr] | ||
fn rs_directed_graph_to_json(directed_graph: &DirectedGraph, pretty: bool) -> Result<String> { | ||
if pretty { | ||
Ok(serde_json::to_string_pretty(&directed_graph.0).map_err(to_r_error)?) | ||
} else { | ||
Ok(serde_json::to_string(&directed_graph.0).map_err(to_r_error)?) | ||
} | ||
} | ||
|
||
#[extendr] | ||
fn rs_directed_graph_from_json(text: &str) -> Result<DirectedGraph> { | ||
let dg: ow::DirectedGraph<Robj> = serde_json::from_str(text).map_err(|e| e.to_string())?; | ||
Ok(DirectedGraph(dg)) | ||
} | ||
|
||
#[extendr] | ||
fn rs_dag_to_json(dag: &DirectedAcyclicGraph, pretty: bool) -> Result<String> { | ||
if pretty { | ||
Ok(serde_json::to_string_pretty(&dag.0).map_err(to_r_error)?) | ||
} else { | ||
Ok(serde_json::to_string(&dag.0).map_err(to_r_error)?) | ||
} | ||
} | ||
|
||
#[extendr] | ||
fn rs_dag_from_json(text: &str) -> Result<DirectedAcyclicGraph> { | ||
let dag: ow::DirectedAcyclicGraph<Robj> = | ||
serde_json::from_str(text).map_err(|e| e.to_string())?; | ||
Ok(DirectedAcyclicGraph(dag)) | ||
} | ||
|
||
extendr_module! { | ||
mod to_json; | ||
fn rs_directed_graph_from_json; | ||
fn rs_directed_graph_to_json; | ||
fn rs_dag_to_json; | ||
fn rs_dag_from_json; | ||
} |
Binary file not shown.