toJson should serialize inherited properties, but not any properties that start with $

This commit is contained in:
Igor Minar 2010-09-21 16:19:07 +02:00
parent 0d717310fd
commit 125d725e7d
2 changed files with 13 additions and 6 deletions

View file

@ -70,7 +70,7 @@ function toJsonArray(buf, obj, pretty, stack){
var childPretty = pretty ? pretty + " " : false;
var keys = [];
for(var k in obj) {
if (!obj.hasOwnProperty(k) || k.indexOf('$$') === 0 || obj[k] === _undefined)
if (k.indexOf('$') === 0 || obj[k] === _undefined)
continue;
keys.push(k);
}

View file

@ -74,11 +74,18 @@ JsonTest.prototype.testItShouldPreventRecursion = function () {
assertEquals('{"a":"b","recursion":RECURSION}', angular.toJson(obj));
};
JsonTest.prototype.testItShouldSerializeOnlyOwnProperties = function() {
var parent = createScope();
var child = createScope(parent);
child.c = 'c';
expect(angular.toJson(child)).toEqual('{"c":"c"}');
JsonTest.prototype.testItShouldIgnore$Properties = function() {
var scope = createScope();
scope.a = 'a';
scope['$b'] = '$b';
scope.c = 'c';
expect(angular.toJson(scope)).toEqual('{"a":"a","c":"c","this":RECURSION}');
};
JsonTest.prototype.testItShouldSerializeInheritedProperties = function() {
var scope = createScope({p:'p'});
scope.a = 'a';
expect(angular.toJson(scope)).toEqual('{"a":"a","p":"p","this":RECURSION}');
};
JsonTest.prototype.testItShouldSerializeSameObjectsMultipleTimes = function () {