Skip to content

Commit 4d38004

Browse files
authored
chore: njson to json. (#99)
1 parent 28be708 commit 4d38004

9 files changed

Lines changed: 40 additions & 41 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ jobs:
1111
strategy:
1212
matrix:
1313
combo:
14-
- otp-version: '25.2'
15-
rebar3-version: '3.23.0'
16-
- otp-version: '26.2'
17-
rebar3-version: '3.23.0'
18-
- otp-version: '27.2'
19-
rebar3-version: '3.24.0'
20-
- otp-version: '28.0'
14+
- otp-version: '27.3'
15+
rebar3-version: '3.25.0'
16+
- otp-version: '28.4'
2117
rebar3-version: '3.25.0'
2218

2319
steps:

‎.github/workflows/docs.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
- main
66

77
env:
8-
OTP-VERSION: 25.2.3
9-
REBAR3-VERSION: 3.20.0
8+
OTP-VERSION: 28.4
9+
REBAR3-VERSION: 3.25.0
1010

1111
jobs:
1212
docs:

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
`erf` is a design-first Erlang REST framework. It provides an interface to spawn specification-driven HTTP servers with several automated features that aim to ease the development, operation and maintenance of design-first RESTful services. Its HTTP protocol features are provided as a wrapper of the [elli](https://github.com/elli-lib/elli) HTTP 1.1 server.
66

7+
Requires Erlang/OTP 27 or later.
8+
79
## What is design-first?
810

911
When following a code-first approach to develop APIs, the interface is produced as a result of the implementation and, therefore, client-side code, integration tests and other parts of the system that depend on the API behaviour, need to wait until the server-side work is done.

‎rebar.config‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
{deps, [
77
{elli, {git, "https://github.com/elli-lib/elli.git", {branch, "main"}}},
8-
{ndto, {git, "https://github.com/nomasystems/ndto.git", {branch, "main"}}},
9-
{njson, {git, "https://github.com/nomasystems/njson.git", {branch, "main"}}}
8+
{ndto, {git, "https://github.com/nomasystems/ndto.git", {branch, "main"}}}
109
]}.
1110

1211
{plugins, [

‎rebar.lock‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,5 @@
88
1},
99
{<<"ndto">>,
1010
{git,"https://github.com/nomasystems/ndto.git",
11-
{ref,"e90a034e0a76deec775abdded2951adbaa54258f"}},
12-
0},
13-
{<<"njson">>,
14-
{git,"https://github.com/nomasystems/njson.git",
15-
{ref,"42ce1fb20f43d50b5b5fdec1f3265b79c51fb632"}},
11+
{ref,"e67f44ac541470ddd650677acc50596060b55301"}},
1612
0}].

‎src/erf.app.src‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{application, erf, [
22
{description, "A design-first Erlang REST Framework"},
3-
{vsn, "0.1.2"},
3+
{vsn, "0.2.0"},
44
{registered, []},
5-
{applications, [kernel, stdlib, compiler, syntax_tools, elli, ndto, njson]},
5+
{applications, [kernel, stdlib, compiler, syntax_tools, elli, ndto]},
66
{optional_applications, [
77
telemetry
88
]},

‎src/erf.erl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
%%% TYPES
4444
-type api() :: erf_parser:api().
45-
-type body() :: undefined | njson:t().
45+
-type body() :: undefined | json:decode_value().
4646
-type conf() :: #{
4747
spec_path := binary(),
4848
callback := module(),

‎src/erf_parser/erf_parser_oas_3_0.erl‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
resolved := [binary()],
3232
spec := spec()
3333
}.
34-
-type spec() :: njson:t().
34+
-type spec() :: json:decode_value().
3535

3636
%%% MACROS
3737
-define(METHODS, [
@@ -486,10 +486,11 @@ parse_spec(SpecPath) ->
486486
{ok, BinSpec} when is_binary(BinSpec), byte_size(BinSpec) > 0 ->
487487
case filename:extension(SpecPath) of
488488
JSON when JSON =:= <<".json">> orelse JSON =:= ".json" ->
489-
case njson:decode(BinSpec) of
490-
{ok, Spec} ->
491-
{ok, Spec};
492-
{error, Reason} ->
489+
try json:decode(BinSpec) of
490+
Spec ->
491+
{ok, Spec}
492+
catch
493+
error:Reason ->
493494
{error, {invalid_json, Reason}}
494495
end;
495496
Extension ->

‎src/erf_router.erl‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ handle(Name, RawRequest) ->
132132
postprocess(InitialRequest, Response);
133133
{error, _Reason} ->
134134
ContentTypeHeader = string:casefold(<<"content-type">>),
135-
{ok, ErrorBody} = njson:encode(#{
136-
<<"title">> => <<"Bad Request">>,
137-
<<"status">> => 400,
138-
<<"detail">> => <<"Failed to read request">>
139-
}),
135+
ErrorBody = iolist_to_binary(
136+
json:encode(#{
137+
<<"title">> => <<"Bad Request">>,
138+
<<"status">> => 400,
139+
<<"detail">> => <<"Failed to read request">>
140+
})
141+
),
140142
{400, [{ContentTypeHeader, <<"application/json">>}], ErrorBody}
141143
end.
142144

@@ -889,19 +891,21 @@ postprocess(_Request, {Status, RawHeaders, RawBody}) ->
889891
undefined ->
890892
{Status, RawHeaders, []};
891893
_RawBody ->
892-
case njson:encode(RawBody) of
893-
{ok, EncodedBody} ->
894+
try iolist_to_binary(json:encode(RawBody)) of
895+
EncodedBody ->
894896
Headers = [{ContentTypeHeader, <<"application/json">>} | RawHeaders],
895-
{Status, Headers, EncodedBody};
896-
{error, _Reason} ->
897+
{Status, Headers, EncodedBody}
898+
catch
899+
error:_ ->
897900
{Status, [{ContentTypeHeader, <<"text/plain">>} | RawHeaders], RawBody}
898901
end
899902
end;
900903
<<"application/json">> ->
901-
case njson:encode(RawBody) of
902-
{ok, EncodedBody} ->
903-
{Status, RawHeaders, EncodedBody};
904-
{error, _Reason} ->
904+
try iolist_to_binary(json:encode(RawBody)) of
905+
EncodedBody ->
906+
{Status, RawHeaders, EncodedBody}
907+
catch
908+
error:_ ->
905909
% TODO: handle error
906910
{500, [{ContentTypeHeader, <<"text/plain">>}], <<"Internal Server Error">>}
907911
end;
@@ -922,10 +926,11 @@ preprocess(RawRequest) ->
922926
<<"application/json">> ->
923927
case RawBody of
924928
NonEmptyBinary when is_binary(NonEmptyBinary), byte_size(NonEmptyBinary) > 0 ->
925-
case njson:decode(RawBody) of
926-
{ok, Body} ->
927-
{ok, RawRequest#{body => Body}};
928-
{error, Reason} ->
929+
try json:decode(RawBody) of
930+
Body ->
931+
{ok, RawRequest#{body => Body}}
932+
catch
933+
error:Reason ->
929934
{error, {cannot_decode_body, Reason}}
930935
end;
931936
_RawBody ->

0 commit comments

Comments
 (0)