Skip to content

C extension for Python providing blazing‑fast JSON parsing and serialization of string‑keyed objects with integer and string values. Outperforms ujson and json python-libraries.

Notifications You must be signed in to change notification settings

estnafinema0/cpp-cjson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C JSON Extension (cjson)

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.

Build and Install

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 .venv

Correctness Tests

make 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.

Performance Comparison

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.079s

Quick Example

import 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 spacing

Errors such as cjson.loads("bad") will raise ValueError so you can catch and handle malformed JSON gracefully.

About

C extension for Python providing blazing‑fast JSON parsing and serialization of string‑keyed objects with integer and string values. Outperforms ujson and json python-libraries.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published