View Source AshJason
Ash resource extension for implementing Jason.Encoder protocol.
Installation
Add to the deps, get deps (mix deps.get), compile them (mix deps.compile).
def deps do
[
{:ash_jason, "~> 0.2"},
]
endUsage
Add AshJason.Extension to extensions list in use Ash.Resource:
defmodule Example.Resource do
use Ash.Resource,
extensions: [Ash.ULID.Extension]
endBy default encodes all non-private fields (attributes/relationships/aggregates/calculations) with loaded non-nil values.
Configuration
For configuration there is an optional jason dsl section:
defmodule Example.Resource do
use Ash.Resource,
extensions: [Ash.ULID.Extension]
jason do
# options
end
endfields
Fields to pick from a record and include in json.
Feilds with values of nil/Ash.NotLoaded/Ash.NotSelected are omitted.
By default includes all public non-sensitive fields (attributes/relationships/aggregates/calculations).
Specifying fields overwrites that default, pick/omit can be used instead to simply modify it.
jason do
fields [:only_some_field]
endpick
Keys to pick from a record in addition to fields.
Can be used to whitelist some private/sensitive attributes or custom non-field properties.
jason do
pick [:additional_key]
endomit
Keys to omit from fields/pick.
Can be used to blacklist some public attributes that get included by default.
jason do
omit [:privatish_key]
endmerge
A map to merge into json.
jason do
merge %{merged_key: "merged_value"}
endcustomize
A function to customize json. Receives a current resulted json map and a source resource record. Returns a modified json map.
jason do
customize fn result, _record ->
result |> Map.put(:custom_key, "custom_value")
end
end