A small C extension for Python that parses and serializes JSON objects composed of string keys and integer or string values. It embeds directly into your Python interpreter via the standard C‑API, exposing two functions: loads to convert a JSON text starting with {…} into a native dict, and dumps to turn a dict back into a JSON string with spaces after commas and colons, matching the style of the built‑in json module.
make # setup .venv, install ujson/pytest, build & install cjson
make test # run unit tests via pytest
make perf # benchmark cjson.loads vs json.loads and ujson.loads
make clean # purge build artifacts and delete .venvmake test will run a suite of pytest cases in tests/test_cjson.py. These verify that valid JSON strings like
json_str = '{"hello": 10, "world": "value"}'produce the same dict under cjson.loads as under the standard json and ujson libraries, and that cjson.dumps emits
'{"hello": 10, "world": "value"}'exactly. Any malformed input—missing braces, unexpected characters, or out‑of‑range indices—raises a ValueError with a precise message indicating the error position.
To benchmark parsing speed against the built‑in json and the third‑party ujson library, run make perf.
A script will create a single large JSON object with 100 000 entries, then times each library’s loads function over several iterations. On tested hardware, cjson.loads completes in around 0.08 s, outperforming both json.loads (~0.16 s) and ujson.loads (~0.09 s) on the same data set.
make perf
# Output:
json.loads: 0.158s
ujson.loads: 0.091s
cjson.loads: 0.079simport cjson
text = '{"name": "Alice", "score": 42}'
data = cjson.loads(text)
assert data["name"] == "Alice" and data["score"] == 42
output = cjson.dumps(data)
# output == '{"name": 42, "score": 42}' – matches built‑in spacingErrors such as cjson.loads("bad") will raise ValueError so you can catch and handle malformed JSON gracefully.