Serialization¶
JSON serialization for BACnet values using orjson when available. Install
with pip install bac-py[serialization]. Use serialize() and
deserialize() for round-trip conversion, or to_dict() / from_dict()
on individual constructed types.
External format serialization (JSON, etc.) for BACnet data.
This module provides a pluggable serialization API for converting BACnet data structures to and from external interchange formats.
- class bac_py.serialization.Serializer(*args, **kwargs)[source]¶
Bases:
ProtocolInterface for format-specific serialization backends.
- bac_py.serialization.get_serializer(format='json', **kwargs)[source]¶
Get a serializer instance for the given format.
- Parameters:
- Return type:
- Returns:
A
Serializerinstance.- Raises:
ValueError – If the format is not supported.
ImportError – If the required dependency is not installed.
- bac_py.serialization.json_default(obj)[source]¶
Default handler for serializing BACnet types to JSON.
Use as the default argument to
json.dumps()ororjson.dumps()so that BACnet objects serialize automatically.Handles:
Objects with a
to_dict()method (BitString,ObjectIdentifier,BACnetDate,BACnetTime,StatusFlags, and all other BACnet constructed types).bytesandmemoryview→ hex string.IntEnumsubclasses (ObjectType,PropertyIdentifier, …) →int.
Example:
import json from bac_py import json_default result = await client.read_multiple(...) print(json.dumps(result, default=json_default))
- bac_py.serialization.serialize(obj, format='json', **kwargs)[source]¶
Serialize a BACnet object or dict to the specified format.
Accepts any object with a
to_dict()method, or a plain dict.
JSON Serializer¶
JSON serializer backed by orjson.
- bac_py.serialization.json.json_default(obj)[source]¶
Default handler for serializing BACnet types to JSON.
Use as the default argument to
json.dumps()ororjson.dumps()so that BACnet objects serialize automatically.Handles:
Objects with a
to_dict()method (BitString,ObjectIdentifier,BACnetDate,BACnetTime,StatusFlags, and all other BACnet constructed types).bytesandmemoryview→ hex string.IntEnumsubclasses (ObjectType,PropertyIdentifier, …) →int.
Example:
import json from bac_py import json_default result = await client.read_multiple(...) print(json.dumps(result, default=json_default))
- class bac_py.serialization.json.JsonSerializer(*, pretty=False, sort_keys=False)[source]¶
Bases:
objectJSON serializer using orjson for high-performance encoding.
bytesandmemoryviewvalues are encoded as hex strings. Since JSON has no binary type, deserialization returns them as plain strings — callers must usebytes.fromhex()when round-tripping binary data.- Parameters: