Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[question] How to deserialize invalid boolean? #616

Closed
vimpunk opened this issue Feb 4, 2020 · 2 comments
Closed

[question] How to deserialize invalid boolean? #616

vimpunk opened this issue Feb 4, 2020 · 2 comments

Comments

@vimpunk
Copy link

vimpunk commented Feb 4, 2020

I'm integrating against a third-party backend and unfortunately I'm faced with the following invalid JSON:

{
  "value": True
}

Using this answer, I tried implementing a function for the deserialize_with field attribute with the various visitor APIs (e.g. an obviously failed attempt on the Rust playground), but as far as I can tell, the problem is that the input is not recognized as any correct type, so none of the visitor APIs work here.

Are there any possible workarounds here that do not require implementing an entirely new Deserializer for this invalid JSON? Or am I better off manually parsing the input string? Alternatively, is there a way to implement a Deserializer that only parses the invalid JSON and forwards everything else to the Deserializer in serde_json?

Any insight is much appreciated.

@vimpunk vimpunk changed the title [question] How to deserialize invalid JSON? [question] How to deserialize invalid boolean? Feb 4, 2020
@dtolnay
Copy link
Member

dtolnay commented Feb 4, 2020

We don't support deserializing syntactically invalid JSON. If you need to deserialize something that is not JSON, you should do it with something other than serde_json.

Some people have forked serde_json to add nonstandard features, so you could do the same and modify it to accept True.

@dtolnay dtolnay closed this as completed Feb 5, 2020
@vimpunk
Copy link
Author

vimpunk commented Feb 5, 2020

Thanks, that works, although is more effort than my use case warrants.

A hacky solution, for anyone else having similar issues is to string replace True and False instances in the raw response string to true and false. Not perfect, since if a string contains these those will be replaced, but is perhaps an acceptable solution for a limited usecase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants