Skip to content

Commit

Permalink
switch to jsone default formatter in 2.9 version
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Sep 2, 2015
1 parent 8f5eded commit 4d94304
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/rest.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, rest, [
{description, "REST SXC"},
{vsn, "3.0"},
{vsn, "2.9"},
{applications, [kernel, stdlib, cowboy]},
{modules, []},
{registered, []},
Expand Down
11 changes: 7 additions & 4 deletions src/rest.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-module(rest).
-author('Dmitry Bushmelev').
-export([behaviour_info/1, parse_transform/2, generate_to_json/3, generate_from_json/3, from_json/1, to_json/1]).
-export([behaviour_info/1, parse_transform/2, generate_to_json/3,
generate_from_json/3, from_json/1, to_json/1, to_binary/1]).

behaviour_info(callbacks) -> [{exists, 1}, {get, 0}, {get, 1}, {post, 1}, {delete, 1}, {from_json, 2}, {to_json, 1}];
behaviour_info(_) -> undefined.
Expand Down Expand Up @@ -122,19 +123,21 @@ props_skip({Key, Value}, Acc) -> [{Key, from_json(Value)} | Acc].

to_json(Data) ->
case is_string(Data) of
true -> to_binary(Data);
true -> rest:to_binary(Data);
false -> json_match(Data)
end.

json_match([{_, _} | _] = Props) -> [{to_binary(Key), to_json(Value)} || {Key, Value} <- Props];
json_match([{_, _} | _] = Props) -> [{rest:to_binary(Key), to_json(Value)} || {Key, Value} <- Props];
json_match([_ | _] = NonEmptyList) -> [to_json(X) || X <- NonEmptyList];
json_match(Any) -> Any.

is_char(C) -> is_integer(C) andalso C >= 0 andalso C =< 255.

is_string([N | _] = PossibleString) when is_number(N) -> lists:all(fun is_char/1, PossibleString);
is_string(_) -> false.

to_binary(A) when is_atom(A) -> to_binary(atom_to_list(A));
to_binary(A) when is_atom(A) -> atom_to_binary(A,latin1);
to_binary(B) when is_binary(B) -> B;
to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
to_binary(F) when is_float(F) -> float_to_binary(F,[{decimals,9},compact]);
to_binary(L) when is_list(L) -> iolist_to_binary(L).
22 changes: 8 additions & 14 deletions src/rest_cowboy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
to_html/2, to_json/2, content_types_accepted/2, delete_resource/2,
handle_urlencoded_data/2, handle_json_data/2]).

-ifndef(JSON).
-define(JSON, (config(rest,json,n2o_json))).
-endif.

init(_, _, _) -> {upgrade, protocol, cowboy_rest}.

-ifndef(REST_JSON).
-define(REST_JSON, (application:get_env(rest,json,jsone))).
-endif.

rest_init(Req, _Opts) ->
{Resource, Req1} = cowboy_req:binding(resource, Req),
Module = case rest_module(Resource) of {ok, M} -> M; _ -> undefined end,
Expand Down Expand Up @@ -45,9 +45,9 @@ default_html_layout(Body) -> [<<"<html><body>">>, Body, <<"</body></html>">>].

to_json(Req, #st{resource_module = M, resource_id = Id} = State) ->
Struct = case Id of
undefined -> {struct, [{M, [{struct, M:to_json(Resource)} || Resource <- M:get()]}]};
_ -> {struct, M:to_json(M:get(Id))} end,
{iolist_to_binary(?JSON:encode(Struct)), Req, State}.
undefined -> [{M, [ M:to_json(Resource) || Resource <- M:get() ] } ];
_ -> M:to_json(M:get(Id)) end,
{iolist_to_binary(?REST_JSON:encode(Struct)), Req, State}.

content_types_accepted(Req, State) -> {[{<<"application/x-www-form-urlencoded">>, handle_urlencoded_data},
{<<"application/json">>, handle_json_data}], Req, State}.
Expand All @@ -58,7 +58,7 @@ handle_urlencoded_data(Req, #st{resource_module = M, resource_id = Id} = State)

handle_json_data(Req, #st{resource_module = M, resource_id = Id} = State) ->
{ok, Binary, Req2} = cowboy_req:body(Req),
Data = case ?JSON:decode(Binary) of {struct, Struct} -> Struct; _ -> [] end,
Data = case ?REST_JSON:decode(Binary) of {struct, Struct} -> Struct; S -> S end,
{handle_data(M, Id, Data), Req2, State}.

handle_data(Mod, Id, Data) ->
Expand Down Expand Up @@ -104,9 +104,3 @@ rest_module(Module) ->
true = lists:member(rest, proplists:get_value(behaviour, Info)),
{ok, M}
catch error:Error -> {error, Error} end.

config(Key) -> config(rest, Key, "").
config(App,Key) -> config(App,Key, "").
config(App, Key, Default) -> case application:get_env(App,Key) of
undefined -> Default;
{ok,V} -> V end.
5 changes: 1 addition & 4 deletions src/rest_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@
-compile(export_all).

start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->

{ok, {{one_for_one, 5, 10}, []}}.
init([]) -> {ok, {{one_for_one, 5, 10}, []}}.

0 comments on commit 4d94304

Please sign in to comment.