2015-04-04 09:53:59 -07:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
|
// with the License. You may obtain a copy of the License at
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
2012-04-26 17:55:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @fileoverview Provides JSON utilities that uses native JSON parsing where
|
|
|
|
|
* possible (a feature not currently offered by Closure).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
goog.provide('bot.json');
|
|
|
|
|
|
|
|
|
|
goog.require('bot.userAgent');
|
|
|
|
|
goog.require('goog.json');
|
|
|
|
|
goog.require('goog.userAgent');
|
|
|
|
|
|
|
|
|
|
|
2012-07-26 00:03:42 +00:00
|
|
|
/**
|
|
|
|
|
* @define {boolean} NATIVE_JSON indicates whether the code should rely on the
|
2018-11-25 13:21:40 +00:00
|
|
|
* native `JSON` functions, if available.
|
2012-07-26 00:03:42 +00:00
|
|
|
*
|
|
|
|
|
* <p>The JSON functions can be defined by external libraries like Prototype
|
|
|
|
|
* and setting this flag to false forces the use of Closure's goog.json
|
|
|
|
|
* implementation.
|
|
|
|
|
*
|
|
|
|
|
* <p>If your JavaScript can be loaded by a third_party site and you are wary
|
|
|
|
|
* about relying on the native functions, specify
|
|
|
|
|
* "--define bot.json.NATIVE_JSON=false" to the Closure compiler.
|
|
|
|
|
*/
|
|
|
|
|
bot.json.NATIVE_JSON = true;
|
|
|
|
|
|
|
|
|
|
|
2012-04-26 17:55:18 +00:00
|
|
|
/**
|
|
|
|
|
* Whether the current browser supports the native JSON interface.
|
|
|
|
|
* @const
|
|
|
|
|
* @see http://caniuse.com/#search=JSON
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {boolean}
|
2012-04-26 17:55:18 +00:00
|
|
|
*/
|
2012-07-26 00:03:42 +00:00
|
|
|
bot.json.SUPPORTS_NATIVE_JSON_ =
|
2015-02-28 21:21:52 +01:00
|
|
|
// List WebKit first since every supported version supports
|
|
|
|
|
// native JSON (and we can compile away large chunks of code for
|
|
|
|
|
// individual fragments by setting the appropriate compiler flags).
|
|
|
|
|
goog.userAgent.WEBKIT ||
|
2020-11-27 15:46:30 +00:00
|
|
|
(goog.userAgent.GECKO && bot.userAgent.isEngineVersion(3.5)) ||
|
|
|
|
|
(goog.userAgent.IE && bot.userAgent.isEngineVersion(8));
|
2012-04-26 17:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a JSON object to its string representation.
|
|
|
|
|
* @param {*} jsonObj The input object.
|
|
|
|
|
* @param {?(function(string, *): *)=} opt_replacer A replacer function called
|
|
|
|
|
* for each (key, value) pair that determines how the value should be
|
|
|
|
|
* serialized. By default, this just returns the value and allows default
|
|
|
|
|
* serialization to kick in.
|
|
|
|
|
* @return {string} A JSON string representation of the input object.
|
|
|
|
|
*/
|
2012-07-26 00:03:42 +00:00
|
|
|
bot.json.stringify = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?
|
|
|
|
|
JSON.stringify : goog.json.serialize;
|
2012-04-26 17:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a JSON string and returns the result.
|
|
|
|
|
* @param {string} jsonStr The string to parse.
|
|
|
|
|
* @return {*} The JSON object.
|
|
|
|
|
* @throws {Error} If the input string is an invalid JSON string.
|
|
|
|
|
*/
|
2017-09-01 22:17:06 +02:00
|
|
|
bot.json.parse = JSON.parse;
|