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