marioizquierdo / jquery.serializeJSON

Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.
MIT License
1.71k stars 435 forks source link

Doesn't work with ASP.NET MVC naming conventions #129

Open Merenia opened 10 months ago

Merenia commented 10 months ago

I tried to use this library to serialize a complex form into json to send it as model in my controller but the list items are not serialized correctly.

Name exemple : foo.bar[0].prop Serialization : "foo.bar" : { "0.prop" : "value1", "1.prop" : "value2", "2.prop" : "value3", }

Should be : "foo" : { "bar" : [ {"prop" : "value1"}, {"prop": "value2"}, {"prop": "value3"} ] }

marioizquierdo commented 10 months ago

It seems you expect the plugin to handle a different syntax. This plugin works with the syntax key[nested][nested].

Instead of key.nested.nested:

name="foo.bar.[0].prop"

This plugin uses key[nested][nested]:

name="foo[bar][0][prop]"

In addition, you need to tell the plugin to interpret the numbers as array indexes with the option useIntKeysAsArrayIndex:

$(myform).serializeJSON({useIntKeysAsArrayIndex: true});

This will produce the expected serialization.

Is is possible to serialize the with the dot syntax key.nested.nested?

Currently not. But I am open to a pull request suggestion to incorporate this syntax with an option like {dotSyntax: true}. It seems this could be implemented without much effort on the internal method splitInputNameIntoKeysArray. Thanks!

Merenia commented 10 months ago

Hi, I see your point. However, the syntax used by ASP.NET MVC follows a standard used by all C# developers. I think it would be useful to include this support. For your information, I've found another library that supports this syntax. Nevertheless, I'd like to thank you for your work, which has undoubtedly helped a lot of people.

muhlisatac commented 7 months ago

For your information, I've found another library that supports this syntax.

Can you please share which library is it? Couse I have the same issue.

GuillaumeMiet commented 7 months ago

For your information, I've found another library that supports this syntax.

Can you please share which library is it? Couse I have the same issue.

Here is the repo : https://github.com/raphaelm22/jquery.serializeToJSON

muhlisatac commented 7 months ago

https://github.com/raphaelm22/jquery.serializeToJSON

Thx.