JSON.stringify is ignoring object properties

When an object has its own toJSON() implementation, JSON.stringify() uses the object returned from that method and stringifies that. kendo.data.Model defines it's own toJSON() method which only returns the properties defined on the model, which is why you aren't seeing other values (e.g. dirty, id, uid) in the string result.

"If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation."

Here's an alternative if you must have all properties on the object:

var model = kendo.data.Model.define({
    id: "ID",
    fields: {
        "Name": { type: "string" }
    }
});
var obj = new model();
obj.set("Name","Johhny Foosball");    
var newObj = $.extend({}, obj);
delete newObj.toJSON;
document.write(newObj.dirty);
document.write(JSON.stringify(newObj));

..and the updated fiddle.

Basically I used jQuery.extend to clone the object then deleted the toJSON function. Use at your own risk! :)


If you're working in NodeJS you can use:

import * as util from 'util';
util.inspect(obj)

instead of JSON.stringify