1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! This module provides utilities for working with JSON data.
pub use json;
pub use to_string as struct_to_string;
pub use Result as JsonResult;
pub use Value as JsonValue;
/// Converts a `JsonValue` (`serde_json::Value`) to a string
///
/// # Example
///
/// ```rust
/// use krustie::{ Request, Response, StatusCode, request::RequestBody, json::{ get_string_from_json, json } };
///
/// fn post_req(req: &Request, res: &mut Response) {
/// match req.get_body() {
/// RequestBody::Json(body) => {
/// let server_key_option = body.get("server");
///
/// match get_string_from_json(server_key_option).unwrap().as_str() {
/// "Krustie" => {
/// res.status(StatusCode::Ok).body_json(body.clone());
/// },
/// _ => {
/// res.status(StatusCode::try_from(201).unwrap()).body_json(
/// json!({"error": "Invalid server"})
/// );
/// }
/// }
/// },
/// _ => {
/// res.status(StatusCode::BadRequest).body_json(json!({"error": "Invalid JSON"}));
/// }
/// }
/// }