Registry / serialization / demjson3

demjson3

JSON →
library3.0.6pypypi✓ verified 88d ago

demjson3 is a Python 3 specific library for encoding, decoding, and validating JSON data, fully compliant with RFC 7159. It is a fork of the `demjson` project, updated to address Python 3 compatibility issues and provide enhanced error handling and linting capabilities. It offers both strict JSON parsing and a non-strict mode for more JavaScript-like syntax. The library is currently at version 3.0.6, with its last release in October 2022.

pip install demjson3
INSTALL
IMPORT
SIG · DEMJSON3
D
demjson3
serializationpythonv3.0.6
Install
2.5s avg
Import
10ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.0.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.011s · 19.6MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 2.5s · import 0.010s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

JSON
✓ from demjson3 import JSON
✗ import demjson3
encode
✓ from demjson3 import JSON
✗ import demjson3
decode
✓ from demjson3 import JSON
✗ import demjson3

This quickstart demonstrates basic encoding and decoding of JSON data using `demjson3`. It shows how to convert Python dictionaries and lists to JSON strings and vice-versa. It also includes an example of using the `strict=False` option to parse more lenient, JavaScript-like JSON with features not allowed by RFC 7159.

import demjson3 # Encode Python data to JSON string python_data = {'name': 'Alice', 'age': 30, 'isStudent': False, 'courses': ['Math', 'Science'], 'extra': None} json_output = demjson3.encode(python_data, indent=2, strict=True) print('Encoded JSON:') print(json_output) # Decode JSON string to Python data json_input = '{"product": "Laptop", "price": 1200.50, "inStock": true}' python_decoded = demjson3.decode(json_input) print('\nDecoded Python data:') print(python_decoded) # Example of non-strict decoding (allowing comments and unquoted keys) non_strict_json = "{ /* A comment */ myKey: 'value', another: undefined }" python_non_strict = demjson3.decode(non_strict_json, strict=False) print('\nDecoded non-strict JSON:') print(python_non_strict)
demjson3 --version
Debug
Known issues
breakingUsers migrating from the original `demjson` library to `demjson3` (especially in Python 3 environments) should update their `import` statements. `demjson3` is a dedicated Python 3 fork, resolving compatibility issues (`use_2to3` errors with newer `setuptools`) that plagued `demjson` in Python 3.
fix
For Python 3 projects, replace `demjson` with `demjson3` in `requirements.txt` and use `import demjson3`. If legacy code expects the `demjson` name, use `import demjson3 as demjson`.
affects: demjson (all versions on Python 3); demjson3 (n/a, it's the solution)
gotcha`demjson3` defaults to strict RFC 7159 compliance. This means non-standard JavaScript syntax (e.g., comments, unquoted keys, single-quoted strings, hexadecimal numbers, `undefined` keyword) will raise errors during decoding.
fix
If parsing JavaScript-like input that is not strictly JSON, set `strict=False` in the `decode()` method (e.g., `demjson3.decode(json_string, strict=False)`).
affects: All versions of demjson3
gotchaWhen implementing `json_equivalent()` on custom Python classes for encoding, this method must return standard Python objects (dictionaries, lists, primitives) that `demjson3` can then encode. It should *not* return an already JSON-formatted string, as this will lead to double-encoding or incorrect output.
fix
Ensure `json_equivalent()` returns Python data structures, not `str`.
affects: All versions of demjson3
gotcha`demjson3` automatically converts numbers that would lose precision as standard Python `float` types into `decimal.Decimal` objects during decoding to preserve accuracy.
fix
Be aware that numerical data might be returned as `Decimal` objects. Adjust downstream processing to handle `decimal.Decimal` types, or explicitly convert them to `float` if precision loss is acceptable.
affects: All versions of demjson3
Upgrade
Version history
3.0.6latest on PyPI · released Oct 22, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
24 hits · last 30 days
node
24
Resources
demjson3 — pip install demjson3 · libregistry