View Source JSONOrdered (JSONOrdered v0.1.0)
A simple struct and encoder for the Elixir 1.18+ JSON module that allows you to encode JSON data with the keys in the specified order.
This is handy for external facing API's where the order of keys makes it easier to reason about the response content.
Usage
Provide the data as a keyword list to JSONOrdered.new/1
:
JSONOrdered.new(a: 1, b: 2, c: 3)
When the JSON module encodes the data, the order of the keys will be preserved:
{
"a": 1,
"b": 2,
"c": 3
}
For example, in a Phoenix controller, you could do something like:
def index(conn, _params) do
conn
|> json(JSONOrdered.new(
id: "123",
name: "John Doe",
email: "john.doe@example.com"
))
end
Summary
Types
@type t() :: %JSONOrdered{data: Keyword.t()}