From ac4318a2fa5c6d306dbc19466246292a81767fca Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Mon, 26 Mar 2012 23:38:20 -0700 Subject: 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 --- src/JSON.js | 43 +++---------------------------------------- 1 file changed, 3 insertions(+), 40 deletions(-) (limited to 'src/JSON.js') 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; -- cgit v1.2.3