appengine_js icon indicating copy to clipboard operation
appengine_js copied to clipboard

toJSON should return an object, not a string

Open bartaz opened this issue 14 years ago • 1 comments

JSON.stringify uses value returned by toJSON and serializes it to string, so it should be an object. Currenty Model objects are double-serialized when using JSON.stringify on them.

Please refer to:

 When an object value is found, if the object contains a toJSON
 method, its toJSON method will be called and the result will be
 stringified. A toJSON method does not serialize: it returns the
 value represented by the name/value pair that should be serialized,
 or undefined if nothing should be serialized. 

https://github.com/douglascrockford/JSON-js/blob/master/json.js#L75-81

bartaz avatar Jun 02 '11 22:06 bartaz

So basically toData method should be called toJSON, and current toJSON is not needed at all (or call it toJSONString)

bartaz avatar Jun 02 '11 22:06 bartaz