Last active
October 5, 2023 22:31
-
-
Save myksao/e98c215de383fcef69cc1fdfff41ca3a to your computer and use it in GitHub Desktop.
field and value extraction using serde json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use serde::{Deserialize, Serialize}; | |
use serde_json::Value; | |
pub fn extraction<T: Default + Serialize>(data: &T) -> Value { | |
let columns = serde_json::to_value(data.clone()) | |
.unwrap() | |
.as_object() | |
.unwrap() | |
.keys() | |
.map(|key| key.to_string()) | |
.collect::<Vec<String>>(); | |
match serde_json::to_value(data) { | |
Ok(Value::Object(mut map)) => { | |
map.retain(|k, v| columns.contains(&k.to_string()) && !v.is_null()); | |
Value::Object(map) | |
}, | |
_ => Value::Null, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add preserve_order and indexmap feature flag to serde_json