Reputation: 4444
While trying to deserialize into a JSON Value
from a tokio TcpStream
, I'm trying to use this function:
use futures::prelude::*;
use serde_json::Value;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::stream::StreamExt;
use tokio_serde_json::ReadJson;
use tokio_util::codec::{FramedRead, LengthDelimitedCodec};
pub async fn handle_stream(mut stream: TcpStream) {
let length_delimited = FramedRead::new(stream, LengthDelimitedCodec::new());
let mut deserialized = ReadJson::<_, Value>::new(length_delimited);
}
My dependency versions are:
tokio = { version = "0.2.22", features=["full"] }
tokio-util = { version = "0.3.0", features=["full"] }
tokio-serde = "0.6"
serde = "1.0.92"
serde_derive = "1.0.92"
serde_json = "1.0.39"
futures-preview = "0.3.0-alpha"
I got the code from the examples from carllerche/tokio-serde-json
However I get the following error:
error[E0277]: the trait bound `tokio_util::codec::framed_read::FramedRead<tokio::net::tcp::stream::TcpStream, tokio_util::codec::length_delimited::LengthDelimitedCodec>: futures::stream::Stream` is not satisfied
--> src/kad2.rs:141:58
|
141 | let mut deserialized = ReadJson::<_, Value>::new(length_delimited);
| ^^^^^^^^^^^^^^^^ the trait `futures::stream::Stream` is not implemented for `tokio_util::codec::framed_read::FramedRead<tokio::net::tcp::stream::TcpStream, tokio_util::codec::length_delimited::LengthDelimitedCodec>`
|
= note: required by `tokio_serde_json::ReadJson::<T, U>::new`
Is this a case of APIs and recommended libraries having evolved and moved to another way to do things ?
How would I deserialize JSON into a Value
with Tokio now ?
Upvotes: 4
Views: 5092
Reputation: 480
It seems that this repository is abandoned in favor of another one:
Before: tokio-serde-json
After: tokio-serde
https://github.com/carllerche/tokio-serde/blob/master/examples/server.rs
I've used this server code:
use futures::prelude::*;
use serde_json::Value;
use tokio::net::TcpListener;
use tokio_serde::formats::*;
use tokio_util::codec::{FramedRead, LengthDelimitedCodec};
#[tokio::main]
pub async fn main() {
// Bind a server socket
let listener = TcpListener::bind("127.0.0.1:17653").await.unwrap();
println!("listening on {:?}", listener.local_addr());
loop {
let (socket, _) = listener.accept().await.unwrap();
// Delimit frames using a length header
let length_delimited = FramedRead::new(socket, LengthDelimitedCodec::new());
// Deserialize frames
let mut deserialized = tokio_serde::SymmetricallyFramed::new(
length_delimited,
SymmetricalJson::<Value>::default(),
);
// Spawn a task that prints all received messages to STDOUT
tokio::spawn(async move {
while let Some(msg) = deserialized.try_next().await.unwrap() {
println!("GOT: {:?}", msg);
}
});
}
}
with following dependencies:
[dependencies]
futures = "0.3"
impls = "1"
tokio = { version = "1.0", features = ["full"] }
tokio-util = { version = "0.6", features = ["codec"] }
static_assertions = "1.1.0"
serde_json = "1.0"
tokio-serde = { version = "0.8", features = ["json"] }
and it's compiling and running successfully.
Upvotes: 5