Agent Wranglers

Data shapes Required

Convert to arrays and JSON only at the edge

Keep typed objects inside the app. Turn them into arrays, JSON or strings only where data leaves, such as an HTTP response or a database write.

How to do it

Never call toArray(), json_encode and decode, or JSON.parse(JSON.stringify()) to pass data between your own functions. That throws away the types and lets keys drift. Put each conversion in one serializer at the boundary.

Example

// Bad: the DTO becomes a stringly-typed array inside the app
$this->mailer->send($order->toArray());

// Good: pass the object, convert only in the HTTP response
$this->mailer->send($order);
return new JsonResponse($this->orderSerializer->toJson($order));
# Bad
mailer.send(order.to_dict())

# Good: keep the object, serialise only in the response
mailer.send(order)
return JSONResponse(order_serializer.to_json(order))

How to check

A grep or linter rule fails on toArray(), JSON.parse(JSON.stringify( and json_decode(json_encode( outside the serializer and controller folders. In review, no internal function takes or returns an array made from a DTO.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/data-shapes/convert-at-the-edge.