3

When I have an html input field like this:

<input name="user[name]" value="peter">
<input name="user[emails][]" value="peter@example.com">
<input name="user[emails][]" value="peter_2@example.com">

It gets deserialized in a Ruby on Rails app to a hash like this:

{ "user" => { "name" => "peter", "emails" => ["peter@example.com", "peter_2@example.com"] } }

A PHP application (and possibly many more frameworks) will deserialize this in a similar way. I understand the process is also called demarshalling.

The browser will send this data in the application/x-www-form-urlencoded encoding, but that is not the level at which I want to understand this format, it seems more like a convention to indicate to the server how the data transmitted by the browser is to be parsed.

Here is my question:

What is the name of this serialization / marshalling format? Is it specified anywhere?

Edit:

I did a little digging. In rubyland this is handled by the parse_nested_query method of the Rack::QueryParser in the rack stack.

eikes
  • 4,811
  • 2
  • 31
  • 31
  • Is it OK if we focus on just the first question, "What is the name of this .. format"? – Jared Beck Sep 08 '21 at 16:23
  • https://stackoverflow.com/questions/383692/what-is-json-and-what-is-it-used-for – aynber Sep 08 '21 at 16:26
  • 1
    As to what the name is, I don't think it has a specific name, at least as far as PHP is concerned. `[]` just means array, so this is a [hint to PHP](https://www.php.net/manual/en/faq.html.php#faq.html.arrays) that it should create an array instead of just a string. Also, I don't really think it is marshalling directly, it is [really just a hint](https://edgeguides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters), so maybe a deserialization hint if you are really looking to call it something. – Chris Haas Sep 08 '21 at 17:17
  • 1
    @aynber it is not JSON – eikes Sep 08 '21 at 17:55
  • @JaredBeck sure, let's focus on the name – eikes Sep 08 '21 at 17:56
  • @ChrisHaas Thank, you. Yes, those are the correct places in the documentation. My question remains though: what is the name of this format? And additionally: is it specified anywhere? – eikes Sep 08 '21 at 18:01
  • Note to self: express.js supports this via qs https://github.com/ljharb/qs – eikes Sep 08 '21 at 19:58
  • @eikes, it is specified in those two links, what more are you looking for as far as "specification"? Use empty brackets for an array, and put text in for a hash. If you are looking for the literal code, I'm [95% sure it is here](https://github.com/rails/rails/blob/main/actionpack/lib/action_dispatch/request/utils.rb) although I don't read Ruby that well. And like I said, I don't think it has a name. It isn't a "format", either, just a pattern or convention. – Chris Haas Sep 08 '21 at 20:51
  • @ChrisHaas The ruby parser is here: https://github.com/rack/rack/blob/master/lib/rack/query_parser.rb – eikes Sep 08 '21 at 21:05
  • Its a pattern for parsing key/value pairs (like those used in formData) into higher level structures like arrays and dictionaries that most likely predates both Ruby and PHP and harkens back to the first CGI interfaces and has countless variants. I have never actually seen any attempt to formalize it as each CGI is mapping it into whatever is relevant for that language. PHP for example only has garbage for a type system so `foo[bar]=baz` is an array while it would be a Hash in Ruby and a Dictionary in Python. – max Sep 09 '21 at 18:57
  • It has been asked before: https://stackoverflow.com/questions/30261513/is-there-a-spec-for-nested-urlencoded-params – eikes Nov 16 '21 at 13:07

0 Answers0