{"record":{"id":"0aad8883dd909b5a","repo":"nautechsystems/nautilus_trader","slug":"failed-to-serialize-json-payload-e","errorCode":null,"errorMessage":"Failed to serialize json `payload`: {e}","messagePattern":"Failed to serialize json `payload`: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/redis/queries.rs","lineNumber":93,"sourceCode":"    /// # Errors\n    ///\n    /// Returns an error if serialization to the chosen encoding fails.\n    pub fn serialize_payload<T: Serialize>(\n        encoding: SerializationEncoding,\n        payload: &T,\n    ) -> anyhow::Result<Vec<u8>> {\n        match encoding {\n            SerializationEncoding::MsgPack => {\n                let mut value = serde_json::to_value(payload)?;\n                convert_timestamps(&mut value);\n                rmp_serde::to_vec(&value)\n                    .map_err(|e| anyhow::anyhow!(\"Failed to serialize msgpack `payload`: {e}\"))\n            }\n            SerializationEncoding::Json => {\n                let mut value = serde_json::to_value(payload)?;\n                convert_timestamps(&mut value);\n                serde_json::to_vec(&value)\n                    .map_err(|e| anyhow::anyhow!(\"Failed to serialize json `payload`: {e}\"))\n            }\n            SerializationEncoding::Sbe => {\n                anyhow::bail!(\"SBE encoding is not supported for Redis cache payloads\")\n            }\n            SerializationEncoding::Capnp => {\n                anyhow::bail!(\"Cap'n Proto encoding is not supported for Redis cache payloads\")\n            }\n        }\n    }\n\n    /// Deserializes the given byte slice `payload` into type `T` using the specified `encoding`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if deserialization from the chosen encoding fails or converting to the target type fails.\n    pub fn deserialize_payload<T: DeserializeOwned>(\n        encoding: SerializationEncoding,\n        payload: &[u8],","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/redis/queries.rs#L75-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"Domain values carrying NaN/Inf prices; maps keyed by non-string types; custom Serialize impls emitting JSON-illegal values.","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."],"exampleFix":"// before\nlet payload = Quote { bid: f64::INFINITY };\nserialize_payload(SerializationEncoding::Json, &payload)?;\n// after\nlet bid = if payload.bid.is_finite() { payload.bid } else { 0.0 };\nserialize_payload(SerializationEncoding::Json, &Quote { bid })?;","handlingStrategy":"try-catch","validationCode":"// ensure JSON compatibility before serializing\nfn is_json_safe(v: &serde_json::Value) -> bool {\n    match v {\n        serde_json::Value::Number(n) => n.as_f64().map(|f| f.is_finite()).unwrap_or(true),\n        serde_json::Value::Array(a) => a.iter().all(is_json_safe),\n        serde_json::Value::Object(o) => o.values().all(is_json_safe),\n        _ => true,\n    }\n}","typeGuard":null,"tryCatchPattern":"match serialize_payload(SerializationEncoding::Json, &payload) {\n    Ok(bytes) => write_to_redis(bytes),\n    Err(e) if e.to_string().contains(\"Failed to serialize json\") => {\n        log::error!(\"payload not JSON-serializable: {e}\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["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."],"tags":["redis","serialization","json"],"backgroundTag":"json-serialization-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}