Use when building JSON API endpoints in Hanami 2.x Actions — set `response.format = :json`, use dedicated serializers to encode response bodies, write round-trip serialize→parse tests asserting fields match with `.iso8601` timestamps, include pagination metadata with `{data:, meta:}` shape, return consistent error shapes across endpoints, and rescue `JSON::ParserError` with 400 status. Covers setting content-type headers, serializing Ruby objects to JSON, parsing incoming JSON request bodies, content negotiation, and round-trip parse → serialize → parse verification. Use when you need to render JSON, build a JSON endpoint, create an API controller action, or handle JSON request/response cycles in Hanami 2.x.
Instalación
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Use when building JSON API endpoints in Hanami 2.x Actions — set `response.format = :json`, use dedicated serializers to encode response bodies, write round-trip serialize→parse tests asserting fields match with `.iso8601` timestamps, include pagination metadata with `{data:, meta:}` shape, return consistent error shapes across endpoints, and rescue `JSON::ParserError` with 400 status. Covers setting content-type headers, serializing Ruby objects to JSON, parsing incoming JSON request bodies, content negotiation, and round-trip parse → serialize → parse verification. Use when you need to render JSON, build a JSON endpoint, create an API controller action, or handle JSON request/response cycles in Hanami 2.x.
Use dedicated serializers to encode response bodies.
For implementation details and conventions, see SERIALIZERS.md.
defhandle(request, response)
response.format = :json
user = user_repo.by_id(request.params[:id]).one
response.body = MyApp::Serializers::UserSerializer.new(user).to_json
end
Verify round-trip serialization:
Always assert that a serialized object can be parsed back into equivalent data. Format all date/time fields with .iso8601. If the assertion fails, check that the serializer maps all fields and that timestamps use .iso8601.
# In tests:
serialized = UserSerializer.new(user).to_json
parsed = JSON.parse(serialized, symbolize_names:true)
assert_equal user.email, parsed[:email]
assert_equal user.created_at.iso8601, parsed[:created_at]
# If assertion fails: verify serializer field mapping and timestamp formatting
Handle request body parsing via request.params:
defhandle(request, response)
response.format = :json
attrs = request.params[:user]
result = user_repo.create(attrs)
response.status = 201
response.body = UserSerializer.new(result).to_json
end
Return consistent error shapes:
Ensure all error responses conform to a unified structure: