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

  1. Inspect the `{e}` message for the serialization failure detail.
  2. Re-read the offending row and inspect its `value` JSONB with psql.
  3. Re-insert the affected row from a known-good source.
  4. 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

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


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/91a658cd6d2ffeab. Report an issue: GitHub.