wtfm_rs_serde/
lib.rs

1#![doc(html_playground_url = "https://play.rust-lang.org/")]
2//! # Introduction
3//! This is the serde version of <https://wtfm-rs.github.io/doc/wtfm_rs/>.
4//! ## Why do we need a separate repo?
5//! A separate repo allows a minimum `Cargo.toml` to only pulls in the basic
6//! `serde` and `serde_json` crates so that we can write tests and
7//! deep dive into the dependencies.
8//!
9//! ```toml
10//! [package]
11//! name = "wtfm-rs-tokio"
12//! version = "0.1.0"
13//! edition = "2024"
14//!
15//! [dependencies]
16//! serde = "1.0.228"
17//! serde_json = "1.0.149"
18//! ```
19//! ```text
20//! ├── serde v1.0.228
21//! │   └── serde_core v1.0.228
22//! └── serde_json v1.0.149
23//!    ├── itoa v1.0.17
24//!    ├── memchr v2.7.6
25//!    ├── serde_core v1.0.228
26//!    └── zmij v1.0.19
27//! ```
28//! ## Deserialize untyped data with [`from_str`]
29//! [`from_str`]: <../serde_json/fn.from_str.html>
30//! ```
31//! let data = r#"
32//!     {
33//!         "name": "John Doe",
34//!         "age": 43,
35//!         "phones": [
36//!             "+44 1234567",
37//!             "+44 2345678"
38//!         ]
39//!     }"#;
40//!
41//! let v: serde_json::Value = serde_json::from_str(data).expect("Valid data");
42//! assert_eq!(format!("{}", v), "{\"age\":43,\"name\":\"John Doe\",\"phones\":[\"+44 1234567\",\"+44 2345678\"]}");
43//! ```
44//! ## [`json!`] macro
45//! [`json!`]: <../serde_json/macro.json.html>
46//! ```
47//! let john = serde_json::json!({
48//!     "name": "John Doe",
49//!     "age": 43,
50//!     "phones": [
51//!         "+44 1234567",
52//!         "+44 2345678"
53//!     ]
54//! });
55//!
56//! assert_eq!(format!("{}", john.to_string()), "{\"age\":43,\"name\":\"John Doe\",\"phones\":[\"+44 1234567\",\"+44 2345678\"]}");
57//! ```
58
59#[cfg(test)]
60#[test]
61fn test_from_str() {
62    let data = r#"
63    {
64        "name": "John Doe",
65        "age": 43,
66        "phones": [
67            "+44 1234567",
68            "+44 2345678"
69        ]
70    }"#;
71
72    let v: serde_json::Value = serde_json::from_str(data).expect("Valid data");
73    assert_eq!(
74        format!("{}", v),
75        "{\"age\":43,\"name\":\"John Doe\",\"phones\":[\"+44 1234567\",\"+44 2345678\"]}"
76    );
77}
78
79#[test]
80fn test_json_macro() {
81    let john = serde_json::json!({
82        "name": "John Doe",
83        "age": 43,
84        "phones": [
85            "+44 1234567",
86            "+44 2345678"
87        ]
88    });
89
90    assert_eq!(
91        format!("{}", john.to_string()),
92        "{\"age\":43,\"name\":\"John Doe\",\"phones\":[\"+44 1234567\",\"+44 2345678\"]}"
93    );
94}