kronky icon indicating copy to clipboard operation
kronky copied to clipboard

protocol Jason.Encoder not implemented for %Kronky.Payload

Open kapilpipaliya opened this issue 7 years ago • 1 comments

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:
...

kapilpipaliya avatar Mar 28 '18 03:03 kapilpipaliya

Put this in a module:

require Protocol
Protocol.derive(Jason.Encoder, Kronky.Payload, only: [:messages, :result, :successful])

nlap avatar Jul 25 '18 22:07 nlap