Skip to content

Commit a12d054

Browse files
committed
Fix JSON serialization for LogicState objects
1 parent 742ff3f commit a12d054

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

zlayout/component_interface.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
SequentialLogic, Signal, LogicState
1616
)
1717

18+
class ComponentJSONEncoder(json.JSONEncoder):
19+
"""Custom JSON encoder for component objects"""
20+
21+
def default(self, obj):
22+
if isinstance(obj, LogicState):
23+
# Convert LogicState to its name (string representation)
24+
return obj.name
25+
elif isinstance(obj, Enum):
26+
# Handle other enums by converting to their value
27+
return obj.value
28+
elif hasattr(obj, '__dict__'):
29+
# For other complex objects, try to serialize their __dict__
30+
return obj.__dict__
31+
else:
32+
# Let the base class default method raise the TypeError
33+
return super().default(obj)
34+
1835
class ComponentType(Enum):
1936
"""组件类型枚举"""
2037
DATABASE = "database" # 数据库存储的组件
@@ -304,7 +321,7 @@ def export_design(self, filename: str):
304321
}
305322

306323
with open(filename, 'w', encoding='utf-8') as f:
307-
json.dump(design, f, indent=2, ensure_ascii=False)
324+
json.dump(design, f, indent=2, ensure_ascii=False, cls=ComponentJSONEncoder)
308325

309326
def close(self):
310327
"""关闭数据库连接"""

0 commit comments

Comments
 (0)