diff options
| author | Igor Minar | 2012-03-26 23:38:20 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-03-28 16:30:30 -0700 |
| commit | ac4318a2fa5c6d306dbc19466246292a81767fca (patch) | |
| tree | a8d9d0ff164516b153bf4c9142d1c9fb17315008 /src/JSON.js | |
| parent | bb2fa6f63f0a4a08d4a3a5439d1ab6d9ddfd917e (diff) | |
| download | angular.js-ac4318a2fa5c6d306dbc19466246292a81767fca.tar.bz2 | |
refactor(fromJson/date filter): move date string logic to date filter
Breaks angular.fromJson which doesn't deserialize date strings into date objects.
This was done to make fromJson compatible with JSON.parse.
If you do require the old behavior - if at all neeeded then because of
json deserialization of XHR responses - then please create a custom
$http transform:
$httpProvider.defaults.transformResponse.push(function(data) {
// recursively parse dates from data object here
// see code removed in this diff for hints
});
Closes #202
Diffstat (limited to 'src/JSON.js')
| -rw-r--r-- | src/JSON.js | 43 |
1 files changed, 3 insertions, 40 deletions
diff --git a/src/JSON.js b/src/JSON.js index 21d526c2..cddfc52d 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -1,7 +1,5 @@ 'use strict'; -var array = [].constructor; - /** * @ngdoc function * @name angular.toJson @@ -35,46 +33,11 @@ function toJson(obj, pretty) { function fromJson(json, useNative) { if (!isString(json)) return json; - var obj; - - if (useNative && window.JSON && window.JSON.parse) { - obj = JSON.parse(json); - } else { - obj = parseJson(json, true)(); - } - return transformDates(obj); - - // TODO make forEach optionally recursive and remove this function - // TODO(misko): remove this once the $http service is checked in. - function transformDates(obj) { - if (isString(obj) && 15 <= obj.length && obj.length <= 24) { - return jsonStringToDate(obj); - } else if (isArray(obj) || isObject(obj)) { - forEach(obj, function(val, name) { - obj[name] = transformDates(val); - }); - } - return obj; - } + return (useNative && window.JSON && window.JSON.parse) + ? JSON.parse(json) + : parseJson(json, true)(); } -var R_ISO8061_STR = /^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?:\:?(\d\d)(?:\:?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/; -function jsonStringToDate(string){ - var match; - if (match = string.match(R_ISO8061_STR)) { - var date = new Date(0), - tzHour = 0, - tzMin = 0; - if (match[9]) { - tzHour = int(match[9] + match[10]); - tzMin = int(match[9] + match[11]); - } - date.setUTCFullYear(int(match[1]), int(match[2]) - 1, int(match[3])); - date.setUTCHours(int(match[4]||0) - tzHour, int(match[5]||0) - tzMin, int(match[6]||0), int(match[7]||0)); - return date; - } - return string; -} function jsonDateToString(date){ if (!date) return date; |
