1use babbel_json::{
7 BufferDestination, BufferSource, FileDestination, FileSource, Format, Node, detect_format,
8 parse, read_file_to_string, stringify, write_file_from_string,
9};
10use std::collections::HashMap;
11use std::path::Path;
12
13fn main() {
14 println!("=== Unicode File Handling Demo ===\n");
15
16 println!("1. Creating sample JSON data:");
18
19 let mut data = HashMap::new();
20 data.insert("greeting".to_string(), Node::from("Hello, δΈη! π"));
21 data.insert("title".to_string(), Node::from("ΞαλημΞΟΞ± ΞΊΟΟΞΌΞ΅"));
22 data.insert("description".to_string(), Node::from("ΠΡΠΈΠ²Π΅Ρ ΠΌΠΈΡ"));
23 data.insert("emoji".to_string(), Node::from("π β π»"));
24
25 let json_node = Node::Object(data);
26
27 let mut buffer = BufferDestination::new();
28 babbel_json::print(&json_node, &mut buffer, 2);
29 let json_content = buffer.to_string();
30
31 println!("{}", json_content);
32
33 println!("\n2. Writing JSON in different Unicode formats:");
35
36 let test_files = vec![
37 ("test_utf8.json", Format::Utf8),
38 ("test_utf8_bom.json", Format::Utf8bom),
39 ("test_utf16le.json", Format::Utf16le),
40 ("test_utf16be.json", Format::Utf16be),
41 ("test_utf32le.json", Format::Utf32le),
42 ("test_utf32be.json", Format::Utf32be),
43 ];
44
45 for (filename, format) in test_files.iter() {
46 let format_val = match format {
47 Format::Utf8 => Format::Utf8,
48 Format::Utf8bom => Format::Utf8bom,
49 Format::Utf16le => Format::Utf16le,
50 Format::Utf16be => Format::Utf16be,
51 Format::Utf32le => Format::Utf32le,
52 Format::Utf32be => Format::Utf32be,
53 };
54
55 match write_file_from_string(filename, &json_content, format_val) {
56 Ok(_) => {
57 let format_name = match format {
58 Format::Utf8 => "UTF-8 (no BOM)",
59 Format::Utf8bom => "UTF-8 with BOM",
60 Format::Utf16le => "UTF-16 LE",
61 Format::Utf16be => "UTF-16 BE",
62 Format::Utf32le => "UTF-32 LE",
63 Format::Utf32be => "UTF-32 BE",
64 };
65 println!(" β Wrote {} as {}", filename, format_name);
66 }
67 Err(e) => println!(" β Failed to write {}: {}", filename, e),
68 }
69 }
70
71 println!("\n3. Detecting Unicode format from files:");
73
74 for (filename, _) in &test_files {
75 if Path::new(filename).exists() {
76 match detect_format(filename) {
77 Ok(detected) => {
78 let format_name = match detected {
79 Format::Utf8 => "UTF-8 (no BOM)",
80 Format::Utf8bom => "UTF-8 with BOM",
81 Format::Utf16le => "UTF-16 LE",
82 Format::Utf16be => "UTF-16 BE",
83 Format::Utf32le => "UTF-32 LE",
84 Format::Utf32be => "UTF-32 BE",
85 };
86 println!(" {}: {}", filename, format_name);
87 }
88 Err(e) => println!(" {}: Error - {}", filename, e),
89 }
90 }
91 }
92
93 println!("\n4. Reading and parsing files:");
95
96 for (filename, _) in test_files.iter().take(3) {
97 if Path::new(filename).exists() {
98 match read_file_to_string(filename) {
99 Ok(content) => {
100 let mut source = BufferSource::new(content.as_bytes());
101 match parse(&mut source) {
102 Ok(node) => {
103 if let Some(greeting) = node.get("greeting").and_then(|g| g.as_str()) {
104 println!(" {} - greeting: {}", filename, greeting);
105 }
106 }
107 Err(e) => println!(" {} - Parse error: {}", filename, e),
108 }
109 }
110 Err(e) => println!(" {} - Read error: {}", filename, e),
111 }
112 }
113 }
114
115 println!("\n5. Round-trip format conversion:");
117
118 let input_file = "test_utf8.json";
120 let output_file = "converted_utf16.json";
121
122 if Path::new(input_file).exists() {
123 match read_file_to_string(input_file) {
124 Ok(content) => {
125 match write_file_from_string(output_file, &content, Format::Utf16le) {
126 Ok(_) => {
127 println!(
128 " β Converted {} (UTF-8) to {} (UTF-16 LE)",
129 input_file, output_file
130 );
131
132 if let Ok(read_back) = read_file_to_string(output_file) {
134 if content == read_back {
135 println!(" β Round-trip verification successful");
136 } else {
137 println!(" β Round-trip verification failed - content mismatch");
138 }
139 }
140 }
141 Err(e) => println!(" β Conversion failed: {}", e),
142 }
143 }
144 Err(e) => println!(" β Failed to read input: {}", e),
145 }
146 }
147
148 println!("\n6. Using FileSource/FileDestination:");
150
151 let unicode_test_file = "unicode_test.json";
152
153 let mut unicode_data = HashMap::new();
155 unicode_data.insert("ascii".to_string(), Node::from("Hello World"));
156 unicode_data.insert("japanese".to_string(), Node::from("γγγ«γ‘γ―"));
157 unicode_data.insert("arabic".to_string(), Node::from("Ω
Ψ±ΨΨ¨Ψ§"));
158 unicode_data.insert("emoji".to_string(), Node::from("π π π"));
159 unicode_data.insert("symbols".to_string(), Node::from("β β« β β β"));
160
161 let unicode_node = Node::Object(unicode_data);
162
163 match FileDestination::new(unicode_test_file) {
165 Ok(mut dest) => {
166 if let Err(e) = stringify(&unicode_node, &mut dest) {
167 println!(" β Failed to write: {}", e);
168 } else {
169 println!(" β Wrote {} using FileDestination", unicode_test_file);
170 }
171 }
172 Err(e) => println!(" β Failed to create destination: {}", e),
173 }
174
175 if Path::new(unicode_test_file).exists() {
177 match FileSource::new(unicode_test_file) {
178 Ok(mut source) => match parse(&mut source) {
179 Ok(node) => {
180 println!(" β Read {} using FileSource", unicode_test_file);
181
182 if let Some(obj) = node.as_object() {
183 println!(" Fields found: {}", obj.len());
184 for key in obj.keys() {
185 println!(" - {}", key);
186 }
187 }
188 }
189 Err(e) => println!(" β Parse error: {}", e),
190 },
191 Err(e) => println!(" β Failed to open source: {}", e),
192 }
193 }
194
195 println!("\n7. Error handling:");
197
198 match detect_format("nonexistent.json") {
200 Ok(_) => println!(" Unexpected success"),
201 Err(_) => println!(" β Properly handled non-existent file error"),
202 }
203
204 match read_file_to_string("nonexistent.json") {
206 Ok(_) => println!(" Unexpected success"),
207 Err(_) => println!(" β Properly handled read error"),
208 }
209
210 println!("\n8. Unicode format information:");
212 println!(" UTF-8 (no BOM): Variable-length encoding (1-4 bytes), most common");
213 println!(" UTF-8 with BOM: Starts with EF BB BF");
214 println!(" UTF-16 LE: 16-bit little-endian, starts with FF FE");
215 println!(" UTF-16 BE: 16-bit big-endian, starts with FE FF");
216 println!(" UTF-32 LE: 32-bit little-endian, starts with FF FE 00 00");
217 println!(" UTF-32 BE: 32-bit big-endian, starts with 00 00 FE FF");
218
219 println!("\n9. Cleanup:");
221 for (filename, _) in &test_files {
222 if Path::new(filename).exists() {
223 if let Err(e) = std::fs::remove_file(filename) {
224 println!(" Warning: Could not remove {}: {}", filename, e);
225 }
226 }
227 }
228
229 let cleanup_files = vec!["converted_utf16.json", "unicode_test.json"];
230
231 for filename in &cleanup_files {
232 if Path::new(filename).exists() {
233 if let Err(e) = std::fs::remove_file(filename) {
234 println!(" Warning: Could not remove {}: {}", filename, e);
235 }
236 }
237 }
238
239 println!(" β Cleaned up test files");
240
241 println!("\n=== Demo Complete ===");
242}