Skip to content

Commit

Permalink
Merge pull request #34 from williamthome/feat/formatter
Browse files Browse the repository at this point in the history
Add formatter and bump version
  • Loading branch information
williamthome authored Nov 20, 2023
2 parents 14656b5 + 50d9998 commit 694ab94
Show file tree
Hide file tree
Showing 9 changed files with 1,317 additions and 45 deletions.
59 changes: 56 additions & 3 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 and generator.
An incredibly flexible and performant JSON parser, generator and formatter.

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

Expand Down Expand Up @@ -33,6 +33,10 @@ Like Thoas, both the parser and generator fully conform to
- [Encode](#encode-1)
- [Decode](#decode-1)
- [Resuming](#resuming)
- [Format](#format)
- [Miniffy](#minify)
- [Prettify](#prettify)
- [Custom](#custom)
- [Why Euneus over Thoas?](#why-euneus-over-thoas)
- [Benchmarks](#benchmarks)
- [Encode](#encode-2)
Expand Down Expand Up @@ -60,15 +64,15 @@ Like Thoas, both the parser and generator fully conform to

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

### Elixir

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

Expand Down Expand Up @@ -452,6 +456,55 @@ Euneus permits resuming the decoding when an invalid token is found. Any value c
>
> By using `euneus_decoder:resume/6` the replacement will be the `null_term` option.
### Format

#### Minify

Remove extra spaces and line feeds from JSON, e.g.:

```erlang
1> euneus:minify_to_binary(<<"{\n \"foo\": \"bar\",\n \"baz\": {\n \"foo\": \"bar\",\n \"0\": [\n \"foo\",\n 0\n ]\n }\n}">>).
<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>
```

#### Prettify

Format JSON for printing, e.g.:

```erlang
1> io:format("~s~n", [euneus:prettify(<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>)]).
{
"foo": "bar",
"baz": {
"foo": "bar",
"0": [
"foo",
0
]
}
}
```

#### Custom

Use `euneus:format/2` or `euneus:format_parsed/2` for custom formatting, e.g.:

```erlang
1> Opts = #{spaces => <<$\t>>, indent => <<$\t, $\t>>, crlf => <<$\n>>}.

2> io:format("~s~n", [euneus:format(<<"{\"foo\":\"bar\",\"baz\":{\"foo\":\"bar\",\"0\":[\"foo\",0]}}">>, Opts)]).
{
"foo": "bar",
"baz": {
"foo": "bar",
"0": [
"foo",
0
]
}
}
```

### Why Euneus over Thoas?

`Thoas` is incredible, works performant and perfectly fine, but `Euneus` is more flexible, permitting more customizations, and is more performant than Thoas. See the [benchmarks](#benchmarks).
Expand Down
4 changes: 2 additions & 2 deletions src/euneus.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, euneus, [
{description, "An incredibly flexible and performant JSON parser and generator"},
{vsn, "1.0.3"},
{description, "An incredibly flexible and performant JSON parser, generator and formatter"},
{vsn, "1.1.0"},
{registered, []},
{applications, [
kernel,
Expand Down
Loading

0 comments on commit 694ab94

Please sign in to comment.