Skip to main content

json_unicode_files/
json_unicode_files.rs

1//! Demonstrates Unicode file handling with BOM detection
2//!
3//! Shows how to read and write JSON files in different Unicode formats
4//! (UTF-8, UTF-16, UTF-32) with proper BOM (Byte Order Mark) handling.
5
6use 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    // Example 1: Creating sample JSON data
17    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    // Example 2: Writing files in different Unicode formats
34    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    // Example 3: Detecting file format
72    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    // Example 4: Reading files with automatic format detection
94    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    // Example 5: Round-trip conversion
116    println!("\n5. Round-trip format conversion:");
117
118    // Read from UTF-8, write as UTF-16
119    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                        // Verify by reading back
133                        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    // Example 6: Using FileSource and FileDestination with Unicode
149    println!("\n6. Using FileSource/FileDestination:");
150
151    let unicode_test_file = "unicode_test.json";
152
153    // Create a JSON with various Unicode characters
154    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    // Write using FileDestination
164    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    // Read using FileSource
176    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    // Example 7: Handling errors gracefully
196    println!("\n7. Error handling:");
197
198    // Try to detect format of non-existent file
199    match detect_format("nonexistent.json") {
200        Ok(_) => println!("  Unexpected success"),
201        Err(_) => println!("  βœ“ Properly handled non-existent file error"),
202    }
203
204    // Try to read non-existent file
205    match read_file_to_string("nonexistent.json") {
206        Ok(_) => println!("  Unexpected success"),
207        Err(_) => println!("  βœ“ Properly handled read error"),
208    }
209
210    // Example 8: Format information
211    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    // Cleanup - remove test files
220    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}