JSON serialization of Date objects


I have a question about the JSON serialization of JavaScript Date objects:

saving a Date object in the nodeDataArray and serializing it to JSON will persist the date as an obejct:

"birthDate":{"class":"Date", "value":"2017-01-27T16:56:29.301Z"}

while the default JSON.stringify method will produce a simple string value:

See here for a live example: GoJS Sample for JSFIDDLE - JSFiddle - Code Playground

Can I influence how goJS converts to Date to JSON? I would prefer to serialize it to a string instead of an object.
As I am not directly using this value in the goJS diagram (using it in another part of the application) but only want to persist it in the model, I assume I cannot use bindings for this purpose? Is there another way?

I am using goJS 1.7 beta.

Best regards,

Well, if Model.toJson were modified to detect Date objects and automatically just write out a string for their values, how would Model.fromJson know that some strings need to be parsed as Date objects and that most strings should not be parsed that way? Using {"class":"Date", ...} is the only way it has to make sure that it knows the difference between a string or a number and an instance of Date.

So I think you need to change your code to convert Date to string before calling Model.toJson, and then convert those strings (and only those strings) back to Date objects after calling JSON.parse and before calling Model.fromJson not with a string argument.

That makes sense. Thanks for the explanation!