Skip to content

Introduction

Transformer handles transforming to<->from Protobuf format. In most cases from Protobuf<->JSON.

Protobuf Python API

JSON Format

https://googleapis.dev/python/protobuf/4.21.1/google/protobuf/json_format.html

main.py
result = json_format.MessageToDict(
        uplink, 
        use_integers_for_enums=False,
        including_default_value_fields=False, # (1)
        preserving_proto_field_name=True # (2)
        )
  1. Highly recommend setting to False. Read more about Default Values to learn why this is the case.
  2. Highly recommend setting this to True to avoid having the Protobuf library convert .proto fields to cammelCase.

    If False for example battery_soc is printed as batterySoc and could cause downstream issues.

Default Values

I recommend setting including_default_value_fields=False to ensure that downstream systems do not receive misinterpret default values as being valid.

For example, including_default_value_fields=True produces 'battery_soc': 0.0. This could cause issues if downstream consumers of this message are not prepared to interpret these values as defaults.

{'battery_soc': 0.0, 'battery_volts': 3.74, 'humidity': 0.51, 'moisture': 25.0, 'temperature': 19.1, 'device_id': '', 'lat': 0.0, 'lng': 0.0, 'fw': 'NULL'}
It is safer to simple not send those fields at all. Example: including_default_value_fields=False

{'battery_volts': 3.74, 'humidity': 0.51, 'moisture': 25.0, 'temperature': 29.3}

Note

Protobufs have their own default values if not defined in the payload to be encoded. For example, booleans default to False, numeric fields default to 0. More info