use babbel_core::{
BabbelError, FormatEngine, FormatOptions, Value,
io::{IDestination, ISource},
};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct JsonEngine;
impl FormatEngine for JsonEngine {
fn format_id(&self) -> &'static str {
"json"
}
fn mime_type(&self) -> &'static str {
"application/json"
}
fn file_extensions(&self) -> &'static [&'static str] {
&["json"]
}
fn parse(&self, source: &mut dyn ISource) -> Result<Value, BabbelError> {
let node = crate::parser::default::parse(source)
.map_err(|err| BabbelError::syntax(err).with_format("json"))?;
Ok(Value::from(node))
}
fn serialize(
&self,
value: &Value,
destination: &mut dyn IDestination,
options: &FormatOptions,
) -> Result<(), BabbelError> {
if options.pretty {
let node = crate::Node::from(value.clone());
let indent_spaces = options.indent.max(1);
let indent_str = " ".repeat(indent_spaces);
crate::stringify::pretty::stringify_pretty(&node, destination, &indent_str)
.map_err(|e| BabbelError::syntax(e.to_string()).with_format("json"))?;
} else {
value.serialize_json(destination);
}
Ok(())
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct JsonLinesEngine;
impl FormatEngine for JsonLinesEngine {
fn format_id(&self) -> &'static str {
"jsonlines"
}
fn mime_type(&self) -> &'static str {
"application/x-ndjson"
}
fn file_extensions(&self) -> &'static [&'static str] {
&["jsonl", "ndjson"]
}
fn parse_str(&self, input: &str) -> Result<Value, BabbelError> {
let nodes = crate::lines::parse_json_lines(input)
.map_err(|err| BabbelError::syntax(err).with_format("jsonlines"))?;
let values: alloc::vec::Vec<Value> = nodes.into_iter().map(Value::from).collect();
Ok(Value::Array(values))
}
#[cfg(feature = "alloc")]
fn parse_documents(&self, input: &str) -> Result<alloc::vec::Vec<Value>, BabbelError> {
let nodes = crate::lines::parse_json_lines(input)
.map_err(|err| BabbelError::syntax(err).with_format("jsonlines"))?;
Ok(nodes.into_iter().map(Value::from).collect())
}
fn serialize(
&self,
value: &Value,
destination: &mut dyn IDestination,
_options: &FormatOptions,
) -> Result<(), BabbelError> {
let records = match value {
Value::Array(arr) => arr.as_slice(),
single => core::slice::from_ref(single),
};
for r in records {
r.serialize_json(destination);
destination.add_byte(b'\n');
}
Ok(())
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Json5Engine;
impl FormatEngine for Json5Engine {
fn format_id(&self) -> &'static str {
"json5"
}
fn mime_type(&self) -> &'static str {
"application/json5"
}
fn file_extensions(&self) -> &'static [&'static str] {
&["json5", "jsonc"]
}
fn parse_str(&self, input: &str) -> Result<Value, BabbelError> {
let node = crate::parser::json5::parse_json5(input)
.map_err(|err| BabbelError::syntax(err).with_format("json5"))?;
Ok(Value::from(node))
}
fn serialize(
&self,
value: &Value,
destination: &mut dyn IDestination,
options: &FormatOptions,
) -> Result<(), BabbelError> {
if options.pretty {
let node = crate::Node::from(value.clone());
let indent_spaces = options.indent.max(1);
let indent_str = " ".repeat(indent_spaces);
crate::stringify::pretty::stringify_pretty(&node, destination, &indent_str)
.map_err(|e| BabbelError::syntax(e.to_string()).with_format("json"))?;
} else {
value.serialize_json(destination);
}
Ok(())
}
}