nikivdev/code · error

failed to decode json bytes with simd-json: {err}

Error message

failed to decode json bytes with simd-json: {err}

What it means

parse_json_bytes_in_place decodes a byte slice into T using simd-json (which mutates the buffer in place) when the linux-host-simd-json feature and matching target are active. Failures from simd_json::serde::from_slice are wrapped with this message. The caller must own a mutable buffer for this path.

Source

Thrown at src/json_parse.rs:36

        feature = "linux-host-simd-json",
        target_os = "linux",
        any(target_arch = "x86_64", target_arch = "aarch64")
    )))]
    {
        serde_json::from_str(line).map_err(|err| anyhow!("failed to decode json line: {err}"))
    }
}

#[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

  1. Log the wrapped simd-json error to identify the failure offset
  2. Confirm the byte slice contains exactly one complete JSON value
  3. Deserialize into serde_json::Value first to inspect the real shape if the schema is uncertain
  4. Check for stray BOM or trailing bytes after the JSON value

Example fix

// before
let v: Event = parse_json_bytes_in_place(&mut buf)?;
// after
if buf.is_empty() { return Err(anyhow!("empty payload")); }
let v: Event = parse_json_bytes_in_place(&mut buf)?;
Defensive patterns

Strategy: try-catch

Validate before calling

fn looks_like_json_bytes(bytes: &[u8]) -> bool {
    let t = bytes.iter().find(|b| !b.is_ascii_whitespace());
    matches!(t, Some(b'{') | Some(b'['))
}

Try / catch

match parse_json_bytes_in_place::<Event>(&mut buf) {
    Ok(v) => handle(v),
    Err(e) if e.to_string().contains("simd-json") => {
        eprintln!("malformed payload: {e:#}");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Malformed, truncated, or schema-mismatched JSON bytes are passed to parse_json_bytes_in_place on a simd-json-enabled build.

Common situations: Reading partially flushed network/file buffers; concatenated JSON values without a delimiter; T doesn't match the byte payload's structure.

Understand the failure class

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/d1236c7ea0a5b89c. Report an issue: GitHub.