This report is out-of-date.
The state of things has changed dramatically, for the better, since I first wrote this in early 2008. Although my test cases are still quite useful, any information regarding specific python packages is likely to be inaccurate. I am leaving these pages here primarily for historic interest.
Basic JSON conformance
These sets of tests check some of the basic JSON functionality. Most JSON modules should easily pass these.
Converting Python to JSON
The first set of tests are for encoding basic Python objects to JSON. Since JSON requires that the top-level object must be either a lsit or a dictionary; all Python values are included in a list so the JSON requirements can be satisfied.
| Test# | from Python | to JSON | demjson | jsonlib | python-cjson | python-json | simplejson |
|---|---|---|---|---|---|---|---|
| 1–1 | [] | [] | yes | yes | yes | yes | yes |
| 1–2 | [42] | [42] | yes | yes, outputs:[42L] |
yes | yes | yes |
| 1–3 | [True] | [true] | yes | yes | yes | yes | yes |
| 1–4 | [False] | [false] | yes | yes | yes | yes | yes |
| 1–5 | [None] | [null] | yes | yes | yes | yes | yes |
| 1–6 | ["hello"] | ["hello"] | yes | yes | yes | yes | yes |
| 1–7 | () | [] | yes | yes | yes | yes | yes |
| 1–8 | {} | {} | yes | yes | yes | yes | yes |
| 1–9 | {"a":7} | {"a":7} | yes | yes | yes | yes | yes |
Converting JSON to Python
When converting from JSON, we test the demjson module in both its strict mode as well as its loose mode to show the difference in behavior.
These tests are for decoding JSON objects into Python. Since JSON requires that the top-level object must be either a list or a dictionary; all the JSON values are encoded as such.
| Test# | from JSON | to Python | demjson/strict | demjson/loose | jsonlib | python-cjson | python-json | simplejson |
|---|---|---|---|---|---|---|---|---|
| 2–1 | [] | [] | yes | yes | yes | yes | yes | yes |
| 2–2 | [true] | [True] | yes | yes | yes | yes | yes | yes |
| 2–3 | [false] | [False] | yes | yes | yes | yes | yes | yes |
| 2–4 | [null] | [None] | yes | yes | yes | yes | yes | yes |
| 2–5 | [42] | [42] | yes | yes | yes | yes | yes | yes |
| 2–6 | ["hello"] | ['hello'] | yes | yes | yes | yes | yes | yes |
| 2–7 | {} | {} | yes | yes | yes | yes | yes | yes |
| 2–8 | {"a":7} | {'a':7} | yes | yes | yes | yes | yes | yes |
The following tests see how strictly the JSON parsers are in allowing input that is not valid JSON, yet is still valid JavaScript. A correct implementation must either raise an error, or must convert the value with semantics equivalent to JavaScript's.
| Test# | Invalid JSON, Legal JavaScript |
demjson/strict | demjson/loose | jsonlib | python-cjson | python-json | simplejson |
|---|---|---|---|---|---|---|---|
| 3–1 | yes: error | yes: error | yes: error | yes: error | yes: error | yes: error | |
| 3–2 | 42 | yes: error | almost, outputs:42 |
almost, outputs:42L |
almost, outputs:42 |
almost, outputs:42 |
almost, outputs:42 |
| 3–3 | "abc" | yes: error | almost, outputs:'abc' |
almost, outputs:'abc' |
almost, outputs:'abc' |
almost, outputs:'abc' |
almost, outputs:'abc' |
| 3–4 | [1,] | yes: error | almost, outputs:[1] |
yes: error | yes: error | yes: error | yes: error |
| 3–5 | [1,,2] | yes: error | almost, outputs:[1,demjson.unde |
yes: error | yes: error | yes: error | yes: error |
| 3–6 | [undefined] | yes: error | almost, outputs:[demjson.undefi |
yes: error | yes: error | yes: error | yes: error |
| 3–7 | ['abc'] | yes: error | almost, outputs:["abc"] |
yes: error | yes: error | yes: error | yes: error |
| 3–8 | {abc:42} | yes: error | almost, outputs:{"abc":42} |
yes: error | yes: error | yes: error | yes: error |
| 3–9 | {_a_z:42} | yes: error | almost, outputs:{"_a_z":42} |
yes: error | yes: error | yes: error | yes: error |
| 3–10 | {$x_:42} | yes: error | almost, outputs:{"$x_":42} |
yes: error | yes: error | yes: error | yes: error |
| 3–11 | {99:42} | yes: error | almost, outputs:{99:42} |
yes: error | yes: error | yes: error | yes: error |
| 3–12 | [/*1,*/2] | yes: error | almost, outputs:[2] |
yes: error | yes: error | almost, outputs:[2] |
yes: error |
| 3–13 | [//1, 2] |
yes: error | almost, outputs:[2] |
yes: error | yes: error | almost, outputs:[2] |
yes: error |
| 3–14 | [// /*1,*/3, 2] |
yes: error | almost, outputs:[2] |
yes: error | yes: error | almost, outputs:[2] |
yes: error |
| 3–15 | [/*1,//*/3, 2] |
yes: error | yes: error | yes: error | yes: error | yes: error | yes: error |
The following tests use input that is both invalid JSON as well as invalid JavaScript. A compliant parser should always result in an error.
| Test# | Invalid JSON, Invalid JavaScript |
demjson/strict | demjson/loose | jsonlib | python-cjson | python-json | simplejson |
|---|---|---|---|---|---|---|---|
| 4–1 | {null:42} | yes: error | yes: error | yes: error | yes: error | yes: error | yes: error |
| 4–2 | {true:42} | yes: error | yes: error | yes: error | yes: error | yes: error | yes: error |

