nautechsystems/nautilus_trader · error · anyhow::Error
Failed to serialize JSON: {e}
Error message
Failed to serialize JSON: {e} What it means
Raised inside `load_custom_data`'s row loop when `serde_json::to_vec(&value_json)` fails to re-serialize the JSONB `value` column read from the database. Since the value came from serde_json::Value, this is rare but indicates the row's JSONB content cannot be converted back to bytes (or an internal conversion error).
Source
Thrown at crates/infrastructure/src/sql/queries.rs:1728
WHERE (data_type = $1 OR data_type = $2)
AND metadata = $3
AND identifier = ''
ORDER BY ts_init ASC"#,
)
.bind(type_name)
.bind(short_type)
.bind(&metadata_json)
.fetch_all(pool)
.await
}
}
.map_err(|e| anyhow::anyhow!("Failed to load custom data: {e}"))?;
let mut results = Vec::with_capacity(rows.len());
for row in rows {
let value_json: serde_json::Value = row.try_get("value")?;
let json_bytes = serde_json::to_vec(&value_json)
.map_err(|e| anyhow::anyhow!("Failed to serialize JSON: {e}"))?;
let custom =
CustomData::from_json_bytes(&json_bytes).map_err(|e| anyhow::anyhow!("{e}"))?;
results.push(custom);
}
Ok(results)
}
}
View on GitHub (pinned to 18893faf8b)
Solutions
- Inspect the `{e}` message for the serialization failure detail.
- Re-read the offending row and inspect its `value` JSONB with psql.
- Re-insert the affected row from a known-good source.
- Upgrade the library/serde_json in case of a decoding bug.
Defensive patterns
Strategy: try-catch
Try / catch
let bytes = serde_json::to_vec(&value_json)
.map_err(|e| anyhow::anyhow!("row value not serializable: {e}"))?; Prevention
- Only write rows through the library API to avoid exotic JSONB
- Inspect suspicious rows with psql before retrying
- Pin serde_json versions across the workspace
When it happens
Trigger: Iterating rows returned by load_custom_data where a row's `value` JSONB cannot be serialized to bytes — practically only with unusual JSONB values or an internal bug in the try_get/conversion path.
Common situations: Rows written by other tools with exotic JSONB content; type confusion between the sqlx JSONB decoding and serde_json::Value; corrupted rows.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- Failed to serialize exec algorithm params: {e}
- Failed to serialize order event info: {e}
- Failed to serialize fill info: {e}
- Failed to serialize account event: {e}
- CustomData must be valid JSON: {e}
AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08).
Data as JSON: /api/errors/91a658cd6d2ffeab.
Report an issue: GitHub.