gpt4 book ai didi

json - 东京和塞尔德 : deserializing JSON

转载 作者:行者123 更新时间:2023-12-03 11:29:46 26 4
gpt4 key购买 nike

在尝试从 tokio TcpStream 反序列化为 JSON Value 时,我正在尝试使用此函数:

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);
}

我的依赖版本是:

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"

我从 examples from carllerche/tokio-serde-json 得到代码

但是我得到以下错误:

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`

这是 API 和推荐的库已经发展并转向另一种做事方式的情况吗?我现在如何使用 Tokio 将 JSON 反序列化为 Value

最佳答案

似乎这个存储库被另一个存储库所取代:

之前:tokio-serde-json

之后:tokio-serde

https://github.com/carllerche/tokio-serde/blob/master/examples/server.rs

我用过这个服务器代码:

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);
}
});
}
}

具有以下依赖性:

[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"] }

编译运行成功

关于json - 东京和塞尔德 : deserializing JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63644976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com