How to parse an escaped JSON non UTF-8 string while keeping it's binary representation?
⚓ Rust 📅 2025-07-17 👤 surdeus 👁️ 17I want to parse the contents of a "Copy as Fetch" text from the browser which has a format similar to:
await fetch("https://some-url/", {
"headers": {
"Header1": "value1",
"Header2": "value2"
},
"referrer": "https://some-referrer/",
"body": "\b\u0001\u0012Å"
});
If I parse everything except the {...} block and use serde to parse that, given that it's JSON, then body will not maintain the correct byte representation because Strings in Rust are UTF-8: it will be [8, 1, 18, 195, 133] instead of [ 8, 1, 18, 197], because of the different representation of Å in ASCII and UTF-8. However what I need is the same byte representation!
I'd need to interpret the string as ASCII and then unescape it, but AsciiStr does not implement Deserialize and if I use Vec<u8> as the field type serde complains.
This is a [u8] in disguise, so it's arguable whether serde is right or wrong on this, but is there a way to get the underlying bytes without rewritting my own deserializer and unescaper? Is it even possible to write a deserializer that reads the bytes of a json string using serde!?
9 posts - 5 participants
🏷️ rust_feed