Background
I have a TypedStruct
with an optional field, :rank
. This field is sometimes present and sometimes not.
@derive Jason.Encoder
typedstruct enforce: true do
@typedoc "An order."
field(:item_id, String.t())
field(:rank, non_neg_integer(), enforce: false)
end
The ideal situation here would be to generate the struct without the field, should the :rank
parameter be missing. However, since I am dealing with structs, this is not possible, so I have to contempt with the nil
value representing the absence of :rank
.
The problem here is that when I use Jason.encode(...)
, I am going to get a "rank": null
in the result json object. I really don’t want this.
Question
Is there an option in Jason that allows me to not encode nil
values, other than manually implementing the Encode
Protocol myself?