nautechsystems/nautilus_trader · error
Failed to serialize json `payload`: {e}
Error message
Failed to serialize json `payload`: {e} What it means
serialize_payload's JSON branch converts the payload to serde_json::Value (with timestamp conversion) and encodes with serde_json::to_vec. This error wraps serde_json serialization failures — values that cannot be represented in JSON.
Source
Thrown at crates/infrastructure/src/redis/queries.rs:93
/// # Errors
///
/// Returns an error if serialization to the chosen encoding fails.
pub fn serialize_payload<T: Serialize>(
encoding: SerializationEncoding,
payload: &T,
) -> anyhow::Result<Vec<u8>> {
match encoding {
SerializationEncoding::MsgPack => {
let mut value = serde_json::to_value(payload)?;
convert_timestamps(&mut value);
rmp_serde::to_vec(&value)
.map_err(|e| anyhow::anyhow!("Failed to serialize msgpack `payload`: {e}"))
}
SerializationEncoding::Json => {
let mut value = serde_json::to_value(payload)?;
convert_timestamps(&mut value);
serde_json::to_vec(&value)
.map_err(|e| anyhow::anyhow!("Failed to serialize json `payload`: {e}"))
}
SerializationEncoding::Sbe => {
anyhow::bail!("SBE encoding is not supported for Redis cache payloads")
}
SerializationEncoding::Capnp => {
anyhow::bail!("Cap'n Proto encoding is not supported for Redis cache payloads")
}
}
}
/// Deserializes the given byte slice `payload` into type `T` using the specified `encoding`.
///
/// # Errors
///
/// Returns an error if deserialization from the chosen encoding fails or converting to the target type fails.
pub fn deserialize_payload<T: DeserializeOwned>(
encoding: SerializationEncoding,
payload: &[u8],View on GitHub (pinned to 18893faf8b)
Solutions
- Validate/replace NaN and Infinity values in the payload before serialization.
- Use string-serializable map keys in the payload type.
- Confirm the type's Serialize impl produces JSON-compatible structures.
- If SBE/Capnp was intended, note those encodings are explicitly unsupported for Redis cache payloads.
Example fix
// before
let payload = Quote { bid: f64::INFINITY };
serialize_payload(SerializationEncoding::Json, &payload)?;
// after
let bid = if payload.bid.is_finite() { payload.bid } else { 0.0 };
serialize_payload(SerializationEncoding::Json, &Quote { bid })?; Defensive patterns
Strategy: try-catch
Validate before calling
// ensure JSON compatibility before serializing
fn is_json_safe(v: &serde_json::Value) -> bool {
match v {
serde_json::Value::Number(n) => n.as_f64().map(|f| f.is_finite()).unwrap_or(true),
serde_json::Value::Array(a) => a.iter().all(is_json_safe),
serde_json::Value::Object(o) => o.values().all(is_json_safe),
_ => true,
}
} Try / catch
match serialize_payload(SerializationEncoding::Json, &payload) {
Ok(bytes) => write_to_redis(bytes),
Err(e) if e.to_string().contains("Failed to serialize json") => {
log::error!("payload not JSON-serializable: {e}");
}
Err(e) => return Err(e),
} Prevention
- Sanitize non-finite floats in domain values before caching.
- Ensure custom Serialize impls emit JSON-compatible structures.
- Round-trip test each payload type through serde_json.
When it happens
Trigger: Calling serialize_payload(SerializationEncoding::Json, payload) where serde_json::to_value/to_vec fails, e.g. payloads containing NaN/Infinity floats or non-string map keys.
Common situations: Domain values carrying NaN/Inf prices; maps keyed by non-string types; custom Serialize impls emitting JSON-illegal values.
Understand the failure class
Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.
Related errors
- Failed to deserialize json `payload`: {e}
- SBE encoding is not supported for Redis cache payloads
- Cap'n Proto encoding is not supported for Redis cache payloa
- Python object has no to_json() method or __dict__ attribute
- Failed to serialize the deployment manifest
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/0aad8883dd909b5a.
Report an issue: GitHub.