# lib/atoms/json.js

    1// Copyright 2012 WebDriver committers2// Copyright 2012 Google Inc.3//4// Licensed under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8//      http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516/**17 * @fileoverview Provides JSON utilities that uses native JSON parsing where18 * possible (a feature not currently offered by Closure).19 */2021goog.provide('bot.json');2223goog.require('bot.userAgent');24goog.require('goog.json');25goog.require('goog.userAgent');262728/**29 * @define {boolean} NATIVE_JSON indicates whether the code should rely on the30 * native {@code JSON} functions, if available.31 *32 * <p>The JSON functions can be defined by external libraries like Prototype33 * and setting this flag to false forces the use of Closure's goog.json34 * implementation.35 *36 * <p>If your JavaScript can be loaded by a third_party site and you are wary37 * about relying on the native functions, specify38 * "--define bot.json.NATIVE_JSON=false" to the Closure compiler.39 */40bot.json.NATIVE_JSON = true;414243/**44 * Whether the current browser supports the native JSON interface.45 * @const46 * @see http://caniuse.com/#search=JSON47 * @private {boolean}48 */49bot.json.SUPPORTS_NATIVE_JSON_ =50    // List WebKit and Opera first since every supported version of these51    // browsers supports native JSON (and we can compile away large chunks of52    // code for individual fragments by setting the appropriate compiler flags).53    goog.userAgent.WEBKIT || goog.userAgent.OPERA ||54        (goog.userAgent.GECKO && bot.userAgent.isEngineVersion(3.5)) ||55        (goog.userAgent.IE && bot.userAgent.isEngineVersion(8));565758/**59 * Converts a JSON object to its string representation.60 * @param {*} jsonObj The input object.61 * @param {?(function(string, *): *)=} opt_replacer A replacer function called62 *     for each (key, value) pair that determines how the value should be63 *     serialized. By default, this just returns the value and allows default64 *     serialization to kick in.65 * @return {string} A JSON string representation of the input object.66 */67bot.json.stringify = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?68    JSON.stringify : goog.json.serialize;697071/**72 * Parses a JSON string and returns the result.73 * @param {string} jsonStr The string to parse.74 * @return {*} The JSON object.75 * @throws {Error} If the input string is an invalid JSON string.76 */77bot.json.parse = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?78    JSON.parse : goog.json.parse;