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: Protocol

Interface for format-specific serialization backends.

encode(data)[source]

Encode a dict to the target format.

Return type:

bytes

Parameters:

data (dict[str, Any])

decode(raw)[source]

Decode bytes in the target format to a dict.

Return type:

dict[str, Any]

Parameters:

raw (bytes)

property content_type: str

MIME type for the output format (e.g. ‘application/json’).

bac_py.serialization.deserialize(raw, format='json')[source]

Deserialize bytes to a dict.

Parameters:
  • raw (bytes) – Bytes to deserialize.

  • format (str (default: 'json')) – Input format (default "json").

Return type:

dict[str, Any]

Returns:

Deserialized dict.

bac_py.serialization.get_serializer(format='json', **kwargs)[source]

Get a serializer instance for the given format.

Parameters:
  • format (str (default: 'json')) – Output format. Currently supported: "json".

  • kwargs (Any) – Format-specific options passed to the serializer constructor.

Return type:

Serializer

Returns:

A Serializer instance.

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() or orjson.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).

  • bytes and memoryview → hex string.

  • IntEnum subclasses (ObjectType, PropertyIdentifier, …) → int.

Example:

import json
from bac_py import json_default

result = await client.read_multiple(...)
print(json.dumps(result, default=json_default))
Parameters:

obj (object) – The object to convert.

Return type:

object

Returns:

A JSON-serializable representation.

Raises:

TypeError – If obj is not a recognised type.

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.

Parameters:
  • obj (Any) – Object to serialize.

  • format (str (default: 'json')) – Output format (default "json").

  • kwargs (Any) – Format-specific options.

Return type:

bytes

Returns:

Serialized bytes.

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() or orjson.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).

  • bytes and memoryview → hex string.

  • IntEnum subclasses (ObjectType, PropertyIdentifier, …) → int.

Example:

import json
from bac_py import json_default

result = await client.read_multiple(...)
print(json.dumps(result, default=json_default))
Parameters:

obj (object) – The object to convert.

Return type:

object

Returns:

A JSON-serializable representation.

Raises:

TypeError – If obj is not a recognised type.

class bac_py.serialization.json.JsonSerializer(*, pretty=False, sort_keys=False)[source]

Bases: object

JSON serializer using orjson for high-performance encoding.

bytes and memoryview values are encoded as hex strings. Since JSON has no binary type, deserialization returns them as plain strings — callers must use bytes.fromhex() when round-tripping binary data.

Parameters:
  • pretty (bool (default: False)) – Indent output with 2 spaces.

  • sort_keys (bool (default: False)) – Sort dict keys alphabetically.

encode(data)[source]

Encode a dict to JSON bytes.

Return type:

bytes

Parameters:

data (dict[str, Any])

decode(raw)[source]

Decode JSON bytes to a dict.

Return type:

dict[str, Any]

Parameters:

raw (bytes)

property content_type: str

MIME content type for JSON.