sile / jsone

Erlang JSON library
MIT License
291 stars 71 forks source link

Add `map_unknown_value` encoding option #51

Closed sile closed 3 years ago

sile commented 3 years ago

This PR adds map_unknown_value encoding option. By this option, we can encode values that aren't supported by default as follows:

> IpAddr = {127, 0, 0, 1}.
> MapFun = fun ({_,_,_,_} = Ip4) -> {ok, list_to_binary(inet:ntoa(Ip4))}; (_) -> error end.
> jsone:encode(#{<<"ipaddr">> => IpAddr}, [{map_unknown_value, MapFun}]).
<<"{\"ipaddr\":\"127.0.0.1\"}">>

See also https://github.com/sile/jsone/pull/49