Json io
Helpers for loading, dumping, and streaming JSON and JSONL data with pydantic_core.
json_dump_bytes(obj: Any, indent: int | None = None, **kwargs: Any) -> bytes
¶
Serialize a Python object to JSON bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to serialize. |
required |
indent
|
int | None
|
Optional indentation level passed to pydantic_core.to_json(). |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to pydantic_core.to_json(). |
{}
|
Returns:
| Type | Description |
|---|---|
bytes
|
JSON document as bytes. |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
json_dumps(obj: Any, indent: int | None = None, encoding: str = 'utf-8', **kwargs: Any) -> str
¶
Serialize a Python object to JSON text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to serialize. |
required |
indent
|
int | None
|
Optional indentation level passed to pydantic_core.to_json(). |
None
|
encoding
|
str
|
Text encoding used to decode the serialized JSON bytes. |
'utf-8'
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to pydantic_core.to_json(). |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
JSON document as a string. |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
json_dumps_path(obj: Any, *, filepath: Path, overwrite: bool = False, encoding: str = 'utf-8', indent: int | None = None, **kwargs: Any) -> int
¶
Dump a Python object to a JSON file.
Uses pydantic_core.to_json() to serialize the object and writes a trailing newline after the JSON document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The Python object to dump. |
required |
filepath
|
Path
|
The path to the JSON file. |
required |
overwrite
|
bool
|
Whether to overwrite the file if it exists. |
False
|
encoding
|
str
|
The encoding to use when writing the file. |
'utf-8'
|
indent
|
int | None
|
The indentation level for the JSON file. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
The number of characters written to the file, including the trailing |
int
|
newline. |
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If the file already exists and |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
json_load_path(filepath: Path) -> Any
¶
json_loads(json_string: str | bytes) -> Any
¶
Parse a JSON text or byte payload into a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_string
|
str | bytes
|
JSON document encoded as text or bytes. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed Python object produced by pydantic_core.from_json(). |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_dump_bytes(objs: Iterator[Any], **kwargs: Any) -> bytes
¶
Serialize an iterator of objects to JSONL bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objs
|
Iterator[Any]
|
Iterator of Python objects to serialize as JSONL. |
required |
**kwargs
|
Any
|
Additional keyword arguments forwarded to json_dump_bytes(). |
{}
|
Returns:
| Type | Description |
|---|---|
bytes
|
JSONL document as bytes with one serialized object per line. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_dump_path(objs: Iterator[Any], *, filepath: Path, encoding: str = 'utf-8', overwrite: bool = False, append: bool = False, **kwargs: Any) -> int
¶
Write JSONL output from an iterator of Python objects.
Serializes one object per line without materializing the full output in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objs
|
Iterator[Any]
|
Iterator of Python objects to serialize as JSONL. |
required |
filepath
|
Path
|
The path to the JSONL file. |
required |
encoding
|
str
|
The encoding to use when writing the file. |
'utf-8'
|
overwrite
|
bool
|
Whether to overwrite the file if it exists. |
False
|
append
|
bool
|
Whether to append to the file if it exists. |
False
|
**kwargs
|
Any
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
Number of characters written to the file, including newline |
int
|
separators. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FileExistsError
|
If the file already exists and neither overwrite nor append permits writing. |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_dumps(objs: Iterator[Any], encoding: str = 'utf-8', **kwargs: Any) -> str
¶
Serialize an iterator of objects to a JSONL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objs
|
Iterator[Any]
|
Iterator of Python objects to serialize as JSONL. |
required |
encoding
|
str
|
Text encoding used when converting each JSON document. |
'utf-8'
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to json_dumps(). |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
JSONL document with one serialized object per line. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_load_bytes(jsonl_bytes: bytes) -> Iterator[Any]
¶
Yield Python objects parsed lazily from non-empty JSONL byte lines.
jsonl_load_bytes_indexed(jsonl_bytes: bytes) -> Iterator[tuple[int, Any]]
¶
Yield line-number and object pairs from non-empty JSONL byte lines.
Line numbers correspond to the original input line positions, including blank lines.
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_load_path(filepath: Path) -> Iterator[Any]
¶
Yield Python objects parsed lazily from non-empty lines in a JSONL file.
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_load_path_indexed(filepath: Path) -> Iterator[tuple[int, Any]]
¶
Yield line-number and object pairs parsed from a JSONL file.
Line numbers correspond to the original file line positions, including blank lines.
Source code in src/pfmsoft/eve_snippets/pydantic/json_io.py
jsonl_loads(jsonl_string: str) -> Iterator[Any]
¶
Yield Python objects parsed lazily from non-empty JSONL text lines.
jsonl_loads_indexed(jsonl_string: str) -> Iterator[tuple[int, Any]]
¶
Yield line-number and object pairs from non-empty JSONL text lines.
Line numbers correspond to the original input line positions, including blank lines.