jsonx API Reference¶
Auto-generated API documentation for the jsonx (extended JSON) parser module.
jsonx
¶
Extended JSON parser — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Drop-in replacement for commentjson / stdlib json with JSONC support, plus JSONL (JSON Lines) helpers.
Supports
- Single-line comments:
//and# - Block comments:
/* ... */ - Trailing commas in objects and arrays
- All standard JSON types
- JSONL (one JSON value per line, with optional comments)
Example::
loads('{"a": 1, // comment
"b": 2}') # {'a': 1, 'b': 2} load(open("config.jsonc")) # {...}
# JSONL with comments
loads_lines('{"a":1}
// skip {"b":2}') # [{'a': 1}, {'b': 2}]
JSONCDecodeError
¶
Bases: JSONDecodeError
Error raised when JSONC parsing fails.
Provides line and column numbers relative to the original JSONC source (before comment/trailing-comma stripping).
loads(text, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kwargs)
¶
Deserialize a JSONC string to a Python object.
Strips //, #, and /* */ comments and trailing commas before
delegating to :func:json.loads. Documents that already parse as plain
JSON skip the preprocessing pass entirely; a custom decoder or hook opts
out of that fast path, so it never parses the same text twice.
Hooks (object_hook, object_pairs_hook, parse_float, …) must
not raise :class:json.JSONDecodeError to reject a document: that
exception is the syntax-error signal, so a hook raising it is
indistinguishable from a parse failure. Raise ValueError instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
JSONC source string. Only |
required |
cls
|
type[JSONDecoder] | None
|
Custom JSON decoder class. |
None
|
object_hook
|
Any
|
Called with the result of any object literal decoded. |
None
|
parse_float
|
Any
|
Called with every JSON float string decoded. |
None
|
parse_int
|
Any
|
Called with every JSON int string decoded. |
None
|
parse_constant
|
Any
|
Called with |
None
|
object_pairs_hook
|
Any
|
Called with an ordered list of pairs. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If the text is not valid JSONC. |
Source code in jsonx/jsonx.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kwargs)
¶
Deserialize a JSONC file to a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
IO[str]
|
A text file-like object containing JSONC. |
required |
cls
|
type[JSONDecoder] | None
|
Custom JSON decoder class. |
None
|
object_hook
|
Any
|
Called with the result of any object literal decoded. |
None
|
parse_float
|
Any
|
Called with every JSON float string decoded. |
None
|
parse_int
|
Any
|
Called with every JSON int string decoded. |
None
|
parse_constant
|
Any
|
Called with |
None
|
object_pairs_hook
|
Any
|
Called with an ordered list of pairs. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If the content is not valid JSONC. |
Source code in jsonx/jsonx.py
dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kwargs)
¶
Serialize a Python object to a JSON string.
This is a pass-through to :func:json.dumps for API compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to serialize. |
required |
skipkeys
|
bool
|
Skip keys that are not basic types. |
False
|
ensure_ascii
|
bool
|
Escape non-ASCII characters. |
True
|
check_circular
|
bool
|
Check for circular references. |
True
|
allow_nan
|
bool
|
Allow |
True
|
cls
|
type[JSONEncoder] | None
|
Custom JSON encoder class. |
None
|
indent
|
int | str | None
|
Indentation level for pretty-printing. |
None
|
separators
|
tuple[str, str] | None
|
Item and key separators. |
None
|
default
|
Any
|
Called for objects that are not serializable. |
None
|
sort_keys
|
bool
|
Sort dictionary keys. |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string. |
Source code in jsonx/jsonx.py
dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kwargs)
¶
Serialize a Python object to a JSON file.
This is a pass-through to :func:json.dump for API compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to serialize. |
required |
fp
|
IO[str]
|
A text file-like object to write to. |
required |
skipkeys
|
bool
|
Skip keys that are not basic types. |
False
|
ensure_ascii
|
bool
|
Escape non-ASCII characters. |
True
|
check_circular
|
bool
|
Check for circular references. |
True
|
allow_nan
|
bool
|
Allow |
True
|
cls
|
type[JSONEncoder] | None
|
Custom JSON encoder class. |
None
|
indent
|
int | str | None
|
Indentation level for pretty-printing. |
None
|
separators
|
tuple[str, str] | None
|
Item and key separators. |
None
|
default
|
Any
|
Called for objects that are not serializable. |
None
|
sort_keys
|
bool
|
Sort dictionary keys. |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to :func: |
{}
|
Source code in jsonx/jsonx.py
loads_lines(text, **kwargs)
¶
Deserialize a JSONL string to a list of Python objects.
Uses a batch fast-path for clean JSONL: non-empty lines are joined
into a JSON array and parsed in a single :func:json.loads call,
matching ndjson-level performance. If that fails (comments,
trailing commas, etc.), falls back to per-line JSONC processing.
A custom decoder or hook opts out of the batch fast-path, so the
callback is never invoked on a speculative parse and a hook that raises
:class:json.JSONDecodeError is not retried.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
JSONL source string (one JSON value per line). |
required |
**kwargs
|
Any
|
Keyword arguments forwarded to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of deserialized Python objects. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If any line contains invalid JSON/JSONC. |
Source code in jsonx/jsonx.py
load_lines(fp, **kwargs)
¶
Deserialize a JSONL file to a list of Python objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp
|
IO[str]
|
A text file-like object containing JSONL. |
required |
**kwargs
|
Any
|
Keyword arguments forwarded to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of deserialized Python objects. |
Raises:
| Type | Description |
|---|---|
JSONCDecodeError
|
If any line contains invalid JSON/JSONC. |
Source code in jsonx/jsonx.py
dumps_lines(objects, **kwargs)
¶
Serialize a list of Python objects to a JSONL string.
Each object is serialized on its own line using :func:json.dumps.
No trailing newline is emitted (consistent with "\n".join(...)
semantics).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[Any]
|
Iterable of Python objects to serialize. |
required |
**kwargs
|
Any
|
Keyword arguments forwarded to :func: |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
JSONL string (one JSON value per line). |
Source code in jsonx/jsonx.py
dump_lines(objects, fp, **kwargs)
¶
Serialize a list of Python objects to a JSONL file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[Any]
|
Iterable of Python objects to serialize. |
required |
fp
|
IO[str]
|
A text file-like object to write to. |
required |
**kwargs
|
Any
|
Keyword arguments forwarded to :func: |
{}
|