zed-industries/zed · error

failed to serialize JSON

Error message

failed to serialize JSON

What it means

Panics inside From<Json<T>> for AsyncBody when serde_json::to_vec fails to serialize the wrapped T. Because serde_json's Serializer is infallible for virtually all types (it errors only on maps/keys that are not strings or on disabled features), this effectively only fires when T's Serialize impl itself errors — i.e., a custom Serialize implementation returning an Err.

Source

Thrown at crates/http_client/src/async_body.rs:99

        Self::from_bytes(Bytes::from_static(s))
    }
}

impl From<&'static str> for AsyncBody {
    #[inline]
    fn from(s: &'static str) -> Self {
        Self::from_bytes(Bytes::from_static(s.as_bytes()))
    }
}

/// Newtype wrapper that serializes a value as JSON into an `AsyncBody`.
pub struct Json<T: Serialize>(pub T);

impl<T: Serialize> From<Json<T>> for AsyncBody {
    fn from(json: Json<T>) -> Self {
        Self::from_bytes(
            serde_json::to_vec(&json.0)
                .expect("failed to serialize JSON")
                .into(),
        )
    }
}

impl<T: Into<Self>> From<Option<T>> for AsyncBody {
    fn from(body: Option<T>) -> Self {
        match body {
            Some(body) => body.into(),
            None => Self::empty(),
        }
    }
}

impl futures::AsyncRead for AsyncBody {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,

View on GitHub (pinned to f4178619ac)

Solutions

  1. Audit the custom Serialize impl of the wrapped type T for error-returning paths
  2. Switch the conversion to return a Result (e.g., serde_json::to_vec(...).map_err(...) into anyhow) so callers can propagate the failure
  3. Add a unit test serializing the concrete type to catch regressions in its Serialize impl
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at crates/http_client/src/async_body.rs:99 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/ca4c890d0ac5ab1c. Report an issue: GitHub.