diff options
| author | Igor Minar | 2010-12-22 13:19:26 -0800 |
|---|---|---|
| committer | Igor Minar | 2010-12-22 14:16:36 -0800 |
| commit | e7a0fb250f6cc69c93daffe0d043d073fd488c03 (patch) | |
| tree | a799e9264a131a9124de7983b5655b9e97df44ba /src/JSON.js | |
| parent | e3ddc2bcc48ba6160455c4ffe35df87715c05693 (diff) | |
| download | angular.js-e7a0fb250f6cc69c93daffe0d043d073fd488c03.tar.bz2 | |
fromJson delegation to native JSON parser if available
- native parser delegation
- $xhr change to use native parser
Diffstat (limited to 'src/JSON.js')
| -rw-r--r-- | src/JSON.js | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/JSON.js b/src/JSON.js index 2906362c..399b2197 100644 --- a/src/JSON.js +++ b/src/JSON.js @@ -29,19 +29,41 @@ function toJson(obj, pretty) { * Deserializes a string in the JSON format. * * @param {string} json JSON string to deserialize. + * @param {boolean} [useNative=false] Use native JSON parser if available * @returns {Object|Array|Date|string|number} Deserialized thingy. */ -function fromJson(json) { +function fromJson(json, useNative) { if (!json) return json; + + var obj, p, expression; + try { - var p = parser(json, true); - var expression = p.primary(); + if (useNative && JSON && JSON.parse) { + obj = JSON.parse(json); + return transformDates(obj); + } + + p = parser(json, true); + expression = p.primary(); p.assertAllConsumed(); return expression(); + } catch (e) { error("fromJson error: ", json, e); throw e; } + + // TODO make foreach optionally recursive and remove this function + function transformDates(obj) { + if (isString(obj) && obj.length === DATE_ISOSTRING_LN) { + return angularString.toDate(obj); + } else if (isArray(obj) || isObject(obj)) { + foreach(obj, function(val, name) { + obj[name] = transformDates(val); + }); + } + return obj; + } } angular['toJson'] = toJson; |
