View Source JSON.Encoder protocol (Elixir v1.18.0)
A protocol for custom JSON encoding of data structures.
If you have a struct, you can derive the implementation of this protocol by specifying which fields should be encoded to JSON:
@derive {JSON.Encoder, only: [....]}
defstruct ...
It is also possible to encode all fields or skip some fields via the
:except
option:
@derive JSON.Encoder
defstruct ...
Leaking Private Information
The :except
approach should be used carefully to avoid
accidentally leaking private information when new fields are added.
Finally, if you don't own the struct you want to encode to JSON,
you may use Protocol.derive/3
placed outside of any module:
Protocol.derive(JSON.Encoder, NameOfTheStruct, only: [...])
Protocol.derive(JSON.Encoder, NameOfTheStruct)
Summary
Types
@type t() :: term()
All the types that implement this protocol.
Functions
A function invoked to encode the given term to iodata/0
.