Incorrect JSON serialization of false/zero/null values


I have a couple of boolean userdata fields and an enum-based integer field in my scheduler and I need to transmit those values to the server. I am using scheduler.toJSON() for that purpose.

Unfortunately, my server deserialization fails because zeros and false always appear to be serialized as empty strings as a result to calling scheduler.toJSON().

I believe that the problem in the dhtmlxsheduler_serialize plugin is in line #45:

line.push(' "'+attrs[i][0]+'": "'+((attrs[i][1]?attrs[i][1](ev[attrs[i][0]]):ev[attrs[i][0]])||"").toString().replace(/\n/g,"")+'" ');

The one-liner is terrible to debug and make modifications to.
As a quick fix, I ended up using JSON.stringify() like so:

line.push(' "' + attrs[i][0] + '": "' + ((attrs[i][1] ? attrs[i][1](ev[attrs[i][0]]) : JSON.stringify(ev[attrs[i][0]]))).toString().replace(/\n/g, "") + '" ');

I am by no means happy with this solution, but at least it patches up the problem well enough.

Can you revise this line in particular and provide accurate serialization to all values that can be considered “false” (false, zero, null)?

Thank you in advance!

NB: I am using the latest version (4.0) and latest build (130813) of the scheduler.

We will try to include the fix for above problem as part of 4.1 update.

Thank you very much, Stanislav :slight_smile:

1.) for letting me know
2.) for doing so during Christmas

I will post my slightly improved solution which I am using right now in a few minutes.

The somewhat dirty fix I am using right now:

[code]var val = “”;

if (attrs[i][1] !== undefined) {
val = isFunction(attrs[i][1]) ? attrs[i]1 : ev[attrs[i][0]];
val = JSON.stringify(val);

if (val === undefined) {
val = ‘""’;

line.push(’ “’ + attrs[i][0] + '”: ’ + val + ’ ');[/code]

The resulting JSON objects in all situations I tested (bool, string, scheduler dates, numbers and NULLs) are valid in accordance to the JSLint rules.