-
Notifications
You must be signed in to change notification settings - Fork 571
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
Ability to step past errors in StreamDeserializer if input is valid JSON #70
Comments
The desired behavior: let data = b"[0] {"k":"v"} [1]";
let de = serde_json::Deserializer::from_slice(data);
let mut stream = de.into_iter::<Vec<i32>>();
assert_eq!(0, stream.byte_offset());
println!("{:?}", stream.next()); // [0]
assert_eq!(3, stream.byte_offset());
println!("{:?}", stream.next()); // type error
assert_eq!(13, stream.byte_offset());
println!("{:?}", stream.next()); // [1]
assert_eq!(17, stream.byte_offset()); |
Has there been any progress towards implementing the change mentioned in the
This is the behavior I was expecting from |
While this could still be useful to implement, I'll describe my workaround as well which may be a better solution. Rather than using the |
An alternative is to move the This way the user's loop can be to first try This gives the user precise control of how they want to handle the intermediate error. But of course the con is that this means removing the Iterator impl, and thus losing all the combinators and such that users can use right now. |
If someone is looking for a workaround, the solution from @H2CO3 on the Rust users forum helped me. It steps past an item in the stream even if it is invalid JSON. |
The current behavior of StreamDeserializer is that if it hits an error, that same error is returned in perpetuity which is typically not what you want. (If that is what you want, it is easy to implement that behavior atop what I propose below.)
Suppose we have a stream deserializer that expects to read a stream of Strings. If it receives the input
"a" {"b": 0} "c"
, a reasonable thing to expect as output would be"a" (error) "c"
because the second JSON object failed to deserialize to a String.On the other hand, if the input is not a valid JSON stream I believe the StreamDeserializer should retain its current behavior because there is no way for it to regain its bearings in an invalid JSON stream. For example if the input is
"a" {"b{{"c" "d"
, I would expect the output to be"a" (error) (error) (error...)
.The text was updated successfully, but these errors were encountered: