This is bad. For clients this is not a problem, the parse gonna be the same, but this can generate problems like cache miss for the same request, because each time the body returns different.
While I wholeheartedly agree that the order of JSON attributes shouldn’t be counted on, there are workarounds in cases where something like this is needed.
Since Phoenix uses Poison to encode JSON, one way would be to use a struct and implement the Poison.Encoder protocol for it, then have render return the struct instead of a map:
defmodule Project.OrderedView do
defstruct pagination: %{}, resources: %{}
defimpl Poison.Encoder, for: Project.OrderedView do
def encode(%Project.OrderedView{pagination: p, resources: r}, _options) do
"""
{
"pagination": #{Poison.encode!(p)},
"resources": #{Poison.encode!(r)}
}
"""
end
end
...
def render("index.json", %{resources: resources, pagination: pagination}) do
%Project.OrderedView{
pagination: render_one(...),
resources: render_many(...)
}
end
...
end