json
json copied to clipboard
In situ parsing
pub fn from_mut_str<'a, T>(s: &'a mut str) -> Result<T>
where
T: Deserialize<'a>;
Strings with escape sequences are decoded in place!
// before
"{\"msg\":\"abc\\nxyz\"}"
// after
"{\"msg\":\"abc\nxyzz\"}"
^^^^^^^^ output &str
&[u8], &[Option<u8>], and &[u16] can be decoded in place as well, since every element of a JSON array takes up at least two bytes. With some effort this can apply to tuples and structs too.
I can imagine doing something clever to represent maps, but that might be going a bit too far.
What would happen to the end of the string here? Because the result is shorter than the actual buffer.
Would the input itself have any guarantees?
There are no guarantees around what modification is made to the input string. In the example above, b'z' happens to be duplicated to account for the difference in length.
Oh, I see. I missed the single b'z' so it looked like the string was shortened, which didn't seem right.
This would be pretty cool. Does anyone have an implementation of this in-place (and no_std) unescaping? I can't find one, but maybe my google-fu just isn't good enough.
EDIT: Got a prototype working. This was much easier than I thought, just needed some time to think on it.