1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/// JSON support for reading and writing structured data.
///
/// This module provides components for reading JSON data from various sources and writing data to JSON output.
/// The implementation uses `serde_json` for efficient JSON parsing and serialization.
///
/// # Module Architecture
///
/// The JSON module consists of two main components:
///
/// 1. **JsonItemReader**: A streaming JSON reader that can efficiently process large JSON arrays
/// by reading and deserializing objects one at a time without loading the entire file into memory.
/// It works by parsing the JSON buffer character by character, tracking nesting levels, and
/// identifying complete JSON objects.
///
/// 2. **JsonItemWriter**: A JSON writer that serializes items into JSON format and writes them as
/// a properly formatted JSON array. It handles opening and closing array brackets, adding commas
/// between items, and supports both compact and pretty-printed output.
///
/// Each component follows the builder pattern for easy configuration.
///
/// # Features
///
/// - Read JSON data from files, strings, or any source implementing the `Read` trait
/// - Write data to JSON files with configurable formatting options
/// - Seamless integration with Serde for serialization and deserialization
/// - Support for both compact and pretty-printed JSON output
/// - Memory-efficient streaming processing for large datasets
///
/// # Examples
///
/// ## Reading from JSON
///
/// ```
/// use spring_batch_rs::item::json::json_reader::JsonItemReaderBuilder;
/// use spring_batch_rs::core::item::ItemReader;
/// use serde::Deserialize;
/// use std::io::Cursor;
///
/// // Define a data structure matching our JSON format
/// #[derive(Debug, Deserialize, PartialEq)]
/// struct User {
/// id: u64,
/// name: String,
/// email: String,
/// active: bool,
/// }
///
/// // Sample JSON data
/// let json_data = r#"[
/// {
/// "id": 1,
/// "name": "AliceJohnson",
/// "email": "alice@example.com",
/// "active": true
/// },
/// {
/// "id": 2,
/// "name": "BobSmith",
/// "email": "bob@example.com",
/// "active": false
/// }
/// ]"#;
///
/// // Create a reader from our JSON
/// let cursor = Cursor::new(json_data);
/// let reader = JsonItemReaderBuilder::<User>::new()
/// .capacity(1024)
/// .from_reader(cursor);
///
/// // Read and process the users
/// let mut users = Vec::new();
/// while let Some(user) = reader.read().unwrap() {
/// users.push(user);
/// }
///
/// // Verify results
/// assert_eq!(users.len(), 2);
/// assert_eq!(users[0].id, 1);
/// assert_eq!(users[0].name, "AliceJohnson");
/// assert_eq!(users[0].email, "alice@example.com");
/// assert_eq!(users[0].active, true);
///
/// assert_eq!(users[1].id, 2);
/// assert_eq!(users[1].name, "BobSmith");
/// assert_eq!(users[1].email, "bob@example.com");
/// assert_eq!(users[1].active, false);
/// ```
///
/// ## Writing to JSON
///
/// ```
/// use spring_batch_rs::item::json::json_writer::JsonItemWriterBuilder;
/// use spring_batch_rs::core::item::ItemWriter;
/// use serde::Serialize;
/// use std::io::Cursor;
///
/// // Define a data structure for serialization
/// #[derive(Serialize)]
/// struct User {
/// id: u64,
/// name: String,
/// email: String,
/// role: String,
/// skills: Vec<String>,
/// }
///
/// // Create some users
/// let users = vec![
/// User {
/// id: 1,
/// name: "Alice Johnson".to_string(),
/// email: "alice@example.com".to_string(),
/// role: "Developer".to_string(),
/// skills: vec!["Rust".to_string(), "Python".to_string()],
/// },
/// User {
/// id: 2,
/// name: "Bob Smith".to_string(),
/// email: "bob@example.com".to_string(),
/// role: "Designer".to_string(),
/// skills: vec!["UI".to_string(), "UX".to_string(), "Figma".to_string()],
/// },
/// ];
///
/// // Create a writer with a memory buffer and pretty formatting
/// let buffer = Cursor::new(Vec::new());
/// let writer = JsonItemWriterBuilder::<User>::new()
/// .pretty_formatter(true)
/// .from_writer(buffer);
///
/// // Write the users to JSON
/// let writer_ref = &writer as &dyn ItemWriter<User>;
/// writer_ref.open().unwrap();
/// writer_ref.write(&users).unwrap();
/// writer_ref.close().unwrap();
///
/// // The resulting JSON would look similar to:
/// // [
/// // {
/// // "id": 1,
/// // "name": "Alice Johnson",
/// // "email": "alice@example.com",
/// // "role": "Developer",
/// // "skills": [
/// // "Rust",
/// // "Python"
/// // ]
/// // },
/// // {
/// // "id": 2,
/// // "name": "Bob Smith",
/// // "email": "bob@example.com",
/// // "role": "Designer",
/// // "skills": [
/// // "UI",
/// // "UX",
/// // "Figma"
/// // ]
/// // }
/// // ]
/// ```
/// A module providing facilities for reading JSON data records.
/// The `json_writer` module contains the `JsonItemWriter` struct, which is the main entry point for writing items to a JSON data source.
/// It implements the `ItemWriter` trait and provides methods for serializing Rust structs into JSON and writing them to a data source.
// Re-export the main types for easier access
pub use ;
pub use ;