I am trying to serialize a form into a JSON object in such a way that i can send the data via AJAX. I'm using the function below:
$.fn.serializeObject = function() {
var arrayData, objectData;
arrayData = this.serializeArray();
objectData = {};
$.each(arrayData, function() {
var value;
if (this.value != null && this.value != '') {
value = this.value;
} else {
value = null;
}
if (objectData[this.name] != null) {
if (!objectData[this.name].push) {
objectData[this.name] = [ objectData[this.name] ];
}
objectData[this.name].push(value);
} else {
objectData[this.name] = value;
}
});
return objectData;
};
The problem is that my serialization does not take into account cyclic data structures. For example i have in my form
<form:input path="discipline.cnfpDisciplineCode" class="required" />
and this gets serialized as
{
...
discipline.cnfpDisciplineCode : someValue
...
}
Is there an elegant solution to serialize the form to make it look like
{
...
discipline :
{
cnfpDisciplineCode : someValue
}
...
}
Or do i have to implement the whole parsing algorithm myself?
Thank you.