Skip to content

Commit 125d725

Browse files
committed
toJson should serialize inherited properties, but not any properties that start with $
1 parent 0d71731 commit 125d725

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/JSON.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function toJsonArray(buf, obj, pretty, stack){
7070
var childPretty = pretty ? pretty + " " : false;
7171
var keys = [];
7272
for(var k in obj) {
73-
if (!obj.hasOwnProperty(k) || k.indexOf('$$') === 0 || obj[k] === _undefined)
73+
if (k.indexOf('$') === 0 || obj[k] === _undefined)
7474
continue;
7575
keys.push(k);
7676
}

test/JsonTest.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,18 @@ JsonTest.prototype.testItShouldPreventRecursion = function () {
7474
assertEquals('{"a":"b","recursion":RECURSION}', angular.toJson(obj));
7575
};
7676

77-
JsonTest.prototype.testItShouldSerializeOnlyOwnProperties = function() {
78-
var parent = createScope();
79-
var child = createScope(parent);
80-
child.c = 'c';
81-
expect(angular.toJson(child)).toEqual('{"c":"c"}');
77+
JsonTest.prototype.testItShouldIgnore$Properties = function() {
78+
var scope = createScope();
79+
scope.a = 'a';
80+
scope['$b'] = '$b';
81+
scope.c = 'c';
82+
expect(angular.toJson(scope)).toEqual('{"a":"a","c":"c","this":RECURSION}');
83+
};
84+
85+
JsonTest.prototype.testItShouldSerializeInheritedProperties = function() {
86+
var scope = createScope({p:'p'});
87+
scope.a = 'a';
88+
expect(angular.toJson(scope)).toEqual('{"a":"a","p":"p","this":RECURSION}');
8289
};
8390

8491
JsonTest.prototype.testItShouldSerializeSameObjectsMultipleTimes = function () {

0 commit comments

Comments
 (0)