Skip to content

Commit 77b81d0

Browse files
committed
Lists all possible return types for $.parseJSON
Also formats "malformed JSON strings" examples to appear as actual strings, and adds a few more invalid cases to the list. Closes jquery#490
1 parent 1b7e2e7 commit 77b81d0

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

entries/jQuery.parseJSON.xml

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
<?xml version="1.0"?>
2-
<entry type="method" name="jQuery.parseJSON" return="Object">
2+
<entry type="method" name="jQuery.parseJSON">
3+
<return type="String"/>
4+
<return type="Number"/>
5+
<return type="Object"/>
6+
<return type="Array"/>
7+
<return type="Boolean"/>
38
<title>jQuery.parseJSON()</title>
49
<signature>
510
<added>1.4.1</added>
611
<argument name="json" type="String">
712
<desc>The JSON string to parse.</desc>
813
</argument>
914
</signature>
10-
<desc>Takes a well-formed JSON string and returns the resulting JavaScript object.</desc>
15+
<desc>Takes a well-formed JSON string and returns the resulting JavaScript value.</desc>
1116
<longdesc>
12-
<p>Passing in a malformed JSON string results in a JavaScript exception being thrown. For example, the following are all malformed JSON strings:</p>
17+
<p>Passing in a malformed JSON string results in a JavaScript exception being thrown. For example, the following are all invalid JSON strings:</p>
1318
<ul>
14-
<li><code>{test: 1}</code> (test does not have double quotes around it).</li>
15-
<li><code>{'test': 1}</code> ('test' is using single quotes instead of double quotes).</li>
19+
<li><code>"{test: 1}"</code> (test does not have double quotes around it).</li>
20+
<li><code>"{'test': 1}"</code> ('test' is using single quotes instead of double quotes).</li>
21+
<li><code>"'test'"</code> ('test' is using single quotes instead of double quotes).</li>
22+
<li><code>".1"</code> (a numbers must start with a digit; <code>"0.1"</code> would be valid).</li>
23+
<li><code>"undefined"</code> (<code>undefined</code> cannot be represented in a JSON string; <code>null</code>, however, can be).</li>
24+
<li><code>"NaN"</code> (<code>NaN</code> cannot be represented in a JSON string; direct representation of <code>Infinity</code> is also not permitted).</li>
1625
</ul>
1726
<p>The JSON standard does not permit "control characters" such as a tab or newline. An example like <code>$.parseJSON( '{ "testing":"1\t2\n3" }' )</code> will throw an error in most implementations because the JavaScript parser converts the string's tab and newline escapes into literal tab and newline; doubling the backslashes like <code>"1\\t2\\n3"</code> yields expected results. This problem is often seen when injecting JSON into a JavaScript file from a server-side language such as PHP.</p>
1827
<p>Where the browser provides a native implementation of <code>JSON.parse</code>, jQuery uses it to parse the string. For details on the JSON format, see <a href="http://json.org/">http://json.org/</a>.</p>
19-
<p>Prior to jQuery 1.9, <code>$.parseJSON</code> returned <code>null</code> instead of throwing an error if it was passed an empty string, <code>null</code>, or <code>undefined</code>, even though those are not valid JSON. </p>
28+
<p>Prior to jQuery 1.9, <code>$.parseJSON</code> returned <code>null</code> instead of throwing an error if it was passed an empty string, <code>null</code>, or <code>undefined</code>, even though those are not valid JSON.</p>
2029
</longdesc>
2130
<example>
2231
<desc>Parse a JSON string.</desc>

0 commit comments

Comments
 (0)