anchor.diff:             end,
       {Html, Req, State}.
   
  -content_types_accepted(Req, State) -> {[{<<"application/x-www-form-urlencoded">>, handle_urlencoded_data}], Req, State}.
  +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(n2o_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}.
   
   handle_urlencoded_data(Req, #st{resource_module = M, resource_id = Id} = State) ->
  -    {ok, Binary, Req2} = cowboy_req:body_qs(Req),
  -    Data = format_data(Binary),
  -    Processable = case erlang:function_exported(M, data_processable, 2) of
  -                      true  -> M:data_processable(Id, Data);
  -                      false -> true
  -                  end,
  -    {case Processable of
  -         true  -> case Id of
  -                      undefined -> M:post(Data);
  -                      _         -> M:put(Id, Data)
  -                  end;
  -         false -> false
  -     end, Req2, State}.
  +    {ok, Data, Req2} = cowboy_req:body_qs(Req),
  +    {handle_data(M, Id, Data), Req2, State}.
   
  -delete_resource(Req,  #st{resource_module = M, resource_id = Id} = State) ->
  -    M:delete(Id),
  -    {true, Req, State}.
  +handle_json_data(Req, #st{resource_module = M, resource_id = Id} = State) ->
  +    {ok, Binary, Req2} = cowboy_req:body(Req),
  +    Data = case n2o_json:decode(Binary) of {struct, Struct} -> Struct; _ -> [] end,
  +    {handle_data(M, Id, Data), Req2, State}.
   
  -default_html_layout(Body) -> [<<"<html><body>">>, Body, <<"</body></html>">>].
  -format_data(Data) -> [{binary_to_list(Key), binary_to_list(Value)} || {Key, Value} <- Data].
  +handle_data(Mod, Id, Data) ->
  +    Valid = case erlang:function_exported(Mod, validate, 2) of
  +                true  -> Mod:validate(Id, Data);
  +                false -> default_validate(Mod, Id, Data)
  +            end,
  +    case Valid of
  +        true  -> case Id of undefined -> Mod:post(Data); _ -> Mod:put(Id, Data) end;
  +        false -> false
  +    end.
   
  -format_data(Data) -> [{binary_to_list(Key), binary_to_list(Value)} || {Key, Value} <- Data].
  +default_validate(Mod, Id, Data) ->
  +    Allowed = case erlang:function_exported(Mod, keys_allowed, 1) of
  +                  true  -> Mod:keys_allowed(proplists:get_keys(Data));
  +                  false -> true
  +              end,
  +    validate_match(Mod, Id, Allowed, proplists:get_value(<<"id">>, Data)).
  +
  +validate_match(_Mod, undefined, true, undefined)                  -> false;
  +validate_match(_Mod, undefined, true, NewId) when NewId == <<"">> -> false;
  +validate_match( Mod, undefined, true, NewId)                      -> not Mod:exists(NewId);
  +validate_match(_Mod,       _Id, true, undefined)                  -> true;
  +validate_match( Mod,        Id, true, NewId) when Id =/= NewId    -> not Mod:exists(NewId);
  +validate_match(   _,         _,    _, _)                          -> false.
  +
  +delete_resource(Req,  #st{resource_module = M, resource_id = Id} = State) -> {M:delete(Id), Req, State}.
   
   rest_module(Module) when is_binary(Module) -> rest_module(binary_to_list(Module));
   rest_module(Module) ->
  
positive.code: -author('Dmitry Bushmelev').
  -record(st, {resource_module = undefined :: atom(), resource_id = undefined :: binary()}).
  -export([init/3, rest_init/2, resource_exists/2, allowed_methods/2, content_types_provided/2,
           to_html/2, content_types_accepted/2, handle_urlencoded_data/2, delete_resource/2]).
  
  init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
  
  
negative.code: allowed_methods(Req, #st{resource_id = undefined} = State) -> {[<<"GET">>, <<"POST">>], Req, State};
  allowed_methods(Req, State)                                -> {[<<"GET">>, <<"PUT">>, <<"DELETE">>], Req, State}.
  
  content_types_provided(Req, State) -> {[{<<"text/html">>, to_html}, {<<"application/json">>, to_json}], Req, State}.
  
  to_html(Req, #st{resource_module = M, resource_id = Id} = State) ->
      Body = case Id of
  