| 1 | mod de; |
| 2 | mod ser; |
| 3 | |
| 4 | pub use de::from_any; |
| 5 | pub use ser::to_any; |
| 6 | |
| 7 | #[cfg(test)] |
| 8 | mod test { |
| 9 | use super::*; |
| 10 | use crate::any; |
| 11 | use crate::any::Any; |
| 12 | use serde::{Deserialize, Serialize}; |
| 13 | use serde_json::json; |
| 14 | use std::collections::HashMap; |
| 15 | |
| 16 | fn roundtrip(any: &Any) -> Any { |
| 17 | // encode |
| 18 | let mut buf = String::new(); |
| 19 | any.to_json(&mut buf); |
| 20 | // decode |
| 21 | Any::from_json(buf.as_str()).unwrap() |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn json_any_null() { |
| 26 | let expected = Any::Null; |
| 27 | let actual = roundtrip(&expected); |
| 28 | assert_eq!(actual, expected); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn json_any_bool() { |
| 33 | for v in [true, false] { |
| 34 | let expected = Any::Bool(v); |
| 35 | let actual = roundtrip(&expected); |
| 36 | assert_eq!(actual, expected); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn json_any_number() { |
| 42 | for v in [1.8, -1.5, 0.2, 0.25, 2349464814556456.4] { |
| 43 | let expected = Any::Number(v); |
| 44 | let actual = roundtrip(&expected); |
| 45 | assert_eq!(actual, expected); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn json_any_string() { |
| 51 | for v in ["", "hello ", "hello \nworld", "hello \"world\"", "hello đŸ˜€"] { |
| 52 | let expected = Any::String(v.into()); |
| 53 | let actual = roundtrip(&expected); |
| 54 | assert_eq!(actual, expected); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn json_any_array() { |
| 60 | let expected = Any::from(vec![ |
| 61 | Any::Number(-10.0), |
| 62 | Any::String("hello \\world".into()), |
| 63 | Any::Null, |
| 64 | Any::Bool(true), |
| 65 | ]); |
| 66 | let actual = roundtrip(&expected); |
| 67 | assert_eq!(actual, expected); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn json_any_map() { |
| 72 | let expected = Any::from(HashMap::from([ |
| 73 | ("a".to_string(), Any::Number(-10.2)), |
| 74 | ("b".to_string(), Any::String("hello world".into())), |
| 75 | ("c".to_string(), Any::Null), |
| 76 | ("d".to_string(), Any::Bool(true)), |
| 77 | ("e".to_string(), Any::from(vec![Any::String("abc".into())])), |
| 78 | ( |
| 79 | "f".to_string(), |
| 80 | Any::from(HashMap::from([("fa".to_string(), Any::Number(1.5))])), |
| 81 | ), |
| 82 | ])); |
| 83 | let actual = roundtrip(&expected); |
| 84 | assert_eq!(actual, expected); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn test_struct_any_roundtrip() { |
| 89 | #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
| 90 | struct Test { |
| 91 | bool: bool, |
| 92 | int: i64, |
| 93 | negative_int: i64, |
| 94 | max_int: i64, |
| 95 | min_int: i64, |
| 96 | real_number: f64, |
| 97 | max_number: f64, |
| 98 | min_number: f64, |
| 99 | null: Option<bool>, |
| 100 | some: Option<bool>, |
| 101 | undefined: Option<bool>, |
| 102 | nested: Nested, |
| 103 | enum_a: StringEnum, |
| 104 | enum_b: StringEnum, |
| 105 | enum_c: StringEnum, |
| 106 | array: Vec<Option<Nested>>, |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
| 110 | struct Nested { |
| 111 | int: i16, |
| 112 | other: f32, |
| 113 | } |
| 114 | |
| 115 | #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
| 116 | enum StringEnum { |
| 117 | VariantA, |
| 118 | VariantB(String), |
| 119 | VariantC { test: bool }, |
| 120 | } |
| 121 | |
| 122 | let test = Test { |
| 123 | bool: true, |
| 124 | int: 1, |
| 125 | negative_int: -1, |
| 126 | max_int: i64::MAX, |
| 127 | min_int: i64::MIN, |
| 128 | real_number: -123.2387f64, |
| 129 | max_number: f64::MIN, |
| 130 | min_number: f64::MAX, |
| 131 | null: None, |
| 132 | some: Some(false), |
| 133 | undefined: None, |
| 134 | nested: Nested { |
| 135 | int: 100, |
| 136 | other: 100.0, |
| 137 | }, |
| 138 | enum_a: StringEnum::VariantA, |
| 139 | enum_b: StringEnum::VariantB("test".to_string()), |
| 140 | enum_c: StringEnum::VariantC { test: true }, |
| 141 | array: vec![None, Some(Nested { int: 0, other: 0.0 }), None], |
| 142 | }; |
| 143 | |
| 144 | assert_eq!(test, from_any(&to_any(&test).unwrap()).unwrap()) |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn test_enum_untagged_any_roundtrip() { |
| 149 | #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
| 150 | struct Test { |
| 151 | enum_a: StringEnum, |
| 152 | enum_b: StringEnum, |
| 153 | enum_c: StringEnum, |
| 154 | } |
| 155 | |
| 156 | #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
| 157 | #[serde(untagged)] |
| 158 | enum StringEnum { |
| 159 | VariantA, |
| 160 | VariantB(String), |
| 161 | VariantC { test: bool }, |
| 162 | } |
| 163 | |
| 164 | let test = Test { |
| 165 | enum_a: StringEnum::VariantA, |
| 166 | enum_b: StringEnum::VariantB("test".to_string()), |
| 167 | enum_c: StringEnum::VariantC { test: true }, |
| 168 | }; |
| 169 | |
| 170 | assert_eq!(test, from_any(&to_any(&test).unwrap()).unwrap()) |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn test_any_to_json_roundtrip() { |
| 175 | let any = Any::from(HashMap::from([ |
| 176 | ("bool".into(), Any::from(true)), |
| 177 | ("int".into(), Any::from(1)), |
| 178 | ("negativeInt".into(), Any::from(-1)), |
| 179 | ("maxInt".into(), Any::from(i64::MAX)), |
| 180 | ("minInt".into(), Any::from(i64::MIN)), |
| 181 | ("realNumber".into(), Any::from(-123.2387f64)), |
| 182 | ("maxNumber".into(), Any::from(f64::MIN)), |
| 183 | ("minNumber".into(), Any::from(f64::MAX)), |
| 184 | ("null".into(), Any::Null), |
| 185 | ( |
| 186 | "map".into(), |
| 187 | Any::from(HashMap::from([ |
| 188 | ("bool".into(), Any::from(true)), |
| 189 | ("int".into(), Any::from(1)), |
| 190 | ("negativeInt".into(), Any::from(-1)), |
| 191 | ("maxInt".into(), Any::from(i64::MAX)), |
| 192 | ("minInt".into(), Any::from(i64::MIN)), |
| 193 | ("realNumber".into(), Any::from(-123.2387f64)), |
| 194 | ("maxNumber".into(), Any::from(f64::MIN)), |
| 195 | ("minNumber".into(), Any::from(f64::MAX)), |
| 196 | ("null".into(), Any::Null), |
| 197 | ])), |
| 198 | ), |
| 199 | ( |
| 200 | "key6".into(), |
| 201 | Any::from(vec![ |
| 202 | Any::from(true), |
| 203 | Any::from(1), |
| 204 | Any::from(-1), |
| 205 | Any::from(i64::MAX), |
| 206 | Any::from(i64::MIN), |
| 207 | Any::from(-123.2387f64), |
| 208 | Any::from(f64::MIN), |
| 209 | Any::from(f64::MAX), |
| 210 | Any::Null, |
| 211 | ]), |
| 212 | ), |
| 213 | ])); |
| 214 | |
| 215 | assert_eq!( |
| 216 | any, |
| 217 | serde_json::from_str::<Any>(serde_json::to_string(&any).unwrap().as_str()).unwrap() |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn any_is_serde_json_convertible() { |
| 223 | let any = any!({ |
| 224 | "bool": true, |
| 225 | "int": 1, |
| 226 | "negativeInt": -1, |
| 227 | "realNumber": -123.2387f64, |
| 228 | "null": Any::Null, |
| 229 | "map": { |
| 230 | "bool": true, |
| 231 | "int": 1, |
| 232 | "negativeInt": -1, |
| 233 | "realNumber": -123.2387f64, |
| 234 | "null": Any::Null |
| 235 | }, |
| 236 | "key6":[ |
| 237 | true, |
| 238 | 1, |
| 239 | -1, |
| 240 | -123.2387f64, |
| 241 | Any::Null |
| 242 | ] |
| 243 | }); |
| 244 | |
| 245 | let expected = json!({ |
| 246 | "bool": true, |
| 247 | "int": 1, |
| 248 | "negativeInt": -1, |
| 249 | "realNumber": -123.2387f64, |
| 250 | "null": Any::Null, |
| 251 | "map": { |
| 252 | "bool": true, |
| 253 | "int": 1, |
| 254 | "negativeInt": -1, |
| 255 | "realNumber": -123.2387f64, |
| 256 | "null": Any::Null |
| 257 | }, |
| 258 | "key6":[ |
| 259 | true, |
| 260 | 1, |
| 261 | -1, |
| 262 | -123.2387f64, |
| 263 | Any::Null |
| 264 | ] |
| 265 | }); |
| 266 | |
| 267 | let json = serde_json::to_value(&any).unwrap(); |
| 268 | assert_eq!(json, expected); |
| 269 | let any2: Any = serde_json::from_value(json).unwrap(); |
| 270 | assert_eq!(any, any2); |
| 271 | } |
| 272 | } |
| 273 | |