Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(angular.toJson): only strip properties beginning with $$, not $ #6253

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}');
});


Expand Down
14 changes: 14 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down