mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
remove json
crate
This commit is contained in:
parent
01cb601f67
commit
1a634a983f
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -4116,12 +4116,6 @@ dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "json"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
|
||||
|
||||
[[package]]
|
||||
name = "json-example"
|
||||
version = "1.0.0"
|
||||
@ -4129,7 +4123,6 @@ dependencies = [
|
||||
"actix-web",
|
||||
"env_logger",
|
||||
"futures-util",
|
||||
"json",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -5,10 +5,8 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web.workspace = true
|
||||
|
||||
env_logger.workspace = true
|
||||
futures-util.workspace = true
|
||||
json = "0.12"
|
||||
log.workspace = true
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json.workspace = true
|
||||
|
@ -1,6 +1,4 @@
|
||||
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use futures_util::StreamExt as _;
|
||||
use json::JsonValue;
|
||||
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@ -23,39 +21,13 @@ async fn extract_item(item: web::Json<MyObj>, req: HttpRequest) -> HttpResponse
|
||||
HttpResponse::Ok().json(item.0) // <- send json response
|
||||
}
|
||||
|
||||
const MAX_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
/// This handler manually load request payload and parse json object
|
||||
async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error> {
|
||||
// payload is a stream of Bytes objects
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_SIZE {
|
||||
return Err(error::ErrorBadRequest("overflow"));
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
async fn index_manual(body: web::Bytes) -> Result<HttpResponse, Error> {
|
||||
// body is loaded, now we can deserialize serde-json
|
||||
let obj = serde_json::from_slice::<MyObj>(&body)?;
|
||||
Ok(HttpResponse::Ok().json(obj)) // <- send response
|
||||
}
|
||||
|
||||
/// This handler manually load request payload and parse json-rust
|
||||
async fn index_mjsonrust(body: web::Bytes) -> Result<HttpResponse, Error> {
|
||||
// body is loaded, now we can deserialize json-rust
|
||||
let result = json::parse(std::str::from_utf8(&body).unwrap()); // return Result
|
||||
let injson: JsonValue = match result {
|
||||
Ok(v) => v,
|
||||
Err(e) => json::object! {"err" => e.to_string() },
|
||||
};
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(injson.dump()))
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
@ -74,7 +46,6 @@ async fn main() -> std::io::Result<()> {
|
||||
.route(web::post().to(extract_item)),
|
||||
)
|
||||
.service(web::resource("/manual").route(web::post().to(index_manual)))
|
||||
.service(web::resource("/mjsonrust").route(web::post().to(index_mjsonrust)))
|
||||
.service(web::resource("/").route(web::post().to(index)))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
|
Loading…
Reference in New Issue
Block a user