tj / node-querystring

querystring parser for node and the browser - supporting nesting (used by Express, Connect, etc)
MIT License
456 stars 66 forks source link

Added a numeric index in array stringify to preserve reversability #32

Closed yadutaf closed 12 years ago

yadutaf commented 12 years ago

When stringifying this data structure,

{
    name: "answer #2",
    choices: [
        {propositionid:'4f47bfe1bc9dd1432900000a', dispo: 'yes'},
            {propositionid:'4f47bfe1bc9dd14329000009', dispo: 'no'}
    ]
}

one gets

name=answer%20%232&choices[][propositionid]=4f47bfe1bc9dd1432900000a&choices[][dispo]=yes&choices[][propositionid]=4f47bfe1bc9dd14329000009&choices[][dispo]=no

As you can notice, there is an ambiguity that prevents the parser from building the data-structure back. When generated this way

name=answer%20%232&choices[0][propositionid]=4f47bfe1bc9dd1432900000a&choices[0][dispo]=yes&choices[1][propositionid]=4f47bfe1bc9dd14329000009&choices[1][dispo]=no

Then, everything is alright again. Here is a tentative to fix it.

jeffjo commented 10 years ago

When Rails ActionController parses URL parameters, it expects arrays to be encoded without indices:

qs.stringify({a: [1, 2, 3]}) => a[]=1&a[]=2&a[]=3

Is there any way we can bring this behavior back as an option to stringify()?