JSON Processing
Spring Batch RS provides streaming JSON support for processing large JSON arrays efficiently. The reader processes objects one at a time without loading the entire file into memory.
Quick Start
Section titled “Quick Start”use spring_batch_rs::item::json::{JsonItemReaderBuilder, JsonItemWriterBuilder};
// Read JSON arraylet reader = JsonItemReaderBuilder::<Order>::new() .from_reader(file);
// Write JSON with pretty formattinglet writer = JsonItemWriterBuilder::<Order>::new() .pretty_formatter(true) .from_path("output.json");Features
Section titled “Features”- Streaming parser: Memory-efficient processing of large JSON arrays
- Pretty formatting: Optional indented output for readability
- Format conversion: Convert to/from CSV, XML, and other formats
- Type safety: Automatic serialization/deserialization with serde
Complete Example
Section titled “Complete Example”The json_processing example demonstrates:
- Read JSON array: Parse and log JSON objects
- JSON transformation: Apply business logic during processing
- JSON to CSV export: Convert orders to summary CSV
- Formatting options: Compare compact vs pretty output
Run the Example
Section titled “Run the Example”cargo run --example json_processing --features json,csv,loggerAPI Reference
Section titled “API Reference”JsonItemReaderBuilder
Section titled “JsonItemReaderBuilder”| Method | Description |
|---|---|
capacity(usize) | Set internal buffer size (default: 8192 bytes) |
from_reader(R) | Create reader from any Read source |
JsonItemWriterBuilder
Section titled “JsonItemWriterBuilder”| Method | Description |
|---|---|
pretty_formatter(bool) | Enable indented output (default: false) |
indent(&[u8]) | Set custom indentation (default: 2 spaces) |
from_writer(W) | Create writer for any Write destination |
from_path(P) | Create writer to file path |
Input Format
Section titled “Input Format”The JSON reader expects an array of objects:
[ {"id": 1, "name": "Alice", "total": 99.99}, {"id": 2, "name": "Bob", "total": 149.50}, {"id": 3, "name": "Charlie", "total": 75.00}]Common Patterns
Section titled “Common Patterns”Processing with Transformation
Section titled “Processing with Transformation”struct TaxProcessor { rate: f64 }
impl ItemProcessor<Order, Order> for TaxProcessor { fn process(&self, item: &Order) -> Result<Order, BatchError> { Ok(Order { total: item.total * (1.0 + self.rate), ..item.clone() }) }}Reading from In-Memory String
Section titled “Reading from In-Memory String”use std::io::Cursor;
let json_data = r#"[{"id": 1, "name": "test"}]"#;let reader = JsonItemReaderBuilder::<Record>::new() .from_reader(Cursor::new(json_data));Multi-Format Pipeline
Section titled “Multi-Format Pipeline”use spring_batch_rs::core::item::PassThroughProcessor;
// Step 1: JSON to intermediate formatlet processor1 = PassThroughProcessor::<Internal>::new();let step1 = StepBuilder::new("json-to-internal") .chunk::<Internal, Internal>(100) .reader(&json_reader) .processor(&processor1) .writer(&internal_writer) .build();
// Step 2: Internal to CSV exportlet processor2 = PassThroughProcessor::<Internal>::new();let step2 = StepBuilder::new("internal-to-csv") .chunk::<Internal, Internal>(100) .reader(&internal_reader) .processor(&processor2) .writer(&csv_writer) .build();
let job = JobBuilder::new() .start(&step1) .next(&step2) .build();See Also
Section titled “See Also”- CSV Processing - Convert JSON to CSV
- XML Processing - Convert JSON to XML
- Advanced Patterns - Complex ETL pipelines