sonic-rs icon indicating copy to clipboard operation
sonic-rs copied to clipboard

support zero-copy for parsing json from Bytes or FastStr into FastStr

Open liuq19 opened this issue 2 years ago • 1 comments

liuq19 avatar Oct 19 '23 12:10 liuq19

Hi, any updates on this?

I’m running into this in a streaming SSE JSON scenario where I want to extract unescaped string content from bytes without additional copying. Right now I have to do manual bounds checks like this:

fn process_line(line: Bytes) -> Option<Bytes> {
    let json = line.strip_prefix(b"data: ")?;
    if json == b"[DONE]" || json.is_empty() {
        return None;
    }
    let path = pointer!["choices", 0, "delta", "content"];
    let content = unsafe { get_unchecked(json, &path) };
    content.as_str().map(|s| {
        let start_ptr = line.as_ptr() as usize;
        let end_ptr = start_ptr + line.len();
        let s_ptr = s.as_ptr() as usize;

        if s_ptr >= start_ptr && s_ptr + s.len() <= end_ptr {
            let offset = s_ptr - start_ptr;
            line.slice(offset..offset + s.len())
        } else {
            Bytes::copy_from_slice(s.as_bytes())
        }
    })
}

It’d be nice if sonic-rs could expose an API that looks something like this:

fn as_cow_str(&self) -> std::borrow::Cow<'_, str>

dimaestroo avatar Aug 21 '25 09:08 dimaestroo