MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / json_to_f64

Function json_to_f64

nodedb-query/src/json_ops.rs:16–23  ·  view source on GitHub ↗

Coerce a JSON value to f64. - Numbers: `as_f64()` directly - Strings: parse as f64 (`"5"` → `5.0`) - Booleans: `true` → `1.0`, `false` → `0.0` (when `coerce_bool` is true) - Other types: `None`

(v: &serde_json::Value, coerce_bool: bool)

Source from the content-addressed store, hash-verified

14/// - Booleans: `true` → `1.0`, `false` → `0.0` (when `coerce_bool` is true)
15/// - Other types: `None`
16pub fn json_to_f64(v: &serde_json::Value, coerce_bool: bool) -> Option<f64> {
17 match v {
18 serde_json::Value::Number(n) => n.as_f64(),
19 serde_json::Value::String(s) => s.parse::<f64>().ok(),
20 serde_json::Value::Bool(b) if coerce_bool => Some(if *b { 1.0 } else { 0.0 }),
21 _ => None,
22 }
23}
24
25/// Compare two JSON values with type coercion.
26///

Callers 2

compare_jsonFunction · 0.70
coerced_eqFunction · 0.70

Calls 2

as_f64Method · 0.45
okMethod · 0.45

Tested by

no test coverage detected