diff --git a/src/Angular.js b/src/Angular.js index 315766eafc4f..4015aca52128 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -948,7 +948,7 @@ function bind(self, fn) { function toJsonReplacer(key, value) { var val = value; - if (typeof key === 'string' && key.charAt(0) === '$') { + if (typeof key === 'string' && key.charAt(0) === '$' && key.charAt(1) === '$') { val = undefined; } else if (isWindow(value)) { val = '$WINDOW'; @@ -969,7 +969,7 @@ function toJsonReplacer(key, value) { * @function * * @description - * Serializes input into a JSON-formatted string. Properties with leading $ characters will be + * Serializes input into a JSON-formatted string. Properties with leading $$ characters will be * stripped since angular uses this notation internally. * * @param {Object|Array|Date|string|number} obj Input to be serialized into JSON. diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index ce3468794094..f3fd8a582be5 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -493,6 +493,13 @@ angular.module('ngResource', ['ng']). shallowClearAndCopy(value || {}, this); } + Resource.prototype.toJSON = function () { + var data = extend({}, this); + delete data.$promise; + delete data.$resolved; + return data; + }; + forEach(actions, function (action, name) { var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method); diff --git a/test/AngularSpec.js b/test/AngularSpec.js index f1b0b7203abb..9b33e496c75a 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -1093,8 +1093,13 @@ describe('angular', function() { }); - it('should not serialize properties starting with $', function() { - expect(toJson({$few: 'v', $$some:'value'}, false)).toEqual('{}'); + it('should not serialize properties starting with $$', function() { + expect(toJson({$$some:'value'}, false)).toEqual('{}'); + }); + + + it('should serialize properties starting with $', function() { + expect(toJson({$few: 'v'}, false)).toEqual('{"$few":"v"}'); }); diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index ac011216bd7e..7e30cd2cd1ee 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -647,6 +647,20 @@ describe("resource", function() { expect(person2).toEqual(jasmine.any(Person)); }); + it('should not include $promise and $resolved when resource is toJson\'ed', function() { + $httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'}); + var cc = CreditCard.get({id: 123}); + $httpBackend.flush(); + + expect(cc.$promise).toBeDefined(); + expect(cc.$resolved).toBe(true); + + var json = JSON.parse(angular.toJson(cc)); + expect(json.$promise).not.toBeDefined(); + expect(json.$resolved).not.toBeDefined(); + expect(json).toEqual({id: 123, number: '9876'}); + }); + describe('promise api', function() { var $rootScope;