Skip to content

Commit

Permalink
Merge pull request #35 from williamthome/feat/drop-nulls
Browse files Browse the repository at this point in the history
Add drop_nulls plugin and bump version
  • Loading branch information
williamthome committed Nov 20, 2023
2 parents 694ab94 + 5db0d2f commit b75ab32
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Euneus

An incredibly flexible and performant JSON parser, generator and formatter.
An incredibly flexible and performant JSON parser, generator and formatter in pure Erlang.

Euneus is a rewrite of [Thoas][thoas].

Expand Down Expand Up @@ -29,6 +29,7 @@ Like Thoas, both the parser and generator fully conform to
- [proplist](#proplist)
- [reference](#reference)
- [timestamp](#timestamp)
- [drop_nulls](#drop_nulls)
- [Differences to Thoas](#differences-to-thoas)
- [Encode](#encode-1)
- [Decode](#decode-1)
Expand Down Expand Up @@ -64,15 +65,15 @@ Like Thoas, both the parser and generator fully conform to

```erlang
% rebar.config
{deps, [{euneus, "1.1.0"}]}
{deps, [{euneus, "1.2.0"}]}
```

### Elixir

```elixir
# mix.exs
def deps do
[{:euneus, "~> 1.1"}]
[{:euneus, "~> 1.2"}]
end
```

Expand Down Expand Up @@ -137,9 +138,12 @@ end
| {16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38} | #{plugins => [inet]} | "3ffe:b80:1f8d:2:204:acff:fe17:bf38" | #{plugins => [inet]} | {16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38} |
| <0.92.0> | #{plugins => [pid]} | "<0.92.0>" | #{plugins => [pid]} | <0.92.0> |
| #Port<0.1> | #{plugins => [port]} | "#Port<0.1>" | #{plugins => [port]} | #Port<0.1> |
| [{foo, bar}] | #{plugins => [proplist]} | {\"foo\":\"bar\"} | #{plugins => [proplist]} | #{<<"foo">> => <<"bar">>} |
| [{foo, bar}] | #{plugins => [proplist]} | {"foo":"bar"} | #{plugins => [proplist]} | #{<<"foo">> => <<"bar">>} |
| #Ref<0.957048870.857473026.108035> | #{plugins => [reference]} | "#Ref<0.957048870.857473026.108035>" | #{plugins => [reference]} | #Ref<0.957048870.857473026.108035> |
| {0,0,0} | #{plugins => [timestamp]} | "1970-01-01T00:00:00.000Z" | #{plugins => [timestamp]} | {0,0,0} |
| #{foo => bar, baz => undefined} | #{plugins => [drop_nulls]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
| [{foo, bar}, {baz, undefined}] | #{plugins => [drop_nulls, proplist]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
| #{foo => bar, baz => undefined, fizz => nil} | #{nulls => [undefined, nil], plugins => [drop_nulls]} | {"foo":"bar"} | #{} | #{<<"foo">> => <<"bar">>} |
| {myrecord, val} | #{unhandled_encoder => fun({myrecord, Val}, Opts) -> <br> euneus_encoder:encode_list([myrecord, #{key => Val}], Opts)<br><br>end}) | ["myrecord", {"key":"val"}] | #{arrays => fun([<<"myrecord">>, #{<<"key">> := Val}], _Opts) -><br> {myrecord, binary_to_atom(Val)}<br>end} | {myrecord, val} |

### Why not more built-in types?
Expand Down Expand Up @@ -285,6 +289,22 @@ Encodes `erlang:timestamp()` to ISO8601 as JSON string and decodes it back, for
{ok,{0,0,0}}
```

#### drop_nulls

Remove keys from maps whose terms are members of the encode 'nulls' option, for example:

```erlang
1> {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined}, #{plugins => [drop_nulls]}).
{ok,<<"{\"a\":1}">>}

1> {ok, JSON} = euneus:encode_to_binary(#{a => 1, b => undefined, c => foo}, #{nulls => [undefined, foo], plugins => [drop_nulls]}).
{ok,<<"{\"a\":1}">>}
```

> [!IMPORTANT]
>
> The `drop_nulls` plugin also works for `proplists`.
## Differences to Thoas

The main difference between `Euneus` to `Thoas` is that Euneus gives more control to encoding or decoding data. All encode functions can be overridden and extended and all decoded data can be overridden and transformed. Also, there is no plugin mechanism in Thoas.
Expand Down Expand Up @@ -575,6 +595,10 @@ euneus:parse_encode_opts(#{
}).
```

> [!IMPORTANT]
>
> The `drop_nulls` plugin was introduced in v1.2.0 and was not included yet in the built-in plugins benchmarks.
It's the slowest euneus encode run, but at the same time it is very efficient.

<div style="overflow-x: auto;">
Expand Down

0 comments on commit b75ab32

Please sign in to comment.