williamthome / euneus

An incredibly flexible and performant JSON parser, generator and formatter in pure Erlang.
https://hex.pm/packages/euneus
Apache License 2.0
22 stars 2 forks source link

Add formatter and bump version #34

Closed williamthome closed 9 months ago

williamthome commented 9 months ago

This PR adds format functions via the euneus_formatter module.

There are three major functions implemented:

Examples

Minify

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

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

Format / Custom

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
                                ]
                }
}