SteveSanderson/knockout — Issue #278: unable to override toJSON on arrays
Author: jamesfoster
Created: 2012-01-13 13:01:18+00:00
State: open

```
function myObservable() {
var observable = new ko.observableArray();

observable.toJSON = function() { ... };

return observable;
}
```

I was stuck for a while wondering why my toJSON method wasn't being called until I found the following line

https://github.com/SteveSanderson/knockout/blob/master/src/subscribables/mappingHelpers.js#L58

knockout ignores all properties on an array other than its numeric indices. It would be great if you specifically included toJSON, or all properties, or just all "owned" properties!

Thanks

[aside]My object might have many properties, many of which could be arbitrarily complex, however, in my toJSON method I could simplify everything down to a single object with only a few simple properties. knockout will still evaluate my entire object graph before handing over to JSON.stringify().
You could add an extension point in `toJS` which looks for a method on each object (probably called 'toJS') which returns a new object to replace itself in the graph. Potentially preventing wasting time evaluating the entire graph.
Just a thought.[/aside]