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])
| 86 | /// - JSON bytes: parsed and re-encoded as standard msgpack map. |
| 87 | /// - Unknown bytes: returned as-is. |
| 88 | pub(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 | |
| 106 | fn is_standard_msgpack_map(bytes: &[u8]) -> bool { |
| 107 | let first = bytes[0]; |