Path: blob/trunk/third_party/closure/goog/json/nativejsonprocessor.js
2868 views
// Copyright 2012 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 Defines a class for parsing JSON using the browser's built in17* JSON library.18*/1920goog.provide('goog.json.NativeJsonProcessor');2122goog.require('goog.asserts');23goog.require('goog.json.Processor');24252627/**28* A class that parses and stringifies JSON using the browser's built-in JSON29* library, if it is available.30*31* Note that the native JSON api has subtle differences across browsers, so32* use this implementation with care. See json_test#assertSerialize33* for details on the differences from goog.json.34*35* This implementation is signficantly faster than goog.json, at least on36* Chrome. See json_perf.html for a perf test showing the difference.37*38* @param {?goog.json.Replacer=} opt_replacer An optional replacer to use during39* serialization.40* @param {?goog.json.Reviver=} opt_reviver An optional reviver to use during41* parsing.42* @constructor43* @implements {goog.json.Processor}44* @final45*/46goog.json.NativeJsonProcessor = function(opt_replacer, opt_reviver) {47goog.asserts.assert(goog.isDef(goog.global['JSON']), 'JSON not defined');4849/**50* @type {goog.json.Replacer|null|undefined}51* @private52*/53this.replacer_ = opt_replacer;5455/**56* @type {goog.json.Reviver|null|undefined}57* @private58*/59this.reviver_ = opt_reviver;60};616263/** @override */64goog.json.NativeJsonProcessor.prototype.stringify = function(object) {65return goog.global['JSON'].stringify(object, this.replacer_);66};676869/** @override */70goog.json.NativeJsonProcessor.prototype.parse = function(s) {71return goog.global['JSON'].parse(s, this.reviver_);72};737475