From 332a3c7984229a7e3a9a8a277f92942299616fdb Mon Sep 17 00:00:00 2001 From: Greg Thornton Date: Sun, 23 Jun 2013 14:04:59 -0500 Subject: [PATCH] feat(Angular.js): skip JSON.stringify for undefined Return early in `angular.toJson` if the object to be stringified is `undefined`. IE8 stringifies `undefined` to `'undefined'` whereas other browsers return `undefined`. This normalizes behavior and passes currently broken unit tests in IE8. --- src/Angular.js | 3 ++- test/AngularSpec.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index ec3be068c17f..01b37969feaa 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -751,9 +751,10 @@ function toJsonReplacer(key, value) { * * @param {Object|Array|Date|string|number} obj Input to be serialized into JSON. * @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace. - * @returns {string} Jsonified string representing `obj`. + * @returns {string|undefined} Jsonified string representing `obj`. */ function toJson(obj, pretty) { + if (typeof obj === 'undefined') return undefined; return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null); } diff --git a/test/AngularSpec.js b/test/AngularSpec.js index febc02269035..8e4fbb1a7d32 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -879,5 +879,9 @@ describe('angular', function() { it('should not serialize scope instances', inject(function($rootScope) { expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}'); })); + + it('should serialize undefined as undefined', function() { + expect(toJson(undefined)).toEqual(undefined); + }); }); });