You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
7.5 KiB

<!DOCTYPE html><meta charset="UTF-8"><meta http-equiv="Content-Language" content="en" /><title>json.js</title><link href="../../../dossier.css" rel="stylesheet" type="text/css"><div id="main-wrapper"><input type="checkbox" id="sidenav-toggle" /><main><header><h1>lib/atoms/json.js</h1></header><pre><table class="srcfile"><tbody><tr><td><a name="l1" href="#l1">1</a><td>// Copyright 2012 WebDriver committers<tr><td><a name="l2" href="#l2">2</a><td>// Copyright 2012 Google Inc.<tr><td><a name="l3" href="#l3">3</a><td>//<tr><td><a name="l4" href="#l4">4</a><td>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);<tr><td><a name="l5" href="#l5">5</a><td>// you may not use this file except in compliance with the License.<tr><td><a name="l6" href="#l6">6</a><td>// You may obtain a copy of the License at<tr><td><a name="l7" href="#l7">7</a><td>//<tr><td><a name="l8" href="#l8">8</a><td>// http://www.apache.org/licenses/LICENSE-2.0<tr><td><a name="l9" href="#l9">9</a><td>//<tr><td><a name="l10" href="#l10">10</a><td>// Unless required by applicable law or agreed to in writing, software<tr><td><a name="l11" href="#l11">11</a><td>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,<tr><td><a name="l12" href="#l12">12</a><td>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<tr><td><a name="l13" href="#l13">13</a><td>// See the License for the specific language governing permissions and<tr><td><a name="l14" href="#l14">14</a><td>// limitations under the License.<tr><td><a name="l15" href="#l15">15</a><td><tr><td><a name="l16" href="#l16">16</a><td>/**<tr><td><a name="l17" href="#l17">17</a><td> * @fileoverview Provides JSON utilities that uses native JSON parsing where<tr><td><a name="l18" href="#l18">18</a><td> * possible (a feature not currently offered by Closure).<tr><td><a name="l19" href="#l19">19</a><td> */<tr><td><a name="l20" href="#l20">20</a><td><tr><td><a name="l21" href="#l21">21</a><td>goog.provide(&#39;bot.json&#39;);<tr><td><a name="l22" href="#l22">22</a><td><tr><td><a name="l23" href="#l23">23</a><td>goog.require(&#39;bot.userAgent&#39;);<tr><td><a name="l24" href="#l24">24</a><td>goog.require(&#39;goog.json&#39;);<tr><td><a name="l25" href="#l25">25</a><td>goog.require(&#39;goog.userAgent&#39;);<tr><td><a name="l26" href="#l26">26</a><td><tr><td><a name="l27" href="#l27">27</a><td><tr><td><a name="l28" href="#l28">28</a><td>/**<tr><td><a name="l29" href="#l29">29</a><td> * @define {boolean} NATIVE_JSON indicates whether the code should rely on the<tr><td><a name="l30" href="#l30">30</a><td> * native {@code JSON} functions, if available.<tr><td><a name="l31" href="#l31">31</a><td> *<tr><td><a name="l32" href="#l32">32</a><td> * &lt;p&gt;The JSON functions can be defined by external libraries like Prototype<tr><td><a name="l33" href="#l33">33</a><td> * and setting this flag to false forces the use of Closure&#39;s goog.json<tr><td><a name="l34" href="#l34">34</a><td> * implementation.<tr><td><a name="l35" href="#l35">35</a><td> *<tr><td><a name="l36" href="#l36">36</a><td> * &lt;p&gt;If your JavaScript can be loaded by a third_party site and you are wary<tr><td><a name="l37" href="#l37">37</a><td> * about relying on the native functions, specify<tr><td><a name="l38" href="#l38">38</a><td> * &quot;--define bot.json.NATIVE_JSON=false&quot; to the Closure compiler.<tr><td><a name="l39" href="#l39">39</a><td> */<tr><td><a name="l40" href="#l40">40</a><td>bot.json.NATIVE_JSON = true;<tr><td><a name="l41" href="#l41">41</a><td><tr><td><a name="l42" href="#l42">42</a><td><tr><td><a name="l43" href="#l43">43</a><td>/**<tr><td><a name="l44" href="#l44">44</a><td> * Whether the current browser supports the native JSON interface.<tr><td><a name="l45" href="#l45">45</a><td> * @const<tr><td><a name="l46" href="#l46">46</a><td> * @see http://caniuse.com/#search=JSON<tr><td><a name="l47" href="#l47">47</a><td> * @private {boolean}<tr><td><a name="l48" href="#l48">48</a><td> */<tr><td><a name="l49" href="#l49">49</a><td>bot.json.SUPPORTS_NATIVE_JSON_ =<tr><td><a name="l50" href="#l50">50</a><td> // List WebKit and Opera first since every supported version of these<tr><td><a name="l51" href="#l51">51</a><td> // browsers supports native JSON (and we can compile away large chunks of<tr><td><a name="l52" href="#l52">52</a><td> // code for individual fragments by setting the appropriate compiler flags).<tr><td><a name="l53" href="#l53">53</a><td> goog.userAgent.WEBKIT || goog.userAgent.OPERA ||<tr><td><a name="l54" href="#l54">54</a><td> (goog.userAgent.GECKO &amp;&amp; bot.userAgent.isEngineVersion(3.5)) ||<tr><td><a name="l55" href="#l55">55</a><td> (goog.userAgent.IE &amp;&amp; bot.userAgent.isEngineVersion(8));<tr><td><a name="l56" href="#l56">56</a><td><tr><td><a name="l57" href="#l57">57</a><td><tr><td><a name="l58" href="#l58">58</a><td>/**<tr><td><a name="l59" href="#l59">59</a><td> * Converts a JSON object to its string representation.<tr><td><a name="l60" href="#l60">60</a><td> * @param {*} jsonObj The input object.<tr><td><a name="l61" href="#l61">61</a><td> * @param {?(function(string, *): *)=} opt_replacer A replacer function called<tr><td><a name="l62" href="#l62">62</a><td> * for each (key, value) pair that determines how the value should be<tr><td><a name="l63" href="#l63">63</a><td> * serialized. By default, this just returns the value and allows default<tr><td><a name="l64" href="#l64">64</a><td> * serialization to kick in.<tr><td><a name="l65" href="#l65">65</a><td> * @return {string} A JSON string representation of the input object.<tr><td><a name="l66" href="#l66">66</a><td> */<tr><td><a name="l67" href="#l67">67</a><td>bot.json.stringify = bot.json.NATIVE_JSON &amp;&amp; bot.json.SUPPORTS_NATIVE_JSON_ ?<tr><td><a name="l68" href="#l68">68</a><td> JSON.stringify : goog.json.serialize;<tr><td><a name="l69" href="#l69">69</a><td><tr><td><a name="l70" href="#l70">70</a><td><tr><td><a name="l71" href="#l71">71</a><td>/**<tr><td><a name="l72" href="#l72">72</a><td> * Parses a JSON string and returns the result.<tr><td><a name="l73" href="#l73">73</a><td> * @param {string} jsonStr The string to parse.<tr><td><a name="l74" href="#l74">74</a><td> * @return {*} The JSON object.<tr><td><a name="l75" href="#l75">75</a><td> * @throws {Error} If the input string is an invalid JSON string.<tr><td><a name="l76" href="#l76">76</a><td> */<tr><td><a name="l77" href="#l77">77</a><td>bot.json.parse = bot.json.NATIVE_JSON &amp;&amp; bot.json.SUPPORTS_NATIVE_JSON_ ?<tr><td><a name="l78" href="#l78">78</a><td> JSON.parse : goog.json.parse;</table></pre></main><nav id="topnav"><div><div id="menubutton"><label for="sidenav-toggle">Menu</label></div><form id="searchbox"><div><input type="search" placeholder="Search" tabindex="1"></div></form></div></nav><nav id="sidenav"><input type="checkbox" id="sidenav-types-ctrl" /><input type="checkbox" id="sidenav-files-ctrl" /><input type="checkbox" id="sidenav-modules-ctrl" /><a id="sidenav-overview"><div><h4>Overview</h4></div></a><div id="sidenav-types"><label for="sidenav-types-ctrl"><h4>Types</h4></label><i>No data</i></div><div id="sidenav-modules"><label for="sidenav-modules-ctrl"><h4>Modules</h4></label><i>No data</i></div><div id="sidenav-files"><label for="sidenav-files-ctrl"><h4>Files</h4></label><i>No data</i></div><a href="license.html"><div><h4>License</h4></div></a></nav><div id="push-footer"></div></div><footer><a href="https://github.com/jleyba/js-dossier">Generated by dossier</a></footer><script src="../../../types.js"></script><script src="../../../dossier.js"></script>