Updated toJson() to not serialize window/document objects.

The reason to void these to objects is that they cause all sorts
of problems like exceptions being thrown and infinite loops occuring
when we iterate over object properties.
This commit is contained in:
Vojta Jina 2010-11-05 23:04:13 +00:00 committed by Igor Minar
parent fe8353bc5e
commit b7027b9d87
2 changed files with 20 additions and 2 deletions

View file

@ -22,8 +22,18 @@ function fromJson(json) {
angular['toJson'] = toJson;
angular['fromJson'] = fromJson;
function toJsonArray(buf, obj, pretty, stack){
if (typeof obj == "object") {
function toJsonArray(buf, obj, pretty, stack) {
if (isObject(obj)) {
if (obj === window) {
buf.push('WINDOW');
return;
}
if (obj === document) {
buf.push('DOCUMENT');
return;
}
if (includes(stack, obj)) {
buf.push("RECURSION");
return;

View file

@ -92,6 +92,14 @@ describe('json', function(){
it('should not serialize undefined values', function() {
expect(angular.toJson({A:undefined})).toEqual('{}');
});
it('should not serialize $window object', function() {
expect(toJson(window)).toEqual('WINDOW');
});
it('should not serialize $document object', function() {
expect(toJson(document)).toEqual('DOCUMENT');
});
it('should parse floats', function() {
expect(fromJson("{value:2.55, name:'misko'}")).toEqual({value:2.55, name:'misko'});