macek/jquery-serialize-object

Support "dot" notation for object property names

macgyver opened this issue · 11 comments

This is a rad plugin! I personally think it would be cool if it also supported dot notation, e.g:

<input name="dream.as.vividly.as.you.can" value="g">

would parse the same as:

<input name="dream[as][vividly][as][you][can]" value="g">

I don't know of any web framework that currently supports inputs like that, but you're more than welcome to add the feature.

If you do choose to add this feature, I think it should be done by passing options object to the plugin. For example:

$(sel).serializeObject({nesting: "dot"});

Where default options would be:

$.fn.serializeObject.defaults = {
  nesting: "brackets"
};

I see your point - there are probably even lots of frameworks that would break if the inputs were parsed in the way I suggest (it rules out allowing periods in your input names) - then again, those frameworks are probably not designed to work with input nested in this manner in any case.

I'll let you know if I get anywhere with this, thanks for the input!

@macgyver i just wanted to follow up and let you know some things have changed that can support the dot notation now.

Please see this comment from another issue.

Also, please see the update API documentation. I've included this example to support dot notation.

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i
});

Here's a fiddle demo

.net framework mvc uses dots as an indicator of an object
name="a.b" would mean
someObject = {a:{b: the value of a.b }}

@AndersMadsen thanks for the comment. I wasn't sure where people would prefer the dot notation.

Are you having issues getting dot notation working with your .NET project?

@macek I'm not working on that project right now, I'm sitting on php, however I made an issue which got merged with this one (#36) I think that because of the size of .net mvc it would make sense to turn on object creation by dots as an option :).

@AndersMadsen It's not clear whether you're actually having an issue or just making a comment.

"dot" notation is supported as an "option" and is working as tested.

Please let me know if you're having trouble getting this working on your own project.

@macek sorry for being unclear, I was having an issue when i was using it that it did not create any object at all (if you look at the issue I linked) it probably will with the dot-notation. What I was thinking about was to submit hyphen and dots as options instead of regular expressions, but still keeping the flexible reg-ex option, like

$.extend(FormSerializer.patterns, {
  validate-notation: '.' 
});

The reason I'm suggesting a feature like this is for readability, I'm not a reg-ex shark (yet) so In my case I'll have read the reg-ex thoroughly before I understand what it does.

.NET Razor engine creates lists using dot notation. The Razor serialization engine is JSON.NET - So I think this support would be great here because I am working on WebForms with MVC 5 knowledge. So building lists on the client with dot notation and submiting to a webform based on JSON.NET. Its no problem I can use the syntax as provided to build the object but just wanted to let you know that there is a major framework that uses dot notation. The fact you dont know about it is because people that use MVC do not need this plugin because its all extended in the framework and works like dream.

Basically retro fitting projects like I am doing.. it would be nice to stay inline with .NET notations. Because using partial post backs and all that other form crap is a nightmare and this plugin is the closest thing to emulate the Razor implemented convention.

Thanks for interesting plugin.

Basically the formatter provider does not work. THis is how I name my classes for the default .NET Serializes to deserialize to a LIST of Entities in psuedo. Basiclly on load there may be bound data but you change the values with jQuery as the user goes along.

<foreach>
 <input type="hidden" name="SomeName[<%= index %>][ID]" value="<%=index.ID %>"/>
 <input type="hidden" name="SomeName[<%= index %>][Address]" value="<%=index.Address %>" />
<end foreach>

Then in webforms its as simple as. Notice "SomeName" is the variable here not the entity type!

  [WebMethod]
  public static void MyMethod(List<Data.MyEntityMOdel>  SomeName)
  {
          int blah = SomeName.Count(); 
   }

@macgyver i just wanted to follow up and let you know some things have changed that can support the dot notation now.

Please see this comment from another issue.

Also, please see the update API documentation. I've included this example to support dot notation.

$.extend(FormSerializer.patterns, {
  validate: /^[a-z][a-z0-9_]*(?:\.[a-z0-9_]+)*(?:\[\])?$/i
});

Here's a fiddle demo

If you're using compose dot and bracket (like friend[0].nick) Using

$.extend(FormSerializer.patterns, {
    validate: /^[a-z][a-z0-9_]*(?:(?:\.[a-z0-9_]+)|(?:\[[a-z0-9_]+\]))*(?:\[\])?$/i
});

I use it and using the library jsonform (https://jsonform.github.io/jsonform) work together.

Haha, ok thanks for the update ;)