diff options
| author | Igor Minar | 2012-03-27 12:44:37 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-03-28 16:57:22 -0700 |
| commit | 35125d25137ac2da13ed1ca3e652ec8f2c945053 (patch) | |
| tree | 387623dfc398e04c17ea15917d2d8e8cbf40bf19 /src | |
| parent | 87f5c6e5b716100e203ec59c5874c3e927f83fa0 (diff) | |
| download | angular.js-35125d25137ac2da13ed1ca3e652ec8f2c945053.tar.bz2 | |
refactor(toJson): use native JSON.stringify
Instead of using our custom serializer we now use the native one and
use the replacer function to customize the serialization to preserve
most of the previous behavior (ignore $ and $$ properties as well
as window, document and scope instances).
Diffstat (limited to 'src')
| -rw-r--r-- | src/JSON.js | 149 | ||||
| -rw-r--r-- | src/ng/directive/input.js | 4 | ||||
| -rw-r--r-- | src/ng/filter/filters.js | 2 | ||||
| -rw-r--r-- | src/ng/filter/limitTo.js | 10 |
4 files changed, 24 insertions, 141 deletions
diff --git a/src/JSON.js b/src/JSON.js index bcb5fabe..e6d48c86 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -1,5 +1,20 @@ 'use strict'; +var jsonReplacer = function(key, value) { + var val = value; + if (/^\$+/.test(key)) { + val = undefined; + } else if (isWindow(value)) { + val = '$WINDOW'; + } else if (value && document === value) { + val = '$DOCUMENT'; + } else if (isScope(value)) { + val = '$SCOPE'; + } + + return val; +}; + /** * @ngdoc function * @name angular.toJson @@ -13,9 +28,7 @@ * @returns {string} Jsonified string representing `obj`. */ function toJson(obj, pretty) { - var buf = []; - toJsonArray(buf, obj, pretty ? "\n " : null, []); - return buf.join(''); + return JSON.stringify(obj, jsonReplacer, pretty ? ' ' : null); } /** @@ -34,133 +47,3 @@ function fromJson(json) { ? JSON.parse(json) : json; } - - -function jsonDateToString(date){ - if (!date) return date; - var isoString = date.toISOString ? date.toISOString() : ''; - return (isoString.length==24) - ? isoString - : padNumber(date.getUTCFullYear(), 4) + '-' + - padNumber(date.getUTCMonth() + 1, 2) + '-' + - padNumber(date.getUTCDate(), 2) + 'T' + - padNumber(date.getUTCHours(), 2) + ':' + - padNumber(date.getUTCMinutes(), 2) + ':' + - padNumber(date.getUTCSeconds(), 2) + '.' + - padNumber(date.getUTCMilliseconds(), 3) + 'Z'; -} - -function quoteUnicode(string) { - var chars = ['"']; - for ( var i = 0; i < string.length; i++) { - var code = string.charCodeAt(i); - var ch = string.charAt(i); - switch(ch) { - case '"': chars.push('\\"'); break; - case '\\': chars.push('\\\\'); break; - case '\n': chars.push('\\n'); break; - case '\f': chars.push('\\f'); break; - case '\r': chars.push(ch = '\\r'); break; - case '\t': chars.push(ch = '\\t'); break; - default: - if (32 <= code && code <= 126) { - chars.push(ch); - } else { - var encode = "000" + code.toString(16); - chars.push("\\u" + encode.substring(encode.length - 4)); - } - } - } - chars.push('"'); - return chars.join(''); - } - - -function toJsonArray(buf, obj, pretty, stack) { - if (isObject(obj)) { - if (obj === window) { - buf.push('WINDOW'); - return; - } - - if (obj === document) { - buf.push('DOCUMENT'); - return; - } - - if (includes(stack, obj)) { - buf.push('RECURSION'); - return; - } - stack.push(obj); - } - if (obj === null) { - buf.push('null'); - } else if (obj instanceof RegExp) { - buf.push(quoteUnicode(obj.toString())); - } else if (isFunction(obj)) { - return; - } else if (isBoolean(obj)) { - buf.push('' + obj); - } else if (isNumber(obj)) { - if (isNaN(obj)) { - buf.push('null'); - } else { - buf.push('' + obj); - } - } else if (isString(obj)) { - return buf.push(quoteUnicode(obj)); - } else if (isObject(obj)) { - if (isArray(obj)) { - buf.push("["); - var len = obj.length; - var sep = false; - for(var i=0; i<len; i++) { - var item = obj[i]; - if (sep) buf.push(","); - if (!(item instanceof RegExp) && (isFunction(item) || isUndefined(item))) { - buf.push('null'); - } else { - toJsonArray(buf, item, pretty, stack); - } - sep = true; - } - buf.push("]"); - } else if (isElement(obj)) { - // TODO(misko): maybe in dev mode have a better error reporting? - buf.push('DOM_ELEMENT'); - } else if (isDate(obj)) { - buf.push(quoteUnicode(jsonDateToString(obj))); - } else { - buf.push("{"); - if (pretty) buf.push(pretty); - var comma = false; - var childPretty = pretty ? pretty + " " : false; - var keys = []; - for(var k in obj) { - if (k!='this' && k!='$parent' && k.substring(0,2) != '$$' && obj.hasOwnProperty(k) && obj[k] !== undefined) { - keys.push(k); - } - } - keys.sort(); - for ( var keyIndex = 0; keyIndex < keys.length; keyIndex++) { - var key = keys[keyIndex]; - var value = obj[key]; - if (!isFunction(value)) { - if (comma) { - buf.push(","); - if (pretty) buf.push(pretty); - } - buf.push(quoteUnicode(key)); - buf.push(":"); - toJsonArray(buf, value, childPretty, stack); - comma = true; - } - } - buf.push("}"); - } - } - if (isObject(obj)) { - stack.pop(); - } -} diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 348c9f25..7343c358 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -671,7 +671,7 @@ function checkboxInputType(scope, element, attr, ctrl) { </doc:source> <doc:scenario> it('should initialize to model', function() { - expect(binding('user')).toEqual('{"last":"visitor","name":"guest"}'); + expect(binding('user')).toEqual('{"name":"guest","last":"visitor"}'); expect(binding('myForm.userName.$valid')).toEqual('true'); expect(binding('myForm.$valid')).toEqual('true'); }); @@ -685,7 +685,7 @@ function checkboxInputType(scope, element, attr, ctrl) { it('should be valid if empty when min length is set', function() { input('user.last').enter(''); - expect(binding('user')).toEqual('{"last":"","name":"guest"}'); + expect(binding('user')).toEqual('{"name":"guest","last":""}'); expect(binding('myForm.lastName.$valid')).toEqual('true'); expect(binding('myForm.$valid')).toEqual('true'); }); diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index c792cace..19f79ad1 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -407,7 +407,7 @@ function dateFilter($locale) { </doc:source> <doc:scenario> it('should jsonify filtered objects', function() { - expect(binding("{'name':'value'}")).toBe('{\n "name":"value"}'); + expect(binding("{'name':'value'}")).toMatch(/\{\n "name": ?"value"\n}/); }); </doc:scenario> </doc:example> diff --git a/src/ng/filter/limitTo.js b/src/ng/filter/limitTo.js index 4928fb9a..af94b2f4 100644 --- a/src/ng/filter/limitTo.js +++ b/src/ng/filter/limitTo.js @@ -31,24 +31,24 @@ } </script> <div ng-controller="Ctrl"> - Limit {{numbers}} to: <input type="integer" ng-model="limit"/> - <p>Output: {{ numbers | limitTo:limit | json }}</p> + Limit {{numbers}} to: <input type="integer" ng-model="limit" ng-model-instant> + <p>Output: {{ numbers | limitTo:limit }}</p> </div> </doc:source> <doc:scenario> it('should limit the numer array to first three items', function() { expect(element('.doc-example-live input[ng-model=limit]').val()).toBe('3'); - expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3]'); + expect(binding('numbers | limitTo:limit')).toEqual('[1,2,3]'); }); it('should update the output when -3 is entered', function() { input('limit').enter(-3); - expect(binding('numbers | limitTo:limit | json')).toEqual('[7,8,9]'); + expect(binding('numbers | limitTo:limit')).toEqual('[7,8,9]'); }); it('should not exceed the maximum size of input array', function() { input('limit').enter(100); - expect(binding('numbers | limitTo:limit | json')).toEqual('[1,2,3,4,5,6,7,8,9]'); + expect(binding('numbers | limitTo:limit')).toEqual('[1,2,3,4,5,6,7,8,9]'); }); </doc:scenario> </doc:example> |
