feat(kcsolve): JSON serialization for all solver types (Phase 3a) #299
Reference in New Issue
Block a user
Delete Branch "feat/kcsolve-serialization"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Phase 3a of the solver server integration chain (ref #289). Adds
to_dict()/from_dict()JSON serialization to all KCSolve pybind11 types, enabling SolveContext and SolveResult transport as JSON between the Create client, Silo server, and solver runners.Changes
src/Mod/Assembly/Solver/bindings/kcsolve_py.cpp(+754 lines)enum_to_str()/str_to_enum()helpersto_dict()/from_dict()conversion functions for all types.def("to_dict")and.def_static("from_dict")attached to everypy::class_<>bindingsrc/Mod/Assembly/AssemblyTests/TestKCSolvePy.py(+259 lines)TestKCSolveSerializationclass with 16 testsSerialization format (per SOLVER.md §3)
SolveContext.to_dict()includesapi_version: 1fieldSolveContext.from_dict()validates api_version, raisesValueErroron mismatch.value()names{"position": [x,y,z], "quaternion": [w,x,y,z]}simulationfield serializes asNone/nullpy::dictconstruction — no new dependenciesTests
json.dumps()→json.loads()→from_dict()Build
Compiles clean in release (
cmake --build build/release --target kcsolve_py). All 32 tests pass (16 new + 16 existing).Phase 3a of the solver server integration: add dict/JSON serialization to all KCSolve pybind11 types so SolveContext and SolveResult can be transported as JSON between the Create client, Silo server, and solver runners. Implementation: - Constexpr enum string mapping tables for all 5 enums (BaseJointKind, SolveStatus, DiagnosticKind, MotionKind, LimitKind) with template bidirectional lookup helpers - File-local to_dict/from_dict conversion functions for all 10 types (Transform, Part, Constraint::Limit, Constraint, MotionDef, SimulationParams, SolveContext, ConstraintDiagnostic, SolveResult::PartResult, SolveResult) - .def("to_dict") and .def_static("from_dict") on every py::class_<> binding chain Serialization details per SOLVER.md §3: - SolveContext.to_dict() includes api_version field - SolveContext.from_dict() validates api_version, raises ValueError on mismatch - Enums serialize as strings matching pybind11 .value() names - Transform: {position: [x,y,z], quaternion: [w,x,y,z]} - Optional simulation serializes as None/null - Pure pybind11 py::dict construction, no new dependencies Tests: 16 new tests in TestKCSolveSerialization covering round-trips for all types, all 24 BaseJointKind values, all 4 SolveStatus values, json.dumps/loads stdlib round-trip, and error cases (missing key, invalid enum, bad array length, wrong api_version).