jquery.serializeJSON icon indicating copy to clipboard operation
jquery.serializeJSON copied to clipboard

Doesn't work with ASP.NET MVC naming conventions

Open Merenia opened this issue 1 year ago • 5 comments

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"} ] }

Merenia avatar Oct 23 '23 11:10 Merenia

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!

marioizquierdo avatar Oct 24 '23 21:10 marioizquierdo

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.

Merenia avatar Oct 24 '23 21:10 Merenia

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.

muhlisatac avatar Jan 30 '24 15:01 muhlisatac

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

GuillaumeMiet avatar Jan 30 '24 15:01 GuillaumeMiet

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

Thx.

muhlisatac avatar Jan 30 '24 16:01 muhlisatac