JViewer is an excellent way to declaratively represent Elixir data in a JSON encodable format.
Explicitly describe what you want your data to look like in a schema.
defmodule MyApp.DataPresenter do
import JViewer
@return_schema object(
fields: [
field(
key: "id",
type: number()
),
field(
key: "nested_data",
source_key: "data",
type: object(
fields: [
field(
key: "data",
handler: &MyApp.Handlers.data_handler/2
),
...
]
),
...
)
]
)
end
Add dynamic processing using handlers.
defmodule MyApp.DataPresenter do
...
def present_for_client(data, general_handlers_params) do
represent(data, @return_schema, general_handlers_params)
end
end
Get nicely looking data ready to be encoded in JSON!
%{
"id" => 1,
"nested_data" => %{
...
}
}
The package can be installed by adding j_viewer
to your list of dependencies in mix.exs
:
def deps do
[
{:j_viewer, "~> 0.1.5"}
]
end
JViewer's approach and implementation is inspired by an amazing params type validation library Talos.