poison
poison copied to clipboard
Allow decoder as option to be a function
This PR adds a change that allows the option as value to be a function which is useful when you want decode to structs dynamically based on the json received:
person = ~s({"name":"Devin Torres"})
contact = ~s({"email":"[email protected]"})
as = fn
%{"name" => _name} -> %Person{}
%{"email" => _email} -> %Contact{}
end
Poison.decode!(person, %{as: as})
# %Person{name: "Devin Torres"}
Poison.decode!(contact, %{as: as})
# %Contact{email: "[email protected]"}