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

Function json_to_msgpack

nodedb/src/data/executor/doc_format.rs:88–104  ·  view source on GitHub ↗

Convert JSON bytes to MessagePack bytes. If the input is already MessagePack, returns it unchanged. Handles three input formats: - Standard msgpack map (0x80–0x8F / 0xDE / 0xDF): returned as-is. - JSON bytes: parsed and re-encoded as standard msgpack map. - Unknown bytes: returned as-is.

(bytes: &[u8])

Source from the content-addressed store, hash-verified

86/// - JSON bytes: parsed and re-encoded as standard msgpack map.
87/// - Unknown bytes: returned as-is.
88pub(super) fn json_to_msgpack(bytes: &[u8]) -> Vec<u8> {
89 if bytes.is_empty() {
90 return bytes.to_vec();
91 }
92
93 // Already a standard MessagePack map? Return as-is.
94 let first = bytes[0];
95 if (0x80..=0x8F).contains(&first) || first == 0xDE || first == 0xDF {
96 return bytes.to_vec();
97 }
98
99 // Try parsing as JSON and converting to MessagePack.
100 match sonic_rs::from_slice::<serde_json::Value>(bytes) {
101 Ok(value) => encode_to_msgpack(&value),
102 Err(_) => bytes.to_vec(),
103 }
104}
105
106fn is_standard_msgpack_map(bytes: &[u8]) -> bool {
107 let first = bytes[0];

Calls 4

encode_to_msgpackFunction · 0.85
is_emptyMethod · 0.45
to_vecMethod · 0.45
containsMethod · 0.45