use crate::error::Result;
use crate::model::Document;
use super::options::RenderOptions;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum JsonFormat {
Compact,
#[default]
Pretty,
}
pub fn to_json(doc: &Document, format: JsonFormat) -> Result<String> {
match format {
JsonFormat::Compact => serde_json::to_string(doc)
.map_err(|e| crate::error::Error::XmlParse(format!("JSON serialization error: {}", e))),
JsonFormat::Pretty => serde_json::to_string_pretty(doc)
.map_err(|e| crate::error::Error::XmlParse(format!("JSON serialization error: {}", e))),
}
}
pub fn to_json_default(doc: &Document) -> Result<String> {
to_json(doc, JsonFormat::Pretty)
}
pub fn to_json_with_options(doc: &Document, _options: &RenderOptions) -> Result<String> {
to_json(doc, JsonFormat::Pretty)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{HeadingLevel, Paragraph, Section};
#[test]
fn test_to_json_pretty() {
let mut doc = Document::new();
doc.metadata.title = Some("Test".to_string());
let mut section = Section::new(0);
section.add_paragraph(Paragraph::with_text("Hello"));
doc.add_section(section);
let json = to_json(&doc, JsonFormat::Pretty).unwrap();
assert!(json.contains("\"title\": \"Test\""));
assert!(json.contains("\"text\": \"Hello\""));
}
#[test]
fn test_to_json_compact() {
let mut doc = Document::new();
doc.metadata.title = Some("Test".to_string());
let json = to_json(&doc, JsonFormat::Compact).unwrap();
assert!(!json.contains('\n')); assert!(json.contains("\"title\":\"Test\""));
}
#[test]
fn test_to_json_default() {
let doc = Document::new();
let json = to_json_default(&doc).unwrap();
assert!(json.contains('\n')); }
#[test]
fn test_document_roundtrip() {
let mut doc = Document::new();
doc.metadata.title = Some("Roundtrip Test".to_string());
doc.metadata.author = Some("Test Author".to_string());
let mut section = Section::new(0);
section.name = Some("Section 1".to_string());
section.add_paragraph(Paragraph::heading(HeadingLevel::H1, "Heading"));
section.add_paragraph(Paragraph::with_text("Content."));
doc.add_section(section);
let json = to_json(&doc, JsonFormat::Pretty).unwrap();
let parsed: Document = serde_json::from_str(&json).unwrap();
assert_eq!(parsed.metadata.title, doc.metadata.title);
assert_eq!(parsed.metadata.author, doc.metadata.author);
assert_eq!(parsed.sections.len(), 1);
}
}