Skip to main content

rust_web_server/json/
mod.rs

1//! A dependency-free JSON parser and serializer.
2//!
3//! You describe a struct and implement [`New`](crate::core::New),
4//! [`FromJSON`](object::FromJSON) and [`ToJSON`](object::ToJSON) for it; the trait bodies call the module's helpers
5//! for each property. Nested objects and arrays are supported. It is not a
6//! validator or formatter, but invalid input is an error, not a panic.
7//! Submodules: [`object`], [`array`], [`property`]; examples live in their
8//! `tests`/`example_*` modules (objects, nested objects, arrays of every scalar
9//! type, arrays of objects).
10//!
11//! With the `serde` feature, `Json<T>` extracts a `Deserialize` type
12//! from a request body and turns a `Serialize` type into an `application/json`
13//! response — use that unless you want the explicit, macro-free mapping.
14//!
15//! # FAQ
16//!
17//! - **Line endings.** The parser doesn't care about `\n` vs `\r\n`;
18//!   `to_json_string` writes `\r\n`, so compare against `\r\n` text in tests.
19//! - **No pretty-printing.** `to_json_string` adds line breaks but doesn't indent
20//!   nested values.
21//! - **Why `New`?** Parsing an array has to create instances of your struct.
22//! - **Mixed-type arrays** (`[1, true, "x"]`, tuples) don't map to one struct
23//!   type; model them as an array of objects, or split the raw array with
24//!   `RawUnprocessedJSONArray::split_into_vector_of_strings` and parse each item.
25//! - **Numbers** are `i128` and `f64` (JSON has one number type); cast in your
26//!   `FromJSON` if you need smaller types. `f64` has the usual binary rounding
27//!   (`4356.257` reads back as `4356.2569999999996`) — store money as integer
28//!   cents, or round when formatting.
29//! - **Enums** have no JSON form: keep a string/integer property and map it to
30//!   your enum in `FromJSON`/`ToJSON`.
31
32#[cfg(test)]
33mod tests;
34pub mod property;
35pub mod array;
36pub mod object;
37
38#[cfg(feature = "serde")]
39mod extractor;
40#[cfg(feature = "serde")]
41pub use extractor::Json;
42
43// TODO: wip
44
45pub struct JSONType {
46    pub string: &'static str,
47    pub boolean: &'static str,
48    pub object: &'static str,
49    pub array: &'static str,
50    pub integer: &'static str,
51    pub number: &'static str,
52    pub null: &'static str,
53}
54
55pub const JSON_TYPE: JSONType = JSONType{
56    string: "String",
57    boolean: "bool",
58    object: "object",
59    array: "array",
60    integer: "i128",
61    number: "f64",
62    null: "null",
63};
64
65
66
67
68
69
70
71