marioizquierdo / jquery.serializeJSON

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

Do not treat semicolon names as type #102

Closed Laykou closed 4 years ago

Laykou commented 4 years ago

E.g. if the name is: article[my:custom::key][active]

marioizquierdo commented 4 years ago

Thanks Laykou for the contribution.

However, I'm not sure changing the regexp would be totally backwards compatible. Maybe someone has a different input name that would not match properly?

Maybe, instead of using a regext, we should just split by ":" and take the last value:

extractTypeAndNameWithNoType: function(name) {
  var parts = name.split(':');
  if (parts.length > 1) {
    var t = parts.pop();
    return {nameWithNoType: parts.join(''), type: t};
  } else {
    return {nameWithNoType: name, type: null};
  }
},

This way, only the last colon counts for the type.

With this, the example you provided article[my:custom::key][active] would not "just work", but it would be easy to add the type explicitly to avoid the other colons to be parsed: article[my:custom::key][active]:string.

What do you think?

Laykou commented 4 years ago

I would then make this configurable. In my use case I cannot add :string suffix to the attribute names in HTML. I have to keep the name as-is.

So then maybe if there would be some global option where this behaviour could be either turn on (default) or off?

marioizquierdo commented 4 years ago

Yes, having an option to disable the type syntax on the field names seems reasonable. Types can also be specified through the data-value-type attribute. With the option to enable/disable, field names, and data attributes, there should be plenty of options for everyone, while keeping things as simple as possible for the common case.

marioizquierdo commented 4 years ago

Fixed on PR #105

You can now use the option {disableSemicolonTypes: true}