{"record":{"id":"cf4c03d694af5894","repo":"nautechsystems/nautilus_trader","slug":"json-does-not-represent-customdata","errorCode":null,"errorMessage":"JSON does not represent CustomData","messagePattern":"JSON does not represent CustomData","errorType":"validation","errorClass":"serde_json::Error","httpStatus":null,"severity":"error","filePath":"crates/model/src/data/custom.rs","lineNumber":427,"sourceCode":"impl PartialEq for CustomData {\n    fn eq(&self, other: &Self) -> bool {\n        self.data.eq_arc(other.data.as_ref()) && self.data_type == other.data_type\n    }\n}\n\nimpl HasTsInit for CustomData {\n    fn ts_init(&self) -> UnixNanos {\n        self.data.ts_init()\n    }\n}\n\npub(crate) fn parse_custom_data_from_json_bytes(\n    bytes: &[u8],\n) -> Result<CustomData, serde_json::Error> {\n    let data: Data = serde_json::from_slice(bytes)?;\n    match data {\n        Data::Custom(custom) => Ok(custom),\n        _ => Err(serde_json::Error::io(std::io::Error::new(\n            std::io::ErrorKind::InvalidData,\n            \"JSON does not represent CustomData\",\n        ))),\n    }\n}\n\nimpl CustomData {\n    /// Deserializes `CustomData` from JSON bytes (full `CustomData` format with type and `data_type`).\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the bytes are not valid JSON or do not represent `CustomData`.\n    pub fn from_json_bytes(bytes: &[u8]) -> Result<Self, serde_json::Error> {\n        parse_custom_data_from_json_bytes(bytes)\n    }\n}\n\n/// Canonical JSON envelope for `CustomData`. All serialized `CustomData` uses this shape so","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/data/custom.rs#L409-L445","documentation":"parse_custom_data_from_json_bytes deserializes a JSON payload into the model's Data enum and requires the variant to be Data::Custom. If the JSON parses as valid Data but represents a different type (quote, trade, bar, etc.), an InvalidData io-backed serde_json error is raised with \"JSON does not represent CustomData\". It means the caller asked for CustomData but supplied JSON of another Data kind.","triggerScenarios":"Calling CustomData.from_json_bytes (or its Python binding py_from_json_bytes_py) with bytes that decode to a Data JSON object for a non-custom variant, e.g. a serialized QuoteTick, TradeTick, Bar, or Delta.","commonSituations":"Mixing up files in a data pipeline (feeding bar JSON where custom data is expected); writing generic Data JSON to disk and later assuming it is custom; passing user-supplied payloads to the custom-data parser without checking the \"type\" field.","solutions":["Inspect the JSON's \"type\" field and use the matching from_json_bytes parser for that Data type.","Re-export/persist the data ensuring it was serialized as CustomData (type tag \"custom\").","If parsing heterogeneous Data, parse into Data first and route on the variant instead of calling the custom-only parser.","Validate the payload's type tag before calling parse_custom_data_from_json_bytes."],"exampleFix":"// before\nlet custom = CustomData::from_json_bytes(bytes)?;\n// after\nlet data: Data = serde_json::from_slice(bytes)?;\nlet custom = match data {\n    Data::Custom(c) => c,\n    other => anyhow::bail!(\"expected CustomData, got {:?}\", other),\n};","handlingStrategy":"validation","validationCode":"fn is_custom_data_json(bytes: &[u8]) -> bool {\n    serde_json::from_slice::<serde_json::Value>(bytes)\n        .ok()\n        .and_then(|v| v.get(\"type\").and_then(|t| t.as_str()).map(|s| s == \"CustomData\".to_owned() || s == \"custom\"))\n        .unwrap_or(false)\n}","typeGuard":"fn as_custom_data(data: &Data) -> Option<&CustomData> {\n    match data { Data::Custom(c) => Some(c), _ => None }\n}","tryCatchPattern":"match CustomData::from_json_bytes(bytes) {\n    Ok(custom) => handle(custom),\n    Err(e) if e.to_string().contains(\"does not represent CustomData\") => {\n        let data: Data = serde_json::from_slice(bytes)?;\n        route_by_variant(data); // dispatch to the right parser\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Check the JSON \"type\" tag before choosing a from_json_bytes parser","Persist Data payloads with their type tags intact","Route heterogeneous data through a variant dispatch, not a custom-only parser","Never assume user-supplied JSON is CustomData"],"tags":["serialization","json","custom-data","type-mismatch","rust"],"backgroundTag":"type-mismatch","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"}