-
-
Notifications
You must be signed in to change notification settings - Fork 785
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
Allow deserializing without consuming the entire stream #89
Comments
Hello @shaladdle! You should be able to directly use |
Thanks for the response! Sorry for taking a while to reply. I was able to get some time to try again with your suggestion, but I still get the same behavior. The receiver doesn't finish deserialization unless the sender closes the connection. Here's an example: #![feature(custom_derive, plugin)]
#![plugin(serde_macros)]
extern crate serde;
use std::net;
use std::io::Read;
use std::thread;
use serde::Deserialize;
#[derive(Serialize, Deserialize, Debug)]
struct Request {
message: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Response {
message: String,
}
fn main() {
let l = net::TcpListener::bind("localhost:20000").unwrap();
thread::spawn(||{
let l = l;
for stream in l.incoming() {
let mut stream = stream.unwrap();
let read_stream = stream.try_clone().unwrap();
let mut de = serde::json::Deserializer::new(read_stream.bytes()).unwrap();
println!("deserializing");
let request = Request::deserialize(&mut de).unwrap();
println!("deserialized");
let response = Response{message: request.message};
serde::json::to_writer(&mut stream, &response).unwrap();
}
});
let mut stream = net::TcpStream::connect("localhost:20000").unwrap();
let request = Request{message: "hi there".to_string()};
serde::json::to_writer(&mut stream, &request).unwrap();
println!("message sent");
let mut de = serde::json::Deserializer::new(stream.bytes()).unwrap();
let response = Response::deserialize(&mut de).unwrap();
println!("response: {:?}", response);
} I would expect to see something like the following for output:
but it hangs after "deserializing". |
There was a strange behavior in Try to send over your TCP socket something like this: |
Ah right. Some of the JSON constructs require looking one character ahead, which could trigger this. Maybe we're doing an unnecessary read? This will require some investigation. |
Ok what's going on is that when we parse the end of an object, we call It shouldn't be that hard to rewrite the parser to only fetch the next character when it actually needs it, but we'll have to be careful it doesn't impact performance. |
traits.rs: Implement Bounded for tuples of Bounded types
Sorry for bumping this old issue, but I'm getting the exact same behaviour described on top: |
I think this is the correct behavior for from_reader. For example when deserializing from a File using from_reader, we don't want to silently allow trailing garbage. To deserialize from a prefix of a stream you can manage your own Deserializer: let mut de = serde_json::Deserializer::from_reader(stream);
let t = T::deserialize(&mut de)?; |
Thanks, very well explained! Would be nice if it was mentioned in the docs for from_reader because hanging indefinitely is misleading to debug. |
Good call, I filed serde-rs/json#522 to follow up. |
Thanks! |
My use case for this is deserializing more than one json object from a tcp connection. Currently you can't open one connection and send objects back and forth because
serde::json::de::from_reader
hangs until eof.The text was updated successfully, but these errors were encountered: