7

There's a piece of Ruby middleware used by Rails and other frameworks to the parse the parameters you've sent to the server into a nested Hash object.

If you send these parameters to the server:

person[id] = 1
person[name] = Joe Blow
person[email] = joe.blow@test.com
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3

They get parsed to this:

{
  :person => {
    :id => "1",
    :name => "Joe Blow",
    :email => "joe.blow@test.com",
    :address => {
      :street_address => "123 Somewhere St.",
      :city => "Chicago",
      :state => "IL",
      :zip => "12345"
    },
    :other_field => [ 1, 2, 3 ]
  }
}

I believe this is supported by PHP as well. Can anybody tell me what this convention is called, where it came from, and what other languages support it? (Perl, Python, etc.)

Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
  • I've always thought that the "hash syntax" in Ruby is based on Python. – Zabba Oct 01 '11 at 14:55
  • @Zabba perhaps, do you know of any evidence of when it was added to Python? Part of the problem is that I have no idea what search terms to use for this. – Adam Lassek Oct 01 '11 at 19:41

3 Answers3

7

Field research

I'm trying to find out if there's a name for this convention, but I can't find it yet.

Ruby

For what it's worth, the piece of middleware that does this in Ruby is Rack::Utils. See the source on Github.

There is some more information on the subject in the Ruby on Rails Guides.

And here is an interesting ticket about the code being moved from Rails to the Rack middleware.

PHP

I've done some digging in the PHP source, and it seems that all the magic there happens in the main/php_variables.c source file. The SAPI code calls the php_std_post_handler method defined here. This eventually calls the php_register_variable_ex method, which is 185 lines of complex string-parsing in C (I must admit that C isn't my forte).

The only name I could find here was the php_std_post_handler, the PHP standard POST handler.

Python

In Python, this format isn't supported by default. See this question here on stackoverflow.com on the subject.

Perl

The CGI library in Perl doesn't support this format either. It does give easy access to single or multiple values, but not nested values as in your example. See the documentation on feching the value or values of a single named parameter and fetching the parameter list as a hash.

Java

Check out the heated debate on the subject of query parameter parsing in this question. Java doesn't parse this 'nested format' of POST parameters into a data structure by default.

Conclusion

I've looked into this, and haven't found a single name for this way of parameter parsing. Of the languages that I've looked into, only Ruby and PHP support this format natively.

Community
  • 1
  • 1
rdvdijk
  • 4,400
  • 26
  • 30
  • Hmm, one of the commits in that ticket refers to it as a "nested query" but all I get back from Google is SQL stuff. – Adam Lassek Oct 01 '11 at 19:39
  • 1
    There is no "nested format" of POST parameters--there's only POST parameters. Any "nesting" is handled by whatever language/framework is in use. POST parameters are strings, with a name; nesting is a construct layered over that. – Dave Newton Oct 02 '11 at 13:04
  • @DaveNewton: very true. I'll rephrase the sentence. However, the original question is asking which languages also parse the parameters the way Ruby and PHP do. The "nested format" does exist, in those languages at least. – rdvdijk Oct 02 '11 at 13:13
  • Well, I doubt I'm ever going to get a better answer than this. Thanks for the field research! – Adam Lassek Oct 06 '11 at 19:18
2

It's not called anything, AFAIK, other than "sending parameters". If anything, it's called "parameter [type] conversion", where Rails just "converts" it into a hash.

Other frameworks go further, using the parameter names as expressions used to create typed objects initialized with type-converted parameter values.

All parameters are is a string value with a name. Any/all structure is imposed by the language/framework in use on the server side; what it gets transformed to is 100% dependent on that language/framework, and what that conversion consists of would determine what it would be (reasonable) called.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

that would be a JSON object, which is quite standard and supported by most languages/libraries these days.

kennypu
  • 5,950
  • 2
  • 22
  • 28