fix: Convert MySQL Decimal values to float for JSON serialization in ELABDATADISP
MySQL returns numeric values as Decimal objects, which are not JSON serializable. PostgreSQL JSONB requires proper JSON types. Added convert_value() helper in _build_measurement_for_elabdatadisp_node() to: - Convert Decimal → float - Convert str → float - Pass through other types unchanged This ensures all numeric values are JSON-serializable before insertion into the measurements JSONB column. Fixes: "Object of type Decimal is not JSON serializable" error 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -113,9 +113,20 @@ class DataTransformer:
|
||||
Returns:
|
||||
Measurement dictionary for this node with state, calc_err, and measurement fields
|
||||
"""
|
||||
from decimal import Decimal
|
||||
|
||||
# Helper to convert MySQL Decimal to float for JSON serialization
|
||||
def convert_value(val):
|
||||
if isinstance(val, Decimal):
|
||||
return float(val)
|
||||
elif isinstance(val, str):
|
||||
return float(val)
|
||||
else:
|
||||
return val
|
||||
|
||||
measurement = {
|
||||
"state": mysql_row.get("State"),
|
||||
"calc_err": mysql_row.get("calcerr", 0),
|
||||
"calc_err": convert_value(mysql_row.get("calcerr", 0)),
|
||||
}
|
||||
|
||||
# Create nested structure for measurement categories
|
||||
@@ -131,7 +142,7 @@ class DataTransformer:
|
||||
for mysql_col, (category, pg_key) in ELABDATADISP_FIELD_MAPPING.items():
|
||||
value = mysql_row.get(mysql_col)
|
||||
if value is not None:
|
||||
categories[category][pg_key] = float(value) if isinstance(value, str) else value
|
||||
categories[category][pg_key] = convert_value(value)
|
||||
|
||||
# Merge categories into measurement, keeping only non-empty categories
|
||||
for category, values in categories.items():
|
||||
|
||||
Reference in New Issue
Block a user