lib/atoms/json.js

1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/**
19 * @fileoverview Provides JSON utilities that uses native JSON parsing where
20 * possible (a feature not currently offered by Closure).
21 */
22
23goog.provide('bot.json');
24
25goog.require('bot.userAgent');
26goog.require('goog.json');
27goog.require('goog.userAgent');
28
29
30/**
31 * @define {boolean} NATIVE_JSON indicates whether the code should rely on the
32 * native {@code JSON} functions, if available.
33 *
34 * <p>The JSON functions can be defined by external libraries like Prototype
35 * and setting this flag to false forces the use of Closure's goog.json
36 * implementation.
37 *
38 * <p>If your JavaScript can be loaded by a third_party site and you are wary
39 * about relying on the native functions, specify
40 * "--define bot.json.NATIVE_JSON=false" to the Closure compiler.
41 */
42bot.json.NATIVE_JSON = true;
43
44
45/**
46 * Whether the current browser supports the native JSON interface.
47 * @const
48 * @see http://caniuse.com/#search=JSON
49 * @private {boolean}
50 */
51bot.json.SUPPORTS_NATIVE_JSON_ =
52 // List WebKit first since every supported version supports
53 // native JSON (and we can compile away large chunks of code for
54 // individual fragments by setting the appropriate compiler flags).
55 goog.userAgent.WEBKIT ||
56 (goog.userAgent.GECKO && bot.userAgent.isEngineVersion(3.5)) ||
57 (goog.userAgent.IE && bot.userAgent.isEngineVersion(8));
58
59
60/**
61 * Converts a JSON object to its string representation.
62 * @param {*} jsonObj The input object.
63 * @param {?(function(string, *): *)=} opt_replacer A replacer function called
64 * for each (key, value) pair that determines how the value should be
65 * serialized. By default, this just returns the value and allows default
66 * serialization to kick in.
67 * @return {string} A JSON string representation of the input object.
68 */
69bot.json.stringify = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?
70 JSON.stringify : goog.json.serialize;
71
72
73/**
74 * Parses a JSON string and returns the result.
75 * @param {string} jsonStr The string to parse.
76 * @return {*} The JSON object.
77 * @throws {Error} If the input string is an invalid JSON string.
78 */
79bot.json.parse = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?
80 JSON.parse : goog.json.parse;