Skip to content

Cannot deserialize into reference if there are escaped quotation marks in string #742

Closed
@arlyon

Description

@arlyon

Hi,

Sorry if this issue is known, I'm not sure how to phrase it. I am dealing with json strings, which I am parsing into structs that store string slices into the original string.

use serde::{Deserialize, Serialize};

const data: &str = r#"
[{
    "id": 27,
    "name": "grizzlemaw bear",
    "description": "This breed of honeyvore bear is distinguished by its gray fur. They live in deep snow away from villages, so it's uncommon to encounter one."
},
{
    "id": 28,
    "name": "hylian retriever",
    "description": "The native breed of this mammal varies by region, but one thing remains true: this animals has been known as \"man's best friend\" since ancient times."
}]
"#;

#[derive(Serialize, Deserialize, Debug)]
struct Creature<'a> {
    id: u32,
    name: &'a str,
    description: &'a str,
}

fn main() {
    let creatures: Vec<Creature> = serde_json::from_str(data).unwrap();
}

Stangely, serde is unable to parse the data if any of these string slices have escaped quotes in them. In the case of the second list element, I receive an error:

    Finished dev [unoptimized + debuginfo] target(s) in 0.78s
     Running `target/debug/botw-rust`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("invalid type: string \"The native breed of this mammal varies by region, but one thing remains true: this animals has been known as \\\"man\\\'s best friend\\\" since ancient times. They\\\'re very clever and obedient, so aside from serving as pets, they are also put to work watching over grazing livestock. It\\\'s said that all Hylian retrievers are descendants of the dog once owned by the king of Hyrule.\", expected a borrowed string", line: 308, column: 398)', src/main.rs:57:94
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Seems that, for whatever reason, it is not able to be borrowed if there is a quotation mark. If I am missing something, please let me know. If there is suspicion that this is a genuine bug I will happily write a minimal reproducing example.

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