[hunchentoot-devel] parameter-type hash-table encoded with [] or {}

Hi, When I post json objects from jquery $.ajax({ type: 'POST', url: '/url', dataType: 'json', data: {"key":"val","hash",{"foo":"bar"}} then the "hash" parameter is posted like this: "hash[foo]=bar" If I then in hunchentoot have (define-easy-handler (fn :uri "/url") ((hash :parameter-type 'hash-table)) ..... The hash is expected to be posted like this: "hash{foo}=bar"? So I wonder if it would be correct to enable the "hash[foo]=bar" syntax for hash tables. Maybe it should be an argument that specifies if compute-hash-table-parameter should use [] or {}. This little experiment worked for me, here I just added \\[ and \\] to the regexp. Should I bother to make a patch? (defun compute-hash-table-parameter (parameter-name type parameters key-type test-function) "Retrieves all parameters from PARAMETERS which are named like \"PARAMETER-NAME{FOO}\" or \"PARAMETER-NAME[FOO]\" \(where FOO is any sequence of characters not containing curly brackets), converts them to TYPE, and returns a hash table with test function TEST-FUNCTION where the corresponding value is associated with the key FOO \(converted to KEY-TYPE)." (let ((hash-table (make-hash-table :test test-function))) (loop for (full-name . value) in parameters for key = (register-groups-bind (name key-string) ("^(.*)[{\\[]([^\\[\\]{}]+)[}\\]]$" full-name) (when (string= name parameter-name) (convert-parameter key-string key-type))) when key do (setf (gethash key hash-table) (convert-parameter value type))) hash-table)) -- Knut Olav Bøhmer

Making this customizable would be OK with me. The only problem I see is that the current pair of delimiters is hard-coded into the regexp and it's probably not a good idea to compute the regexp anew each time the function is called. I also think only paired delimiters like (), [], {}, <> really make sense. Any comments? If you want to have this in Hunchentoot, please send a patch against the diff version following the usual guidelines: http://weitz.de/patches.html Thanks, Edi. 2010/4/23 Knut Olav Bøhmer <bohmer@gmail.com>:
Hi,
When I post json objects from jquery
$.ajax({ type: 'POST', url: '/url', dataType: 'json', data: {"key":"val","hash",{"foo":"bar"}}
then the "hash" parameter is posted like this: "hash[foo]=bar"
If I then in hunchentoot have
(define-easy-handler (fn :uri "/url") ((hash :parameter-type 'hash-table)) .....
The hash is expected to be posted like this: "hash{foo}=bar"? So I wonder if it would be correct to enable the "hash[foo]=bar" syntax for hash tables.
Maybe it should be an argument that specifies if compute-hash-table-parameter should use [] or {}. This little experiment worked for me, here I just added \\[ and \\] to the regexp.
Should I bother to make a patch?
(defun compute-hash-table-parameter (parameter-name type parameters key-type test-function) "Retrieves all parameters from PARAMETERS which are named like \"PARAMETER-NAME{FOO}\" or \"PARAMETER-NAME[FOO]\" \(where FOO is any sequence of characters not containing curly brackets), converts them to TYPE, and returns a hash table with test function TEST-FUNCTION where the corresponding value is associated with the key FOO \(converted to KEY-TYPE)." (let ((hash-table (make-hash-table :test test-function))) (loop for (full-name . value) in parameters for key = (register-groups-bind (name key-string) ("^(.*)[{\\[]([^\\[\\]{}]+)[}\\]]$" full-name) (when (string= name parameter-name) (convert-parameter key-string key-type))) when key do (setf (gethash key hash-table) (convert-parameter value type))) hash-table))
-- Knut Olav Bøhmer
_______________________________________________ tbnl-devel site list tbnl-devel@common-lisp.net http://common-lisp.net/mailman/listinfo/tbnl-devel

On 23 April 2010 22:06, Edi Weitz <edi@agharta.de> wrote:
On Fri, Apr 23, 2010 at 10:05 PM, Edi Weitz <edi@agharta.de> wrote:
If you want to have this in Hunchentoot, please send a patch against the diff version
Cough. The /dev/ version of course... :)
I tried darcs clone http://common-lisp.net/~loliveira/ediware/hunchentoot But how do I know that I have The /dev/ version? -- Knut Olav Bøhmer

2010/4/23 Knut Olav Bøhmer <bohmer@gmail.com>:
I tried darcs clone http://common-lisp.net/~loliveira/ediware/hunchentoot But how do I know that I have The /dev/ version?
svn checkout svn://bknr.net/svn/ediware/ should get you the development version. -Hans
participants (3)
-
Edi Weitz
-
Hans Hübner
-
Knut Olav Bøhmer