I am working on a small serialization function which serializes form input to json.
Here is the fiddle: http://jsfiddle.net/jdQfj/2/
I have currently no idea how to split up the array declarations let alone a combination of array and object declaration. However the function works good with infinite object nestings and normal values. But I could believe that there is optimization room.
jQuery.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
// check if object
if (this.name.indexOf('.') !== -1) {
var path = this.name.split('.');
var current = o;
for (var i = 0; i < path.length; i++) {
if (i === (path.length - 1)) {
current[path[i]] = this.value;
} else {
if (current[path[i]] === undefined) {
current[path[i]] = {};
}
}
current = current[path[i]];
}
// check if array
} else if (this.name.indexOf('[') !== -1 && this.name.indexOf(']')) {
console.log(this.name + ' is an array');
// has to get implmented
// normal value
} else {
o[this.name] = this.value;
}
});
return o;
};