Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

JSON string with backslashes does not deserialize into borrowed &str #1746

Closed
Noelle-Dev opened this issue Mar 5, 2020 · 1 comment
Closed

Comments

@Noelle-Dev
Copy link

Hi all, as a personal project, I am trying to develop a tool for a game that I've been playing called Path of Exile. The tool is intended to scrape market data and display currency trade prices. I am a C++ developer by nature but I want to learn a new language and chose Rust for this project.

The first step is to scrape item information to build a list of available items. I've done it for the most part, with one exception. So here is an example of the json object that I want to deserialize:
{ "id": "exa", "text": "Exalted Orb", "image": "\/image\/Art\/2DItems\/Currency\/CurrencyAddModToRare.png?v=1745ebafbd533b6f91bccf588ab5efc5" }

I am having trouble deserializing the image value. I am trying to store it into a &str.
It seems like serde is not able to handle escape characters. Can anyone help me with this issue?

@jonasbb
Copy link
Contributor

jonasbb commented Mar 5, 2020

The problem here is that due to the escape sequences, serde_json cannot simply hand out a reference to the original string, since the escape sequences need to be removed while deserializing. You can deserialize this into a String or a Cow<'_, str>. You can also apply #[serde(borrow)] to the latter to get zero copy deserialization when possible.

If it has to be zero copy deserialization you can look into in situ parsing for `serde_json. There is an issue and a pull request for it.

@dtolnay dtolnay changed the title Deserializing String with Backslashes :( JSON string with backslashes does not deserialize into borrowed &str Jul 5, 2020
@dtolnay dtolnay closed this as completed Jul 5, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

No branches or pull requests

3 participants