Principle:8beeeaaat Touchdesigner mcp Recursive JSON Serialization
| Knowledge Sources | |
|---|---|
| Domains | Serialization, JSON |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
A recursive serialization strategy that converts arbitrary runtime objects — including framework-specific wrappers, callable-evaluated parameters, and opaque objects — into JSON-safe primitives by walking a priority-ordered dispatch ladder.
Description
TouchDesigner service methods return objects that are not JSON-serializable by default: TD operators (with `.path`), parameter objects (with `.eval()`), Result wrappers, Page objects, and custom classes. The recursive JSON serialization principle defines a dispatch ladder: None → primitives → list/tuple (recurse) → dict (recurse) → objects with `.eval()` (call it, recurse the value) → objects with `.path` (return path string) → `__dict__` (recurse each attribute) → `str(obj)` fallback. The recursion guarantees that arbitrarily nested structures are fully converted, and the fallback ensures no object ever causes a serialization failure.
Usage
Use this principle when a service layer returns framework-specific objects and the HTTP response must be a JSON string, so that the controller can always call json.dumps on the result.
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def safe_serialize(obj):
if obj is None: return None
if isinstance(obj, (int, float, bool, str)): return obj
if isinstance(obj, (list, tuple)): return [safe_serialize(x) for x in obj]
if isinstance(obj, dict): return {str(k): safe_serialize(v) for k, v in obj.items()}
if hasattr(obj, "eval"): return safe_serialize(obj.eval())
if hasattr(obj, "path"): return obj.path
if hasattr(obj, "__dict__"): return {k: safe_serialize(v) for k, v in obj.__dict__.items()}
return str(obj)
Key properties:
- Priority-ordered dispatch: more specific cases checked before generic ones.
- Total recursion: every container type recurses into its children.
- Universal fallback: str(obj) guarantees termination.