form-serialize
form-serialize copied to clipboard
Arrays of objects
I have something that looks like this:
<input name="foo[0][bar]" value="a"/>
<input name="foo[0][baz]" value="b"/>
<input name="foo[1][bar]" value="c"/>
<input name="foo[1][baz]" value="d"/>
This is being parsed as
{
"foo": {
"0": {"bar": "a", "baz": "b"},
"1": {"bar": "b", "baz": "c"},
}
}
Since the keys are numbers, I would have expected it to parse it as an array, as so:
{
"foo": [
{"bar": "a", "baz": "b"},
{"bar": "b", "baz": "c"},
]
}
I think this is a reasonable setting to use since numeric keys are already used for arrays of strings.
Would a PR for this change be welcome?
Would entertain a PR for this. Also will add that you can post process such objects into arrays in your own code in the event that the default hash behavior is not adequate for your needs. In this case, we had to pick something to support for brackets with a value in them and I think we support array index without additional brackets after (per the readme) but we could make an improvement there.
Yeah, right now I'm post processing the data since I needed it done quickly, but I'll look into it when I have more time next week and get it in so that I won't have to do additional processing for future use cases.
Currently struggling with this same issue.
Not sure I'm up to the task but maybe? But perhaps @RangerMauve has something brewing. I'd be very pleased.
(that having been said... post-proc working fine afaik)
@IanRr I've added a post processing hook in my code, and I happen to rely on JSON-Schema so I know which parts of my form needs to be an array of objects.
Sadly I'm a bit too busy to work on this and actually don't need it for my main projects at the moment. From doing a quick review of the code, the change needs to be done around here where you'd need to set the existing key to an array. Might also need to look around here