This is a migrated thread and some comments may be shown as answers.

ObservableObject.toJSON does not convert Date objects to JSON

3 Answers 143 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
Dustin
Top achievements
Rank 1
Dustin asked on 31 Jul 2013, 01:39 PM
I have a Model object that needs converted to a JSON object for passing to a WebAPI and when I call the .toJSON method a valid JSON object is returned, but Dates are still in a Date format, which they are not being properly serialized to JSON. So I have for the meantime added a prototype method to the ObservableObject called toJSON2 that checks if the field being processed is a Date object to call the Date objects toJSON method which is a prototype method you guys created anyways. It would be nice though if this one condition to check if the field being processed is a Date to then convert to a JSON date could be added into your source code.

kendo.data.ObservableObject.prototype.toJSON2 = function () {
    var result = {}, value, field;
 
    for (field in this) {
        if (this.shouldSerialize(field)) {
            value = this[field];
 
            if (value instanceof kendo.data.ObservableObject || value instanceof kendo.data.ObservableArray) {
                value = value.toJSON2();
            }
            else if (value instanceof Date) {
                value = value.toJSON();
            }
 
            result[field] = value;
        }
    }
     
    return result;
}


3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 31 Jul 2013, 02:05 PM
Hi Dustin,

The following demo http://jsbin.com/ulubuq/1/edit shows that JSON.stringify correctly serializes JavaScript dates which are part of a kendo observableobject. The toJSON method is used by JSON.stringify to get the "raw" data of an object. Could you please modify that demo to show the actual problem you are dealing with?

Regards,
Atanas Korchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Nathan
Top achievements
Rank 2
answered on 05 Oct 2016, 03:33 PM

Atanas - Dustin is correct.  I ended up here after having the same problem... toJSON DOES NOT WORK.  It does not create valid JSON for date fields.  JSON.stringify(MyObservableObject) does create JSON dates, but you can pass the result through as an MVC model.  toJSON is largely useless.  It might work with some servers, but not with c#/MVC/IIS.

I've ended up using this.Request.Params["DateFieldName"] as parsing that into the model field.

0
Dimiter Topalov
Telerik team
answered on 07 Oct 2016, 10:39 AM
Hi Nathan,

Can you please specify is there any Kendo UI - specific issue, related to the discussed topic, and if so - provide isolated example of what exactly the discrepancy between the expected and the actual behavior is, so we can provide a more to-the-point explanation, if one is needed.

Thank you in advance.

Regards,
Dimiter Topalov
Telerik by Progress
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
Tags
MVVM
Asked by
Dustin
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Nathan
Top achievements
Rank 2
Dimiter Topalov
Telerik team
Share this question
or