Skip to content

Commit

Permalink
feat(Encoding JSON): Add option for compact or indented
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 31, 2021
1 parent 847a28b commit 5ad37af
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rust/src/methods/encode/json.rs
@@ -1,7 +1,15 @@
use super::Options;
use eyre::Result;
use stencila_schema::Node;

/// Encode a `Node` to a JSON document
pub fn encode(node: &Node) -> Result<String> {
Ok(serde_json::to_string_pretty::<Node>(node)?)
///
/// Defaults to pretty (indented). Use "compact" theme for non-indented JSON.
pub fn encode(node: &Node, options: Option<Options>) -> Result<String> {
let Options { theme, .. } = options.unwrap_or_default();
let json = match theme.as_str() {
"compact" => serde_json::to_string::<Node>(node)?,
_ => serde_json::to_string_pretty::<Node>(node)?,
};
Ok(json)
}

0 comments on commit 5ad37af

Please sign in to comment.