Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions picojson/examples/test_surrogate_pairs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use picojson::{Event, PullParser, SliceParser};

fn main() {
// Test the original failing case: ["\uD801\udc37"]
let json = r#"["\uD801\udc37"]"#;
let mut buffer = [0u8; 1024];
let mut parser = SliceParser::with_buffer(json, &mut buffer);

let mut string_found = false;
let mut string_bytes = Vec::new();

loop {
match parser.next_event() {
Ok(Event::EndDocument) => break,
Ok(Event::String(s)) => {
string_found = true;
string_bytes = s.as_bytes().to_vec();
println!("Found string: {:?}", s);
println!("String content as bytes: {:?}", string_bytes);
}
Ok(event) => {
println!("Event: {:?}", event);
}
Err(e) => panic!("Parser error: {:?}", e),
}
}

println!("Successfully parsed surrogate pair JSON");

// The string should be decoded to U+10437 which is encoded as UTF-8: [0xF0, 0x90, 0x90, 0xB7]
if string_found {
assert_eq!(
string_bytes,
vec![0xF0, 0x90, 0x90, 0xB7],
"Surrogate pair should decode to U+10437"
);
println!("✅ Surrogate pair correctly decoded!");
} else {
panic!("Expected String event not found");
}

// Test additional surrogate pairs
let test_cases = vec![
(r#"["\uD834\uDD1E"]"#, "Musical G clef symbol"),
(r#"["\uD83D\uDE00"]"#, "Grinning face emoji"),
];

for (json, description) in test_cases {
println!("\nTesting {}: {}", description, json);
let mut buffer = [0u8; 1024];
let mut parser = SliceParser::with_buffer(json, &mut buffer);

let mut success = false;
loop {
match parser.next_event() {
Ok(Event::EndDocument) => break,
Ok(Event::String(_)) => success = true,
Ok(_) => {}
Err(e) => panic!("Parser error for {}: {:?}", description, e),
}
}

if success {
println!("✅ Successfully parsed {}", description);
} else {
panic!("Failed to find string in {}", description);
}
}
}
Loading