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

Function json_to_msgpack

nodedb/src/engine/document/store/extract.rs:98–126  ·  view source on GitHub ↗

Convert serde_json::Value to rmpv::Value for MessagePack serialization.

(val: &serde_json::Value)

Source from the content-addressed store, hash-verified

96
97/// Convert serde_json::Value to rmpv::Value for MessagePack serialization.
98pub fn json_to_msgpack(val: &serde_json::Value) -> rmpv::Value {
99 match val {
100 serde_json::Value::Null => rmpv::Value::Nil,
101 serde_json::Value::Bool(b) => rmpv::Value::Boolean(*b),
102 serde_json::Value::Number(n) => {
103 if let Some(i) = n.as_i64() {
104 rmpv::Value::Integer(rmpv::Integer::from(i))
105 } else if let Some(u) = n.as_u64() {
106 rmpv::Value::Integer(rmpv::Integer::from(u))
107 } else {
108 rmpv::Value::F64(n.as_f64().unwrap_or(0.0))
109 }
110 }
111 serde_json::Value::String(s) => rmpv::Value::String(rmpv::Utf8String::from(s.as_str())),
112 serde_json::Value::Array(arr) => {
113 rmpv::Value::Array(arr.iter().map(json_to_msgpack).collect())
114 }
115 serde_json::Value::Object(obj) => rmpv::Value::Map(
116 obj.iter()
117 .map(|(k, v)| {
118 (
119 rmpv::Value::String(rmpv::Utf8String::from(k.as_str())),
120 json_to_msgpack(v),
121 )
122 })
123 .collect(),
124 ),
125 }
126}
127
128/// Convert rmpv::Value back to serde_json::Value.
129pub(crate) fn rmpv_to_json(val: &rmpv::Value) -> serde_json::Value {

Callers 10

msgpack_is_compactFunction · 0.70
mp_objFunction · 0.50
make_entryFunction · 0.50
raw_msgpack_roundtripFunction · 0.50
putMethod · 0.50

Calls 6

collectMethod · 0.80
as_i64Method · 0.45
as_u64Method · 0.45
as_f64Method · 0.45
as_strMethod · 0.45
iterMethod · 0.45