# lib/atoms/json.js

1 | // Copyright 2012 WebDriver committers
2 | // 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 at
7 | //
8 | // http://www.apache.org/licenses/LICENSE-2.0
9 | //
10 | // Unless required by applicable law or agreed to in writing, software
11 | // 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 and
14 | // limitations under the License.
15 |
16 | /**
17 | * @fileoverview Provides JSON utilities that uses native JSON parsing where
18 | * possible (a feature not currently offered by Closure).
19 | */
20 |
21 | goog.provide('bot.json');
22 |
23 | goog.require('bot.userAgent');
24 | goog.require('goog.json');
25 | goog.require('goog.userAgent');
26 |
27 |
28 | /**
29 | * @define {boolean} NATIVE_JSON indicates whether the code should rely on the
30 | * native {@code JSON} functions, if available.
31 | *
32 | * <p>The JSON functions can be defined by external libraries like Prototype
33 | * and setting this flag to false forces the use of Closure's goog.json
34 | * implementation.
35 | *
36 | * <p>If your JavaScript can be loaded by a third_party site and you are wary
37 | * about relying on the native functions, specify
38 | * "--define bot.json.NATIVE_JSON=false" to the Closure compiler.
39 | */
40 | bot.json.NATIVE_JSON = true;
41 |
42 |
43 | /**
44 | * Whether the current browser supports the native JSON interface.
45 | * @const
46 | * @see http://caniuse.com/#search=JSON
47 | * @private {boolean}
48 | */
49 | bot.json.SUPPORTS_NATIVE_JSON_ =
50 | // List WebKit first since every supported version supports
51 | // native JSON (and we can compile away large chunks of code for
52 | // individual fragments by setting the appropriate compiler flags).
53 | goog.userAgent.WEBKIT ||
54 | (goog.userAgent.GECKO && bot.userAgent.isEngineVersion(3.5)) ||
55 | (goog.userAgent.IE && bot.userAgent.isEngineVersion(8));
56 |
57 |
58 | /**
59 | * Converts a JSON object to its string representation.
60 | * @param {*} jsonObj The input object.
61 | * @param {?(function(string, *): *)=} opt_replacer A replacer function called
62 | * for each (key, value) pair that determines how the value should be
63 | * serialized. By default, this just returns the value and allows default
64 | * serialization to kick in.
65 | * @return {string} A JSON string representation of the input object.
66 | */
67 | bot.json.stringify = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?
68 | JSON.stringify : goog.json.serialize;
69 |
70 |
71 | /**
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 | */
77 | bot.json.parse = bot.json.NATIVE_JSON && bot.json.SUPPORTS_NATIVE_JSON_ ?
78 | JSON.parse : goog.json.parse;