visionmedia/node-querystring — pull request #27 by kainosnoema (2011-12-18 00:13:53+00:00)
Title: Handle partially parsed objects (w/ querystrings in keys)

## tl;dr ##

- { 'foo[bar]': { qux: 0 }, 'foo': { baz: 1 } } => { foo: { bar: { qux: 0 }, baz: 1 } }
- previously this would work in some cases, this fix handles most cases properly
- allows for handling of pre-parsed multipart forms from felixge/node-formidable
- only added a few lines plus tests, everything passes

## Why? ##

For multipart forms, particularly from node-formidable. This particular use case is important:

**We can use mimetypes to parse some data from node-formidable Parts before using qs**

````js

// Raw POST body:
//
// --xxxxxxxx
// Content-Disposition: form-data; name="foo"
// Content-Type: application/json
//
// {"qux": "baz"}
// --xxxxxxxx
// Content-Disposition: form-data; name="foo[bar]"
// Content-Type: image/jpeg
//
// (... image data ...)
// --xxxxxxxx--

// Simplified example:

> console.log(qs.parse({
> foo: JSON.parse("{\"qux\": \"baz\"}")
> , 'foo[bar]': [object Object] // JPEG data in Formidable.File object
> }));
>
{
foo: {
qux: "baz",
bar: [object Object] // JPEG data in Formidable.File object
}
}
````

## Does we really need this? ##

Yes. The RestKit iOS framework sends multipart forms containing both `application/json` and attachment (ie. `image/jpeg`) data. Properly merging the parts back into a single params object is crucial. The ExpressJS body-parser got us most of the way there (although we did have to override `handlePart()` so we could parse the JSON based on the mimetype).

The final piece of the puzzle for us is getting qs to reassemble the parts from the querystring paths as expected. As mentioned in the commit message, qs was doing this properly in some cases, but not all (depending on part order from node-formidable, which is apparently non-deterministic). This fix should handle most cases as expected.