Просмотр исходного кода

switch to jsone default formatter in 2.9 version

Namdak Tonpa 9 лет назад
Родитель
Сommit
4d943049e3
4 измененных файлов с 17 добавлено и 23 удалено
  1. 1 1
      src/rest.app.src
  2. 7 4
      src/rest.erl
  3. 8 14
      src/rest_cowboy.erl
  4. 1 4
      src/rest_sup.erl

+ 1 - 1
src/rest.app.src

@@ -1,6 +1,6 @@
 {application, rest, [
     {description,  "REST SXC"},
-    {vsn,          "3.0"},
+    {vsn,          "2.9"},
     {applications, [kernel, stdlib, cowboy]},
     {modules, []},
     {registered,   []},

+ 7 - 4
src/rest.erl

@@ -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.
@@ -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).

+ 8 - 14
src/rest_cowboy.erl

@@ -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,
@@ -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}.
@@ -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) ->
@@ -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.

+ 1 - 4
src/rest_sup.erl

@@ -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}, []}}.