|
1 | 1 | use plugins::*;
|
2 | 2 | use serde::{Deserialize, Serialize};
|
| 3 | +use serde_json::Value; |
3 | 4 | use std::collections::HashMap;
|
4 | 5 | use users::*;
|
5 | 6 | use wp_localization::{MessageBundle, WpMessages, WpSupportsLocalization};
|
@@ -118,6 +119,14 @@ pub enum JsonValue {
|
118 | 119 | Object(HashMap<String, JsonValue>),
|
119 | 120 | }
|
120 | 121 |
|
| 122 | +/// Similar to `JsonValue`, but exported as a Uniffi object. |
| 123 | +#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, uniffi::Object)] |
| 124 | +#[uniffi::export(Eq, Hash)] |
| 125 | +pub struct AnyJson { |
| 126 | + #[serde(flatten)] |
| 127 | + pub raw: Value, |
| 128 | +} |
| 129 | + |
121 | 130 | uniffi::custom_newtype!(WpResponseString, Option<String>);
|
122 | 131 | #[derive(Debug, Serialize, Deserialize)]
|
123 | 132 | #[serde(try_from = "BoolOrString")]
|
@@ -235,4 +244,30 @@ mod tests {
|
235 | 244 | fn test_orderby_string_conversion(#[case] orderby: WpApiParamOrder) {
|
236 | 245 | assert_eq!(orderby, orderby.to_string().parse().unwrap());
|
237 | 246 | }
|
| 247 | + |
| 248 | + #[derive(Deserialize, Debug)] |
| 249 | + struct Person { |
| 250 | + name: String, |
| 251 | + #[serde(flatten)] |
| 252 | + other_fields: AnyJson, |
| 253 | + } |
| 254 | + |
| 255 | + #[test] |
| 256 | + fn test_parse_any_json() { |
| 257 | + let json = r#"{"name": "Alice", "age": 30, "city": "Wonderland"}"#; |
| 258 | + let person: Person = serde_json::from_str(json).unwrap(); |
| 259 | + assert_eq!(person.name, "Alice"); |
| 260 | + assert_eq!( |
| 261 | + person.other_fields.raw, |
| 262 | + serde_json::json!({"age": 30, "city": "Wonderland"}) |
| 263 | + ); |
| 264 | + } |
| 265 | + |
| 266 | + #[test] |
| 267 | + fn test_parse_empty_any_json() { |
| 268 | + let json = r#"{"name": "Alice"}"#; |
| 269 | + let person: Person = serde_json::from_str(json).unwrap(); |
| 270 | + assert_eq!(person.name, "Alice"); |
| 271 | + assert_eq!(person.other_fields.raw, serde_json::json!({})); |
| 272 | + } |
238 | 273 | }
|
0 commit comments