{"record":{"id":"d8491e489e2d55fd","repo":"nautechsystems/nautilus_trader","slug":"failed-to-deserialize-json-payload-e","errorCode":null,"errorMessage":"Failed to deserialize json `payload`: {e}","messagePattern":"Failed to deserialize json `payload`: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/redis/queries.rs","lineNumber":117,"sourceCode":"                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],\n    ) -> anyhow::Result<T> {\n        let mut value = match encoding {\n            SerializationEncoding::MsgPack => rmp_serde::from_slice(payload)\n                .map_err(|e| anyhow::anyhow!(\"Failed to deserialize msgpack `payload`: {e}\"))?,\n            SerializationEncoding::Json => serde_json::from_slice(payload)\n                .map_err(|e| anyhow::anyhow!(\"Failed to deserialize json `payload`: {e}\"))?,\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        convert_timestamp_strings(&mut value);\n\n        serde_json::from_value(value)\n            .map_err(|e| anyhow::anyhow!(\"Failed to convert value to target type: {e}\"))\n    }\n\n    /// Scans Redis for keys matching the given `pattern`.\n    ///\n    /// # Errors\n    ///","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/redis/queries.rs#L99-L135","documentation":"deserialize_payload converts raw Redis bytes into a typed value. When the JSON branch (SerializationEncoding::Json) is selected, serde_json::from_slice fails to parse the stored bytes as JSON (or they do not fit T), so the library wraps the serde error in an anyhow error. This indicates the payload stored in Redis does not match the expected encoding or schema.","triggerScenarios":"Calling deserialize_payload (directly or via load_all / load_instruments / load_instrument_closes / load_orders / load_positions / etc.) with encoding=SerializationEncoding::Json while the bytes in Redis are MsgPack-encoded, truncated, corrupted, or were written by an older schema version.","commonSituations":"Encoding was switched between MsgPack and Json after data was written; a manually inserted/corrupted Redis key; a version upgrade changed struct fields so old JSON no longer deserializes; a bulk read returned stale/wrong values due to key-index drift.","solutions":["Verify the SerializationEncoding passed matches the encoding used when the data was written (flush/rewrite the cache if the encoding was changed).","Inspect the offending Redis value with redis-cli GET/<key> and validate it as JSON.","Check whether the target struct changed between versions and re-snapshot the cache from a fresh backtest/live run.","Ensure the Redis index keys point at the correct value keys (no stale index entries)."],"exampleFix":"// before\nlet closes: Vec<InstrumentClose> = RedisCacheDatabase::load_all(con, \"trader\", SerializationEncoding::Json).await?;\n// after: match the encoding used at write time (e.g. MsgPack)\nlet closes: Vec<InstrumentClose> = RedisCacheDatabase::load_all(con, \"trader\", SerializationEncoding::MsgPack).await?;","handlingStrategy":"try-catch","validationCode":"// Rust: verify payload is JSON before loading\nfn looks_like_json(bytes: &[u8]) -> bool {\n    serde_json::from_slice::<serde_json::Value>(bytes).is_ok()\n}","typeGuard":"fn is_json_encoding(encoding: &SerializationEncoding) -> bool {\n    matches!(encoding, SerializationEncoding::Json)\n}","tryCatchPattern":"match RedisCacheDatabase::load_all(&mut con, trader_key, encoding).await {\n    Ok(cache) => cache,\n    Err(e) => {\n        if e.to_string().contains(\"Failed to deserialize\") {\n            // fall back: reload with alternate encoding or re-snapshot cache\n        }\n        return Err(e);\n    }\n}","preventionTips":["Pin one SerializationEncoding in config and use it for both write and read paths.","Flush and re-snapshot the Redis cache whenever changing encoding or upgrading versions.","Validate stored payloads parse as JSON before running load_all in production."],"tags":["redis","deserialization","json","serialization"],"backgroundTag":"json-unmarshal-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"}