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

Function value_to_json

nodedb/src/data/executor/strict_format/coerce.rs:307–341  ·  view source on GitHub ↗

Convert a typed `Value` to JSON (for pgwire output only).

(val: &Value)

Source from the content-addressed store, hash-verified

305
306/// Convert a typed `Value` to JSON (for pgwire output only).
307pub fn value_to_json(val: &Value) -> serde_json::Value {
308 match val {
309 Value::Null => serde_json::Value::Null,
310 Value::Bool(b) => serde_json::Value::Bool(*b),
311 Value::Integer(i) => serde_json::json!(*i),
312 Value::Float(f) => serde_json::Number::from_f64(*f)
313 .map(serde_json::Value::Number)
314 .unwrap_or(serde_json::Value::Null),
315 Value::String(s) | Value::Uuid(s) | Value::Ulid(s) => serde_json::Value::String(s.clone()),
316 Value::Bytes(b) => serde_json::Value::String(base64::Engine::encode(
317 &base64::engine::general_purpose::STANDARD,
318 b,
319 )),
320 Value::DateTime(dt) | Value::NaiveDateTime(dt) => serde_json::json!(dt.micros / 1000),
321 Value::Duration(d) => serde_json::json!(d.as_millis()),
322 Value::Decimal(d) => serde_json::Value::String(d.to_string()),
323 Value::Array(arr) => serde_json::Value::Array(arr.iter().map(value_to_json).collect()),
324 Value::Object(map) => {
325 let mut obj = serde_json::Map::new();
326 for (k, v) in map {
327 obj.insert(k.clone(), value_to_json(v));
328 }
329 serde_json::Value::Object(obj)
330 }
331 Value::Geometry(_)
332 | Value::Set(_)
333 | Value::Regex(_)
334 | Value::Range { .. }
335 | Value::Record { .. }
336 | Value::ArrayCell(_) => serde_json::Value::Null,
337 // Value is #[non_exhaustive]; future variants collapse to JSON null
338 // at the pgwire output boundary.
339 _ => serde_json::Value::Null,
340 }
341}
342
343#[cfg(test)]
344mod tests {

Callers 1

binary_tuple_to_jsonFunction · 0.70

Calls 6

to_stringMethod · 0.80
collectMethod · 0.80
encodeFunction · 0.50
cloneMethod · 0.45
iterMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected