rumtk_core/
json.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D.
5 * Copyright (C) 2025  MedicalMasses L.L.C.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22pub mod serialization {
23    pub use crate::types::{RUMDeserialize, RUMDeserializer, RUMSerialize, RUMSerializer};
24    pub use serde_json::{from_str, to_string, to_string_pretty};
25
26    ///
27    /// Serialization macro which will take an object instance decorated with [Serialize] trait
28    /// from serde and return the JSON string representation.
29    ///
30    /// You can pass up to two parameters. The first parameter is the serializable object instance.
31    /// The second parameter is a boolean indicating whether to pretty print. Omit the second
32    /// parameter if not debugging to save on bytes transferred around.
33    ///
34    /// # Examples
35    /// ## Pretty Print
36    /// ```
37    /// use rumtk_core::json::serialization::{RUMSerialize};
38    /// use rumtk_core::strings::RUMString;
39    /// use rumtk_core::rumtk_serialize;
40    ///
41    /// #[derive(RUMSerialize)]
42    /// struct MyStruct {
43    ///     hello: RUMString
44    /// }
45    ///
46    /// let hw = MyStruct{hello: RUMString::from("World")};
47    /// let hw_str = rumtk_serialize!(&hw, true).unwrap();
48    ///
49    /// assert!(hw_str.len() > 0, "Empty JSON string generated from the test struct!");
50    ///
51    /// ```
52    ///
53    /// ## Default
54    /// ```
55    /// use rumtk_core::json::serialization::{RUMSerialize};
56    /// use rumtk_core::strings::RUMString;
57    /// use rumtk_core::rumtk_serialize;
58    ///
59    /// #[derive(RUMSerialize)]
60    /// struct MyStruct {
61    ///     hello: RUMString
62    /// }
63    ///
64    /// let hw = MyStruct{hello: RUMString::from("World")};
65    /// let hw_str = rumtk_serialize!(&hw).unwrap();
66    ///
67    /// assert!(hw_str.len() > 0, "Empty JSON string generated from the test struct!");
68    ///
69    /// ```
70    ///
71    #[macro_export]
72    macro_rules! rumtk_serialize {
73        ( $object:expr ) => {{
74            use $crate::json::serialization::{to_string, to_string_pretty};
75            use $crate::strings::rumtk_format;
76
77            match to_string(&$object) {
78                Ok(s) => Ok(s),
79                Err(e) => Err(rumtk_format!("Failed to serialize object because of {}", e)),
80            }
81        }};
82        ( $object:expr, $pretty:expr ) => {{
83            use $crate::json::serialization::{to_string, to_string_pretty};
84            use $crate::strings::rumtk_format;
85
86            match $pretty {
87                true => match to_string_pretty(&$object) {
88                    Ok(s) => Ok(s),
89                    Err(e) => Err(rumtk_format!("Failed to serialize object because of {}", e)),
90                },
91                false => match to_string(&$object) {
92                    Ok(s) => Ok(s),
93                    Err(e) => Err(rumtk_format!("Failed to serialize object because of {}", e)),
94                },
95            }
96        }};
97    }
98
99    ///
100    /// Deserialization macro which will take a JSON string representation and return an instance
101    /// of the specified type.
102    ///
103    /// Pass the json string to deserialize. You will need to specify the expected type that will
104    /// be generated.
105    ///
106    /// # Example
107    ///
108    /// ```
109    /// use rumtk_core::json::serialization::{RUMSerialize, RUMDeserialize};
110    /// use rumtk_core::strings::RUMString;
111    /// use rumtk_core::{rumtk_serialize, rumtk_deserialize};
112    ///
113    /// #[derive(RUMSerialize, RUMDeserialize, PartialEq)]
114    /// struct MyStruct {
115    ///     hello: RUMString
116    /// }
117    ///
118    /// let hw = MyStruct{hello: RUMString::from("World")};
119    /// let hw_str = rumtk_serialize!(&hw, true).unwrap();
120    /// let new_hw: MyStruct = rumtk_deserialize!(&hw_str).unwrap();
121    ///
122    /// assert!(
123    ///    new_hw == hw,
124    ///    "Deserialized JSON does not match the expected value!"
125    /// );
126    ///
127    /// ```
128    ///
129    #[macro_export]
130    macro_rules! rumtk_deserialize {
131        ( $string:expr ) => {{
132            use $crate::json::serialization::from_str;
133
134            from_str(&$string)
135        }};
136    }
137}