use crate::nodes::node::Node;
use crate::parser::default::from_str;
use crate::stringify::default::stringify;
use babbel_core::io::{IDestination, ILineReader, SliceSource, StringDestination};
#[cfg(feature = "std")]
use std::string::String;
#[cfg(feature = "std")]
use std::vec::Vec;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JsonLinesConfig {
pub ignore_empty_lines: bool,
pub ignore_comments: bool,
pub trim_whitespace: bool,
}
impl Default for JsonLinesConfig {
fn default() -> Self {
Self {
ignore_empty_lines: true,
ignore_comments: true,
trim_whitespace: true,
}
}
}
pub struct JsonLinesReader<R> {
reader: R,
config: JsonLinesConfig,
line_number: usize,
}
impl<R: ILineReader> JsonLinesReader<R> {
pub fn new(reader: R) -> Self {
Self::with_config(reader, JsonLinesConfig::default())
}
pub fn with_config(reader: R, config: JsonLinesConfig) -> Self {
Self {
reader,
config,
line_number: 0,
}
}
pub fn line_number(&self) -> usize {
self.line_number
}
pub fn next_node(&mut self) -> Option<Result<Node, String>> {
while let Some(line) = self.reader.read_line() {
self.line_number += 1;
let check_slice = if self.config.trim_whitespace {
line.trim()
} else {
&line[..]
};
if self.config.ignore_empty_lines && check_slice.is_empty() {
continue;
}
if self.config.ignore_comments && (check_slice.starts_with('#') || check_slice.starts_with("//")) {
continue;
}
return Some(from_str(check_slice));
}
None
}
}
impl<R: ILineReader> Iterator for JsonLinesReader<R> {
type Item = Result<Node, String>;
fn next(&mut self) -> Option<Self::Item> {
self.next_node()
}
}
pub fn parse_json_lines(input: &str) -> Result<Vec<Node>, String> {
let source = SliceSource::new(input);
let reader = JsonLinesReader::new(source);
let mut nodes = Vec::new();
for res in reader {
nodes.push(res?);
}
Ok(nodes)
}
pub fn to_json_lines(nodes: &[Node]) -> Result<String, String> {
let mut dest = StringDestination::default();
to_json_lines_stream(nodes, &mut dest)?;
Ok(dest.into_string())
}
pub fn to_json_lines_stream(nodes: &[Node], destination: &mut dyn IDestination) -> Result<(), String> {
for node in nodes {
stringify(node, destination)?;
destination.add_byte(b'\n');
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_json_lines_basic() {
let input = "{\"name\": \"Alice\", \"age\": 30}\n{\"name\": \"Bob\", \"age\": 25}\n";
let nodes = parse_json_lines(input).unwrap();
assert_eq!(nodes.len(), 2);
assert_eq!(nodes[0].get("name").and_then(|n| n.as_str()), Some("Alice"));
assert_eq!(nodes[1].get("name").and_then(|n| n.as_str()), Some("Bob"));
}
#[test]
fn test_parse_json_lines_with_comments_and_blanks() {
let input = "# Header comments\n\n{\"id\": 1}\n// Inline comment\n \n{\"id\": 2}\n";
let nodes = parse_json_lines(input).unwrap();
assert_eq!(nodes.len(), 2);
}
#[test]
fn test_to_json_lines_roundtrip() {
let n1 = from_str("{\"x\":10}").unwrap();
let n2 = from_str("{\"y\":20}").unwrap();
let serialized = to_json_lines(&[n1, n2]).unwrap();
assert_eq!(serialized, "{\"x\":10}\n{\"y\":20}\n");
let parsed = parse_json_lines(&serialized).unwrap();
assert_eq!(parsed.len(), 2);
}
}