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

Commit af0ad65

Browse files
committed
refactor(fromJson/toJson): move the contents of these files into Angular.js
these files are now mostly empty so it doesn't make sense to keep them separated from other helper functions
1 parent 35125d2 commit af0ad65

File tree

5 files changed

+103
-108
lines changed

5 files changed

+103
-108
lines changed

angularFiles.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ angularFiles = {
33
'src/Angular.js',
44
'src/loader.js',
55
'src/AngularPublic.js',
6-
'src/JSON.js',
76
'src/jqLite.js',
87
'src/apis.js',
98

src/Angular.js

+53
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,59 @@ function bind(self, fn) {
727727
}
728728
}
729729

730+
731+
function toJsonReplacer(key, value) {
732+
var val = value;
733+
734+
if (/^\$+/.test(key)) {
735+
val = undefined;
736+
} else if (isWindow(value)) {
737+
val = '$WINDOW';
738+
} else if (value && document === value) {
739+
val = '$DOCUMENT';
740+
} else if (isScope(value)) {
741+
val = '$SCOPE';
742+
}
743+
744+
return val;
745+
};
746+
747+
748+
/**
749+
* @ngdoc function
750+
* @name angular.toJson
751+
* @function
752+
*
753+
* @description
754+
* Serializes input into a JSON-formatted string.
755+
*
756+
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
757+
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
758+
* @returns {string} Jsonified string representing `obj`.
759+
*/
760+
function toJson(obj, pretty) {
761+
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
762+
}
763+
764+
765+
/**
766+
* @ngdoc function
767+
* @name angular.fromJson
768+
* @function
769+
*
770+
* @description
771+
* Deserializes a JSON string.
772+
*
773+
* @param {string} json JSON string to deserialize.
774+
* @returns {Object|Array|Date|string|number} Deserialized thingy.
775+
*/
776+
function fromJson(json) {
777+
return isString(json)
778+
? JSON.parse(json)
779+
: json;
780+
}
781+
782+
730783
function toBoolean(value) {
731784
if (value && value.length !== 0) {
732785
var v = lowercase("" + value);

src/JSON.js

-49
This file was deleted.

test/AngularSpec.js

+50
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,54 @@ describe('angular', function() {
542542
expect(snake_case('alanBobCharles')).toEqual('alan_bob_charles');
543543
});
544544
});
545+
546+
547+
describe('fromJson', function() {
548+
549+
it('should delegate to JSON.parse', function() {
550+
var spy = spyOn(JSON, 'parse').andCallThrough();
551+
552+
expect(fromJson('{}')).toEqual({});
553+
expect(spy).toHaveBeenCalled();
554+
});
555+
});
556+
557+
558+
describe('toJson', function() {
559+
560+
it('should delegate to JSON.stringify', function() {
561+
var spy = spyOn(JSON, 'stringify').andCallThrough();
562+
563+
expect(toJson({})).toEqual('{}');
564+
expect(spy).toHaveBeenCalled();
565+
});
566+
567+
568+
it('should format objects pretty', function() {
569+
expect(toJson({a: 1, b: 2}, true)).
570+
toBeOneOf('{\n "a": 1,\n "b": 2\n}', '{\n "a":1,\n "b":2\n}');
571+
expect(toJson({a: {b: 2}}, true)).
572+
toBeOneOf('{\n "a": {\n "b": 2\n }\n}', '{\n "a":{\n "b":2\n }\n}');
573+
});
574+
575+
576+
it('should not serialize properties starting with $', function() {
577+
expect(toJson({$few: 'v', $$some:'value'}, false)).toEqual('{}');
578+
});
579+
580+
581+
it('should not serialize $window object', function() {
582+
expect(toJson(window)).toEqual('"$WINDOW"');
583+
});
584+
585+
586+
it('should not serialize $document object', function() {
587+
expect(toJson(document)).toEqual('"$DOCUMENT"');
588+
});
589+
590+
591+
it('should not serialize scope instances', inject(function($rootScope) {
592+
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
593+
}));
594+
});
545595
});

test/JsonSpec.js

-58
This file was deleted.

0 commit comments

Comments
 (0)