- When you need a computed field, you actually need a function.
- Functions are not data, so they should not reside in structs.
- If you only want that
formatted_sell_pricein JSON serialization, you can define your own impl ofJason.Encoderfor yourItemstructs:
defimpl Jason.Encoder, for: Item do
def encode(%Item{sell_price: price} = item, opts) do
item
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.put(:formatted_sell_price, format_price(price))
|> Jason.Encode.map(opts)
end
end
You need to replace Item with your schema module, and format_price with your own function of formatting prices.






















