API reference¶
The following document outlines betterproto’s api. None of these classes should be extended by the user manually.
Message¶
- class betterproto.Message¶
The base class for protobuf messages, all generated messages will inherit from this. This class registers the message fields which are used by the serializers and parsers to go between the Python, binary and JSON representations of the message.
- bytes(x)
Calls
__bytes__()
.
- bool(x)
Calls
__bool__()
.
- __bool__()¶
True if the Message has any fields with non-default values.
- __bytes__()¶
Get the binary encoded Protobuf representation of this message instance.
- SerializeToString()¶
Get the binary encoded Protobuf representation of this message instance.
Note
This is a method for compatibility with other libraries, you should really use
bytes(x)
.- Returns
The binary encoded Protobuf representation of this message instance
- Return type
- parse(data)¶
Parse the binary encoded Protobuf into this message instance. This returns the instance itself and is therefore assignable and chainable.
- classmethod FromString(data)¶
Parse the binary encoded Protobuf into this message instance. This returns the instance itself and is therefore assignable and chainable.
Note
This is a method for compatibility with other libraries, you should really use
parse()
.
- to_dict(casing=<function camel_case>, include_default_values=False)¶
Returns a JSON serializable dict representation of this object.
- Parameters
casing (
Casing
) – The casing to use for key values. Default isCasing.CAMEL
for compatibility purposes.include_default_values (
bool
) – IfTrue
will include the default values of fields. Default isFalse
. E.g. anint32
field will be included with a value of0
if this is set toTrue
, otherwise this would be ignored.
- Returns
The JSON serializable dict representation of this object.
- Return type
Dict[
str
, Any]
- from_dict(value)¶
Parse the key/value pairs into the current message instance. This returns the instance itself and is therefore assignable and chainable.
- to_json(indent=None)¶
A helper function to parse the message instance into its JSON representation.
This is equivalent to:
json.dumps(message.to_dict(), indent=indent)
- Parameters
indent (Optional[Union[
int
,str
]]) – The indent to pass tojson.dumps()
.- Returns
The JSON representation of the message.
- Return type
- from_json(value)¶
A helper function to return the message instance from its JSON representation. This returns the instance itself and is therefore assignable and chainable.
This is equivalent to:
return message.from_dict(json.loads(value))
- Parameters
value (Union[
str
,bytes
]) – The value to pass tojson.loads()
.- Returns
The initialized message.
- Return type
- betterproto.serialized_on_wire(message)¶
If this message was or should be serialized on the wire. This can be used to detect presence (e.g. optional wrapper message) and is used internally during parsing/serialization.
- Returns
Whether this message was or should be serialized on the wire.
- Return type
Enumerations¶
- class betterproto.Enum¶
The base class for protobuf enumerations, all generated enumerations will inherit from this. Bases
enum.IntEnum
.- classmethod from_string(name)¶
Return the value which corresponds to the string name.
- Parameters
name (
str
) – The name of the enum member to get- Raises
ValueError – The member was not found in the Enum.