use serde_json::Value;
pub type Document = serde_json::Map<String, Value>;
pub fn raw_values(doc: &Document, key: &str) -> Vec<Value> {
match doc.get(key) {
None => vec![Value::String(String::new())],
Some(Value::Array(values)) => values.clone(),
Some(value) => vec![value.clone()],
}
}
pub fn str_values(doc: &Document, key: &str) -> Vec<String> {
raw_values(doc, key).iter().map(py_str).collect()
}
pub fn first_str(doc: &Document, key: &str) -> Option<String> {
let value = raw_values(doc, key).into_iter().next()?;
if !truthy(&value) {
return None;
}
Some(py_str(&value))
}
pub fn first_f64(doc: &Document, key: &str) -> Option<f64> {
match raw_values(doc, key).into_iter().next()? {
Value::Number(n) => n.as_f64(),
Value::String(s) => s.trim().parse().ok(),
_ => None,
}
}
pub fn truthy(value: &Value) -> bool {
match value {
Value::Null => false,
Value::Bool(b) => *b,
Value::Number(n) => n.as_f64().is_some_and(|f| f != 0.0),
Value::String(s) => !s.is_empty(),
Value::Array(a) => !a.is_empty(),
Value::Object(o) => !o.is_empty(),
}
}
pub fn py_str(value: &Value) -> String {
match value {
Value::Null => "None".into(),
Value::Bool(true) => "True".into(),
Value::Bool(false) => "False".into(),
Value::String(s) => s.clone(),
Value::Number(n) => {
if n.is_f64() {
let f = n.as_f64().unwrap_or_default();
if f.fract() == 0.0 && f.abs() < 1e16 {
format!("{f:.1}")
} else {
format!("{f}")
}
} else {
n.to_string()
}
}
other => other.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn reads_like_addok() {
let doc: Document = serde_json::from_value(
json!({"city": ["Paris", "Paname"], "postcode": 75001, "x": 1.0, "n": null}),
)
.unwrap();
assert_eq!(first_str(&doc, "city").as_deref(), Some("Paris"));
assert_eq!(first_str(&doc, "postcode").as_deref(), Some("75001"));
assert_eq!(py_str(&doc["x"]), "1.0");
assert_eq!(first_str(&doc, "n"), None);
assert_eq!(first_str(&doc, "missing"), None);
}
}