Serializer Module API Reference¶
This section documents the internals of the serializer
module in Bijux CLI.
bijux_cli.infra.serializer ¶
Provides concrete serialization services for JSON and YAML formats.
This module defines concrete implementations of the SerializerProtocol
. It offers different serializers optimized for performance and specific formats, gracefully handling optional dependencies.
Key components include
OrjsonSerializer
: A high-performance serializer that usesorjson
for JSON serialization if installed, falling back to the standardjson
module. It usesPyYAML
for YAML.PyYAMLSerializer
: A serializer that exclusively handles the YAML format.Redacted
: A special string subclass for wrapping sensitive data to prevent it from being exposed in serialized output.serializer_for
: A factory function that returns the most appropriate serializer instance for a given format.
OrjsonSerializer ¶
OrjsonSerializer(telemetry: TelemetryProtocol | None)
Bases: _Base
A serializer that uses orjson
for JSON and PyYAML
for YAML.
This implementation prioritizes performance by using orjson
for JSON operations if it is installed, gracefully falling back to the standard library's json
module if it is not.
Source code in src/bijux_cli/infra/serializer.py
dumps ¶
dumps(
obj: Any,
*,
fmt: OutputFormat = JSON,
pretty: bool = False,
) -> str
Serializes an object to a string.
Parameters:
-
obj
(Any
) –The object to serialize.
-
fmt
(OutputFormat
, default:JSON
) –The desired output format.
-
pretty
(bool
, default:False
) –If True, formats the output for human readability.
Returns:
-
str
(str
) –The serialized string.
Raises:
-
BijuxError
–If the format is unsupported or serialization fails.
Source code in src/bijux_cli/infra/serializer.py
dumps_bytes ¶
dumps_bytes(
obj: Any,
*,
fmt: OutputFormat = JSON,
pretty: bool = False,
) -> bytes
Serializes an object to bytes.
Parameters:
-
obj
(Any
) –The object to serialize.
-
fmt
(OutputFormat
, default:JSON
) –The desired output format.
-
pretty
(bool
, default:False
) –If True, formats the output for human readability.
Returns:
-
bytes
(bytes
) –The serialized bytes.
Raises:
-
BijuxError
–If the format is unsupported or serialization fails.
Source code in src/bijux_cli/infra/serializer.py
loads ¶
loads(
data: str | bytes,
*,
fmt: OutputFormat = JSON,
pretty: bool = False,
) -> Any
Deserializes data into a Python object.
Parameters:
-
data
(str | bytes
) –The string or bytes to deserialize.
-
fmt
(OutputFormat
, default:JSON
) –The format of the input data.
-
pretty
(bool
, default:False
) –A hint that may affect parsing (often unused).
Returns:
-
Any
(Any
) –The deserialized object.
Raises:
-
BijuxError
–If the format is unsupported or deserialization fails.
Source code in src/bijux_cli/infra/serializer.py
PyYAMLSerializer ¶
PyYAMLSerializer(telemetry: TelemetryProtocol | None)
Bases: _Base
A serializer that exclusively uses the PyYAML
library for YAML.
Attributes:
-
_patched
(bool
) –A class-level flag to ensure that custom YAML representers are only registered once.
Initializes the PyYAMLSerializer
.
This also registers a custom YAML representer for the Redacted
type on first instantiation.
Parameters:
-
telemetry
(TelemetryProtocol | None
) –The telemetry service.
Raises:
-
BijuxError
–If the
PyYAML
library is not installed.
Source code in src/bijux_cli/infra/serializer.py
dumps ¶
dumps(
obj: Any,
*,
fmt: OutputFormat = YAML,
pretty: bool = False,
) -> str
Serializes an object to a YAML string.
Parameters:
-
obj
(Any
) –The object to serialize.
-
fmt
(OutputFormat
, default:YAML
) –The output format. Must be
OutputFormat.YAML
. -
pretty
(bool
, default:False
) –If True, formats the output in block style.
Returns:
-
str
(str
) –The serialized YAML string.
Raises:
-
BijuxError
–If the format is not
OutputFormat.YAML
.
Source code in src/bijux_cli/infra/serializer.py
dumps_bytes ¶
dumps_bytes(
obj: Any,
*,
fmt: OutputFormat = YAML,
pretty: bool = False,
) -> bytes
Serializes an object to YAML bytes.
Parameters:
-
obj
(Any
) –The object to serialize.
-
fmt
(OutputFormat
, default:YAML
) –The output format. Must be
OutputFormat.YAML
. -
pretty
(bool
, default:False
) –If True, formats the output in block style.
Returns:
-
bytes
(bytes
) –The serialized YAML bytes.
Source code in src/bijux_cli/infra/serializer.py
loads ¶
loads(
data: str | bytes,
*,
fmt: OutputFormat = YAML,
pretty: bool = False,
) -> Any
Deserializes YAML data into a Python object.
Parameters:
-
data
(str | bytes
) –The string or bytes to deserialize.
-
fmt
(OutputFormat
, default:YAML
) –The format of the input. Must be
OutputFormat.YAML
. -
pretty
(bool
, default:False
) –A hint that may affect parsing (unused).
Returns:
-
Any
(Any
) –The deserialized object.
Raises:
-
BijuxError
–If the format is not
OutputFormat.YAML
.
Source code in src/bijux_cli/infra/serializer.py
Redacted ¶
Bases: str
A string subclass that redacts its value when printed or serialized.
This is used to wrap sensitive data, such as secrets or API keys, to prevent them from being accidentally exposed in logs or console output.
to_json staticmethod
¶
Provides a JSON-serializable representation for libraries like orjson
.
Returns:
-
str
(str
) –A static string "***" to represent the redacted value.
serializer_for ¶
serializer_for(
fmt: OutputFormat | str, telemetry: TelemetryProtocol
) -> SerializerProtocol[Any]
A factory function that returns a serializer for the given format.
Parameters:
-
fmt
(OutputFormat | str
) –The desired output format.
-
telemetry
(TelemetryProtocol
) –The telemetry service to inject into the serializer.
Returns:
-
SerializerProtocol[Any]
–SerializerProtocol[Any]: A configured serializer instance appropriate for the specified format.
Source code in src/bijux_cli/infra/serializer.py
yaml_dump ¶
Dumps an object to a YAML string using PyYAML.
Parameters:
-
obj
(Any
) –The object to serialize.
-
pretty
(bool
) –If True, formats the output in an indented block style.
Returns:
-
str
(str
) –The serialized YAML string.
Raises:
-
BijuxError
–If the
PyYAML
library is not installed.