jsonx API 参考¶
自动生成的 jsonx(扩展 JSON)解析器模块 API 文档。
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
JSONC source string. |
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
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.
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: |
{}
|