Convert `serde_json::Value` to `nodedb_types::Value`.
(v: &serde_json::Value)
| 66 | |
| 67 | /// Convert `serde_json::Value` to `nodedb_types::Value`. |
| 68 | pub(crate) fn json_to_value(v: &serde_json::Value) -> Value { |
| 69 | match v { |
| 70 | serde_json::Value::Null => Value::Null, |
| 71 | serde_json::Value::Bool(b) => Value::Bool(*b), |
| 72 | serde_json::Value::Number(n) => { |
| 73 | if let Some(i) = n.as_i64() { |
| 74 | Value::Integer(i) |
| 75 | } else { |
| 76 | Value::Float(n.as_f64().unwrap_or(0.0)) |
| 77 | } |
| 78 | } |
| 79 | serde_json::Value::String(s) => Value::String(s.clone()), |
| 80 | serde_json::Value::Array(a) => Value::Array(a.iter().map(json_to_value).collect()), |
| 81 | serde_json::Value::Object(m) => Value::Object( |
| 82 | m.iter() |
| 83 | .map(|(k, v)| (k.clone(), json_to_value(v))) |
| 84 | .collect(), |
| 85 | ), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Format an f32 slice as a SQL ARRAY literal: `ARRAY[0.1,0.2,0.3]`. |
| 90 | pub(crate) fn format_vector_array(v: &[f32]) -> String { |