Path: blob/trunk/third_party/closure/goog/json/hybrid.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Utility to attempt native JSON processing, falling back to17* goog.json if not available.18*19* This is intended as a drop-in for current users of goog.json who want20* to take advantage of native JSON if present.21*22* @author [email protected] (Nathan Naze)23*/2425goog.provide('goog.json.hybrid');2627goog.require('goog.asserts');28goog.require('goog.json');293031/**32* Attempts to serialize the JSON string natively, falling back to33* {@code goog.json.serialize} if unsuccessful.34* @param {!Object} obj JavaScript object to serialize to JSON.35* @return {string} Resulting JSON string.36*/37goog.json.hybrid.stringify =38goog.json.USE_NATIVE_JSON ? goog.global['JSON']['stringify'] : function(39obj) {40if (goog.global.JSON) {41try {42return goog.global.JSON.stringify(obj);43} catch (e) {44// Native serialization failed. Fall through to retry with45// goog.json.serialize.46}47}4849return goog.json.serialize(obj);50};515253/**54* Attempts to parse the JSON string natively, falling back to55* the supplied {@code fallbackParser} if unsuccessful.56* @param {string} jsonString JSON string to parse.57* @param {function(string):Object} fallbackParser Fallback JSON parser used58* if native59* @return {!Object} Resulting JSON object.60* @private61*/62goog.json.hybrid.parse_ = function(jsonString, fallbackParser) {63if (goog.global.JSON) {64try {65var obj = goog.global.JSON.parse(jsonString);66goog.asserts.assertObject(obj);67return obj;68} catch (e) {69// Native parse failed. Fall through to retry with goog.json.unsafeParse.70}71}7273var obj = fallbackParser(jsonString);74goog.asserts.assert(obj);75return obj;76};777879/**80* Attempts to parse the JSON string natively, falling back to81* {@code goog.json.parse} if unsuccessful.82* @param {string} jsonString JSON string to parse.83* @return {!Object} Resulting JSON object.84*/85goog.json.hybrid.parse =86goog.json.USE_NATIVE_JSON ? goog.global['JSON']['parse'] : function(87jsonString) {88return goog.json.hybrid.parse_(jsonString, goog.json.parse);89};909192/**93* Attempts to parse the JSON string natively, falling back to94* {@code goog.json.unsafeParse} if unsuccessful.95* @param {string} jsonString JSON string to parse.96* @return {!Object} Resulting JSON object.97*/98goog.json.hybrid.unsafeParse =99goog.json.USE_NATIVE_JSON ? goog.global['JSON']['parse'] : function(100jsonString) {101return goog.json.hybrid.parse_(jsonString, goog.json.unsafeParse);102};103104105