From 1a634a983f3c12405bc612db4addea74a69419ae Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Tue, 18 Jul 2023 17:34:26 +0100 Subject: [PATCH] remove `json`crate --- Cargo.lock | 7 ------- json/json/Cargo.toml | 2 -- json/json/src/main.rs | 33 ++------------------------------- 3 files changed, 2 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2dfba1fa..b833f8cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/json/json/Cargo.toml b/json/json/Cargo.toml index 629264a8..d7a9950c 100644 --- a/json/json/Cargo.toml +++ b/json/json/Cargo.toml @@ -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 diff --git a/json/json/src/main.rs b/json/json/src/main.rs index 3cd9fdcc..92eee15d 100644 --- a/json/json/src/main.rs +++ b/json/json/src/main.rs @@ -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, 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 { - // 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 { // body is loaded, now we can deserialize serde-json let obj = serde_json::from_slice::(&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 { - // 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))?