beam-community/stripity-stripe

Missing Jason.Encoder implementation for `Stripe.Customer`

vittoriabitton opened this issue · 1 comments

I'm processing some Stripe Customers using retrieve/2 for a Stripe.Customer and while trying to fetch its attributes I keep getting:

** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for 
%Stripe.Customer{id: "cus_Lt4OfmnftQHhNW", object: "customer", address: nil, balance: 0, created: 1234567890, currency: "usd", default_source: nil, deleted: nil, delinquent: false, description: nil, discount: nil, email: nil, invoice_prefix: "40BEC7C", invoice_settings: %{custom_fields: nil, default_payment_method: nil, footer: nil}, livemode: false, metadata: %{}, name: nil, next_invoice_sequence: 1, payment_method: nil, phone: nil, preferred_locales: [], shipping: nil, sources: nil, subscriptions: nil, tax_exempt: "none", tax_ids: nil, test_clock: nil} 
of type Stripe.Customer (a struct), 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 ...

PS: data from this is using stripe-mock service.

The to-go solution for this would be adding @derive {Jason.Encoder, only: [....]} to stripity-stripe Stripe.Customer model or there is another possible workaround for this?

Thanks in advance :)

You can implement it by yourself, e.g.

defimpl Jason.Encoder, for: Stripe.Customer do
  def encode(struct, opts) do
    # Note that it is just an example and not necessarily correct implementation for your case
    data
    |> Map.from_struct()
    |> Map.drop([:__struct__])
    |> Jason.Encode.map(opts)
  end
end

or derive it using derive/3