Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ pub fn from_str<'input, 'facet, T: Facet<'facet>>(
where
'input: 'facet,
{
from_slice(input.as_bytes())
let input_bytes = input.as_bytes();

if input_bytes.starts_with(&[0xef, 0xbb, 0xbf]) {
return from_slice(&input_bytes[3..]);
}
from_slice(input_bytes)
}

impl Format for crate::Json {
Expand Down
22 changes: 22 additions & 0 deletions tests/bom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use facet::Facet;
use facet_json::from_str;
use facet_testhelpers::test;

#[test]
fn test_basic_with_bom() {
#[derive(Debug, Facet)]
struct HelloBom {
hello: &'static str,
}
let json = "\u{feff}{\"hello\": \"hi!\"}";

let result = from_str::<HelloBom>(json);
match result {
Ok(data) => {
assert_eq!(data.hello, "hi!");
}
Err(e) => {
panic!("Failed to parse JSON: {e}");
}
}
}