yew_stdweb/format/
json.rs

1//! Contains an implementation of JSON serialization format.
2
3/// A representation of a JSON data. Use it as wrapper to
4/// set a format you want to use for conversion:
5///
6/// ```
7/// // Converts (lazy) data to a Json
8/// use yew::format::Json;
9/// let data: String = r#"{lorem: "ipsum"}"#.to_string();
10/// let dump = Json(&data);
11///
12/// // Converts JSON string to a data (lazy).
13/// let Json(data) = dump;
14/// ```
15#[derive(Debug)]
16pub struct Json<T>(pub T);
17
18text_format!(Json based on serde_json);
19
20binary_format!(Json based on serde_json);
21
22#[cfg(test)]
23mod tests {
24    use super::*;
25    use crate::format::{Binary, Text};
26    use serde::{Deserialize, Serialize};
27    #[cfg(feature = "wasm_test")]
28    use wasm_bindgen_test::wasm_bindgen_test as test;
29
30    #[test]
31    fn json_format() {
32        #[derive(Serialize, Deserialize)]
33        struct Data {
34            value: u8,
35        }
36
37        let Json(data): Json<Result<Data, _>> = Json::from(Ok(r#"{"value": 123}"#.to_string()));
38        let data = data.unwrap();
39        assert_eq!(data.value, 123);
40
41        let _stored: Text = Json(&data).into();
42        let _stored: Binary = Json(&data).into();
43    }
44}