anchor.diff:  %% rewriting dynamically the quey list given as query member in
   %% rewrites. Each value is replaced by one binding or an argument
   %% passed in url.
  -make_query_list([], _Bindings, Acc) ->
  +make_query_list([], _Bindings, _Formats, Acc) ->
       Acc;
  -make_query_list([{Key, {Value}}|Rest], Bindings, Acc) ->
  -    Value1 = to_json({Value}),
  -    make_query_list(Rest, Bindings, [{to_binding(Key), Value1}|Acc]);
  -make_query_list([{Key, Value}|Rest], Bindings, Acc) when is_binary(Value) ->
  -    Value1 = replace_var(Key, Value, Bindings),
  -    make_query_list(Rest, Bindings, [{to_binding(Key), Value1}|Acc]);
  -make_query_list([{Key, Value}|Rest], Bindings, Acc) when is_list(Value) ->
  -    Value1 = replace_var(Key, Value, Bindings),
  -    make_query_list(Rest, Bindings, [{to_binding(Key), Value1}|Acc]);
  -make_query_list([{Key, Value}|Rest], Bindings, Acc) ->
  -    make_query_list(Rest, Bindings, [{to_binding(Key), Value}|Acc]).
  -
  -replace_var(Key, Value, Bindings) ->
  -    case Value of
  -        <<":", Var/binary>> ->
  -            get_var(Var, Bindings, Value);
  -        _ when is_list(Value) ->
  -            Value1 = lists:foldr(fun(V, Acc) ->
  -                V1 = case V of
  -                    <<":", VName/binary>> ->
  -                        case get_var(VName, Bindings, V) of
  -                            V2 when is_list(V2) ->
  -                                iolist_to_binary(V2);
  -                            V2 -> V2
  -                        end;
  -                    _ ->
  -                        
  -                        V
  -                end,
  -                [V1|Acc]
  -            end, [], Value),
  -            to_json(Value1);
  -        _ when is_binary(Value) ->
  -            Value;
  +make_query_list([{Key, {Value}}|Rest], Bindings, Formats, Acc) ->
  +    Value1 = {Value},
  +    make_query_list(Rest, Bindings, Formats, [{to_binding(Key), Value1}|Acc]);
  +make_query_list([{Key, Value}|Rest], Bindings, Formats, Acc) when is_binary(Value) ->
  +    Value1 = replace_var(Value, Bindings, Formats),
  +    make_query_list(Rest, Bindings, Formats, [{to_binding(Key), Value1}|Acc]);
  +make_query_list([{Key, Value}|Rest], Bindings, Formats, Acc) when is_list(Value) ->
  +    Value1 = replace_var(Value, Bindings, Formats),
  +    make_query_list(Rest, Bindings, Formats, [{to_binding(Key), Value1}|Acc]);
  +make_query_list([{Key, Value}|Rest], Bindings, Formats, Acc) ->
  +    make_query_list(Rest, Bindings, Formats, [{to_binding(Key), Value}|Acc]).
  +
  +replace_var(<<"*">>=Value, Bindings, Formats) ->
  +    get_var(Value, Bindings, Value, Formats);
  +replace_var(<<":", Var/binary>> = Value, Bindings, Formats) ->
  +    get_var(Var, Bindings, Value, Formats);
  +replace_var(Value, _Bindings, _Formats) when is_binary(Value) ->
  +    Value;
  +replace_var(Value, Bindings, Formats) when is_list(Value) ->
  +    lists:reverse(lists:foldl(fun
  +                (<<":", Var/binary>>=Value1, Acc) ->
  +                    [get_var(Var, Bindings, Value1, Formats)|Acc];
  +                (Value1, Acc) ->
  +                    [Value1|Acc]
  +            end, [], Value));
  +replace_var(Value, _Bindings, _Formats) ->
  +    Value.
  +
  +maybe_json(Key, Value) ->
  +    case lists:member(Key, [<<"key">>, <<"startkey">>, <<"start_key">>,
  +                <<"endkey">>, <<"end_key">>, <<"keys">>]) of
  +        true ->
  +            ?JSON_ENCODE(Value);
  +        false ->
  +            Value
  +    end.
  +
  +get_var(VarName, Props, Default, Formats) ->
  +    VarName1 = to_binding(VarName),
  +    Val = couch_util:get_value(VarName1, Props, Default),
  +    maybe_format(VarName, Val, Formats).
  +
  +maybe_format(VarName, Value, Formats) ->
  +    case couch_util:get_value(VarName, Formats) of
  +        undefined ->
  +             Value;
  +        Format ->
  +            format(Format, Value)
  +    end.
  +
  +format(<<"int">>, Value) when is_integer(Value) ->
  +    Value;
  +format(<<"int">>, Value) when is_binary(Value) ->
  +    format(<<"int">>, ?b2l(Value));
  +format(<<"int">>, Value) when is_list(Value) ->
  +    case (catch list_to_integer(Value)) of
  +        IntVal when is_integer(IntVal) ->
  +            IntVal;
           _ ->
  -            case Key of
  -                <<"key">> -> to_json(Value);
  -                <<"startkey">> -> to_json(Value);
  -                <<"endkey">> -> to_json(Value);
  -                _ ->
  -                    lists:flatten(?JSON_ENCODE(Value))
  -            end
  -    end.
  -
  -
  -get_var(VarName, Props, Default) ->
  -    VarName1 = to_binding(VarName),
  -    couch_util:get_value(VarName1, Props, Default).
  +            Value
  +    end;
  +format(<<"bool">>, Value) when is_binary(Value) ->
  +    format(<<"bool">>, ?b2l(Value));
  +format(<<"bool">>, Value) when is_list(Value) ->
  +    case string:to_lower(Value) of
  +        "true" -> true;
  +        "false" -> false;
  +        _ -> Value
  +    end;
  +format(_Format, Value) ->
  +   Value.
   
   %% doc: build new patch from bindings. bindings are query args
   %% (+ dynamic query rewritten if needed) and bindings found in
  
positive.code:         Rules ->
              % create dispatch list from rules
              DispatchList =  [make_rule(Rule) || {Rule} <- Rules],
  
              %% get raw path by matching url to a rule.
              RawPath = case try_bind_path(DispatchList, couch_util:to_binary(Method), PathParts,
                                      QueryList1) of
                  no_dispatch_path ->
                      throw(not_found);
                  {NewPathParts, Bindings} ->
  
negative.code:                     Bindings1 = Bindings ++ QueryList,
                      % we parse query args from the rule and fill
                      % it eventually with bindings vars
                      QueryArgs1 = make_query_list(QueryArgs, Bindings1,
                          Formats, []),
                      % remove params in QueryLists1 that are already in
                      % QueryArgs1
                      Bindings2 = lists:foldl(fun({K, V}, Acc) ->
  