Convert a document byte blob to `serde_json::Value`. Auto-detects the format: MessagePack, JSON, or Binary Tuple. Binary Tuple detection requires knowing the schema — if the bytes don't match MessagePack or JSON, returns `None` (the caller should use `strict_format::binary_tuple_to_json` with the schema if the collection is known to be strict).
(bytes: &[u8])
| 22 | /// use `strict_format::binary_tuple_to_json` with the schema if the |
| 23 | /// collection is known to be strict). |
| 24 | pub(super) fn decode_document(bytes: &[u8]) -> Option<serde_json::Value> { |
| 25 | if bytes.is_empty() { |
| 26 | return None; |
| 27 | } |
| 28 | |
| 29 | // Detect MessagePack: maps start with 0x80-0x8F (fixmap), 0xDE (map16), 0xDF (map32). |
| 30 | let first = bytes[0]; |
| 31 | if (0x80..=0x8F).contains(&first) || first == 0xDE || first == 0xDF { |
| 32 | // Try MessagePack first. |
| 33 | if let Ok(val) = nodedb_types::json_from_msgpack(bytes) { |
| 34 | return Some(val); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Fall back to JSON. |
| 39 | sonic_rs::from_slice(bytes).ok() |
| 40 | |
| 41 | // Note: Binary Tuple bytes are NOT auto-detected here because decoding |
| 42 | // requires the schema. For strict collections, callers must check |
| 43 | // doc_configs.storage_mode and use strict_format::binary_tuple_to_json(). |
| 44 | } |
| 45 | |
| 46 | /// Convert a document byte blob to `nodedb_types::Value`. |
| 47 | /// |