From 7159b30752f63ad8a127101cbc10d7e672ae1620 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 13 Aug 2010 15:14:41 -0700 Subject: Serialize only own properties to avoid infinite loops when serializing scopes (this) --- src/JSON.js | 2 +- test/JsonTest.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/JSON.js b/src/JSON.js index 340b075a..0d2fbca4 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -74,7 +74,7 @@ function toJsonArray(buf, obj, pretty, stack){ var childPretty = pretty ? pretty + " " : false; var keys = []; for(var k in obj) { - if (k.indexOf('$$') === 0 || obj[k] === undefined) + if (!obj.hasOwnProperty(k) || k.indexOf('$$') === 0 || obj[k] === undefined) continue; keys.push(k); } diff --git a/test/JsonTest.js b/test/JsonTest.js index 4afb7743..d077c0df 100644 --- a/test/JsonTest.js +++ b/test/JsonTest.js @@ -74,6 +74,13 @@ JsonTest.prototype.testItShouldPreventRecursion = function () { assertEquals('{"a":"b","recursion":RECURSION}', angular.toJson(obj)); }; +JsonTest.prototype.testItShouldSerializeOnlyOwnProperties = function() { + var parent = { p: 'p'}; + var child = { c: 'c'}; + child.__proto__ = parent; + assertEquals('{"c":"c"}', angular.toJson(child)); +} + JsonTest.prototype.testItShouldSerializeSameObjectsMultipleTimes = function () { var obj = {a:'b'}; assertEquals('{"A":{"a":"b"},"B":{"a":"b"}}', angular.toJson({A:obj, B:obj})); -- cgit v1.2.3