nikivdev/code · error
failed to decode json bytes: {err}
Error message
failed to decode json bytes: {err} What it means
parse_json_bytes_in_place's fallback path uses serde_json::from_slice when simd-json is unavailable for the target/features. Decode failures are wrapped as "failed to decode json bytes: {err}" with the serde_json error detail.
Source
Thrown at src/json_parse.rs:45
#[inline]
pub fn parse_json_bytes_in_place<T: DeserializeOwned>(bytes: &mut [u8]) -> Result<T> {
#[cfg(all(
feature = "linux-host-simd-json",
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
))]
{
return simd_json::serde::from_slice(bytes)
.map_err(|err| anyhow!("failed to decode json bytes with simd-json: {err}"));
}
#[cfg(not(all(
feature = "linux-host-simd-json",
target_os = "linux",
any(target_arch = "x86_64", target_arch = "aarch64")
)))]
{
serde_json::from_slice(bytes).map_err(|err| anyhow!("failed to decode json bytes: {err}"))
}
}
View on GitHub (pinned to a747e741ae)
Solutions
- Read the wrapped serde_json error for the exact position and cause
- Verify the buffer holds one complete JSON value with no trailing garbage
- Update the target struct to match the current producer schema
- If input may have a BOM, strip it before parsing
Example fix
// before let v: Event = parse_json_bytes_in_place(bytes)?; // after let bytes = bytes.strip_prefix(b"\xef\xbb\xbf").unwrap_or(bytes); let v: Event = parse_json_bytes_in_place(bytes)?;
Defensive patterns
Strategy: try-catch
Validate before calling
fn is_complete_json_bytes(bytes: &[u8]) -> bool {
serde_json::from_slice::<serde_json::Value>(bytes).is_ok()
} Try / catch
match parse_json_bytes_in_place::<Event>(bytes) {
Ok(v) => handle(v),
Err(e) => {
eprintln!("bad json bytes: {e:#}");
// quarantine bytes for inspection
}
} Prevention
- Validate byte boundaries (single JSON value, no trailing data)
- Strip BOM before parsing
- Keep consumer structs aligned with producer schema
- Add round-trip tests serde vs simd backends
When it happens
Trigger: Malformed or schema-incompatible JSON bytes passed to parse_json_bytes_in_place on non-simd builds (non-Linux, non-x86_64/aarch64, or feature disabled).
Common situations: Truncated file reads; extra whitespace/BOM at the start; producer schema changed (renamed fields, different types) while consumer struct is stale.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to decode json line: {err}
- failed to decode json bytes with simd-json: {err}
- failed to decode json line with simd-json: {err}
- 'codanna mcp get_index_info' failed: {}
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/765a4016d37354ba.
Report an issue: GitHub.