Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: How to parse an escaped JSON ascii string while keeping it's binary representation?
I 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!?
3 posts - 2 participants
🏷️ rust_feed