sile / jsone

Erlang JSON library
MIT License
291 stars 71 forks source link

Add `jsone:ip_address_to_json_string/1` function #67

Closed sile closed 2 years ago

sile commented 2 years ago

This PR adds a utility function jsone:io_address_to_json_string/1 to encode IP addresses (i.e., inet:ip_addresss() values). This function can be specified as the value of the map_unknown_value encoding option. Please see the doc of the function and the following example for the detail.

> EncodeOpt = [{map_unknown_value, fun jsone:ip_address_to_json_string/1}].

> jsone:encode(#{ip => {127, 0, 0, 1}}, EncodeOpt).
<<"{\"ip\":\"127.0.0.1\"}">>

> {ok, Addr} = inet:parse_address("2001:DB8:0000:0000:0001:0000:0000:0001").
> jsone:encode(Addr, EncodeOpt).
<<"\"2001:db8::1:0:0:1\"">>

> jsone:encode([foo, {0, 0, 0, 0, 0, 16#FFFF, 16#7F00, 16#0001}], EncodeOpt).
<<"[\"foo\",\"::ffff:127.0.0.1\"]">>

Related issue: #49

codecov-commenter commented 2 years ago

Codecov Report

Merging #67 (8176612) into master (6ba5456) will increase coverage by 0.89%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
+ Coverage   90.54%   91.43%   +0.89%     
==========================================
  Files           3        4       +1     
  Lines         296      327      +31     
==========================================
+ Hits          268      299      +31     
  Misses         28       28              
Impacted Files Coverage Δ
src/jsone.erl 68.75% <100.00%> (+2.08%) :arrow_up:
src/jsone_inet.erl 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 6ba5456...8176612. Read the comment docs.