Skip to content

Deserializing to std::borrow::Cow<str> always allocates #1852

@Vlad-Shcherbina

Description

@Vlad-Shcherbina
// [dependencies]
// serde_json = "1.0.41"
fn main() {
    let v = "hello";
    let s = serde_json::to_string(&v).unwrap();
    println!("{}", s);
    let v2: std::borrow::Cow<str> = serde_json::from_str(&s).unwrap();
    match v2 {
        std::borrow::Cow::Borrowed(..) => println!("borrowed"),
        std::borrow::Cow::Owned(..) => println!("owned"),
    }
}

Expected result

"hello"
borrowed

Because string hello appears in the data as is, with no escaping or reader buffer boundaries.

Actual result

"hello"
owned

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions