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

Merged
merged 1 commit into from Feb 24, 2012

3 participants

@yadutaf

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.

@tj tj merged commit 5a989a2 into tj:master Feb 24, 2012
@jeffjo

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()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment