Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Encoding JSON): Add option for compact or indented
- Loading branch information
Showing
1 changed file
with
10 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |