开发者

Where did this Ruby parameter convention come from?

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 nes开发者_运维百科ted 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.)


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜