kronky
kronky copied to clipboard
protocol Jason.Encoder not implemented for %Kronky.Payload
I tried to use Jason instead of Poison
(Protocol.UndefinedError) protocol Jason.Encoder not implemented for %Kronky.Payload{messages: [%Kronky.ValidationMessage{code: :unknown, field: "base", key: "base", message: "You must login or register to continue.", options: [], template: "You must login or register to continue."}], result: nil, successful: false}, Jason.Encoder protocol must always be explicitly implemented.
If you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:
@derive {Jason.Encoder, only: [....]}
defstruct ...
It is also possible to encode all fields, although this should be used carefully to avoid accidentally leaking private information when new fields are added:
@derive Jason.Encoder
defstruct ...
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:
...
Put this in a module:
require Protocol
Protocol.derive(Jason.Encoder, Kronky.Payload, only: [:messages, :result, :successful])