Skip to main content
The lodum.json module provides JSON serialization and deserialization for lodum-enabled classes with support for both in-memory and streaming operations.

Functions

dump

def dump(
    obj: Any,
    target: Optional[Union[IO[str], Path]] = None,
    **kwargs
) -> Optional[str]
Encodes a Python object to JSON.
obj
Any
required
The object to encode.
target
Optional[Union[IO[str], Path]]
Optional file-like object or Path to write to. If provided, uses O(1) streaming mode.
**kwargs
dict
Additional arguments for json.dump(s) (e.g., indent). Note: If target is provided, O(1) streaming is used and some formatting kwargs might be ignored in the current implementation.
Returns: The JSON string if target is None, otherwise None. Example:
import lodum
from lodum import json

@lodum
class Person:
    name: str
    age: int

person = Person(name="Alice", age=30)

# Serialize to string
json_str = json.dump(person, indent=2)

# Serialize to file
json.dump(person, Path("person.json"))

dumps

def dumps(obj: Any, **kwargs) -> str
Legacy alias for dump(obj).
obj
Any
required
The object to encode.
**kwargs
dict
Additional arguments for json.dumps.
Returns: The JSON string.

load

def load(
    cls: Type[T],
    source: Union[str, IO[Any], Path],
    max_size: int = DEFAULT_MAX_SIZE
) -> T
Decodes JSON from a string, stream, or file into a Python object.
cls
Type[T]
required
The class to instantiate.
source
Union[str, IO[Any], Path]
required
JSON string, file-like object, or Path.
max_size
int
default:"DEFAULT_MAX_SIZE"
Maximum allowed size for string input.
Returns: An instance of cls. Raises: DeserializationError if the input is invalid or exceeds max_size. Example:
import lodum
from lodum import json

@lodum
class Person:
    name: str
    age: int

# Load from string
json_str = '{"name": "Alice", "age": 30}'
person = json.load(Person, json_str)

# Load from file
person = json.load(Person, Path("person.json"))

loads

def loads(cls: Type[T], json_string: str, **kwargs) -> T
Legacy alias for load(cls, source).
cls
Type[T]
required
The class to instantiate.
json_string
str
required
JSON string to parse.
**kwargs
dict
Additional arguments (e.g., max_size).
Returns: An instance of cls.

stream

def stream(cls: Type[T], source: Union[IO[bytes], Path]) -> Iterator[T]
Lazily decodes a stream of JSON objects into instances of cls. Intended for sources containing a top-level array of objects.
cls
Type[T]
required
The class to instantiate for each item.
source
Union[IO[bytes], Path]
required
A binary stream, file-like object, or Path to a JSON array.
Returns: An iterator yielding instances of cls. Raises: RuntimeError if ijson is not installed. Install with: pip install lodum[ijson] Example:
import lodum
from lodum import json

@lodum
class Person:
    name: str
    age: int

# Stream from file containing JSON array
for person in json.stream(Person, Path("people.json")):
    print(person.name, person.age)

load_stream

def load_stream(cls: Type[T], stream_io: IO[bytes]) -> Iterator[T]
Legacy alias for stream(cls, source).
cls
Type[T]
required
The class to instantiate for each item.
stream_io
IO[bytes]
required
A binary stream.
Returns: An iterator yielding instances of cls.

schema

def schema(cls: Type[Any]) -> Dict[str, Any]
Generates a JSON Schema for a given lodum-enabled class.
cls
Type[Any]
required
The lodum-enabled class.
Returns: A dictionary representing the JSON Schema. Example:
import lodum
from lodum import json

@lodum
class Person:
    name: str
    age: int

schema_dict = json.schema(Person)
print(schema_dict)

Build docs developers (and LLMs) love