Skip to main content

edgeparse_core/output/
json.rs

1//! JSON output writer.
2
3use crate::models::document::PdfDocument;
4use crate::EdgePdfError;
5
6/// Write a PdfDocument as JSON to the given writer.
7///
8/// # Errors
9/// Returns `EdgePdfError::OutputError` if serialization fails.
10pub fn write_json<W: std::io::Write>(
11    doc: &PdfDocument,
12    writer: &mut W,
13) -> Result<(), EdgePdfError> {
14    serde_json::to_writer_pretty(writer, doc)
15        .map_err(|e| EdgePdfError::OutputError(format!("JSON serialization failed: {}", e)))
16}
17
18/// Serialize a PdfDocument to a JSON string.
19///
20/// # Errors
21/// Returns `EdgePdfError::OutputError` if serialization fails.
22pub fn to_json_string(doc: &PdfDocument) -> Result<String, EdgePdfError> {
23    serde_json::to_string_pretty(doc)
24        .map_err(|e| EdgePdfError::OutputError(format!("JSON serialization failed: {}", e)))
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_json_output() {
33        let doc = PdfDocument::new("test.pdf".to_string());
34        let json = to_json_string(&doc).unwrap();
35        assert!(json.contains("test.pdf"));
36        assert!(json.contains("number_of_pages"));
37    }
38}