protobuf.txtpb
Protobuf text format (.txtpb) serialization.
The text format is a plain-text syntax used for debugging, tests, and config files.
Examples:
from protobuf.txtpb import message_from_text, message_to_text
text = message_to_text(user)
user = message_from_text(User, text)
merge_from_text
Parse a protobuf text format string, merging fields into an existing message.
Merge rules by field kind:
- Scalar and enum: the existing value is overwritten.
- Message: recursively merged if already present, otherwise set.
- Repeated: elements are appended.
- Map: entries are added; existing keys are overwritten.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
The message instance to merge into. |
required |
text
|
str | bytes | bytearray
|
The text data to parse. |
required |
registry
|
Registry | None
|
Required to read |
None
|
ignore_unknown_fields
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the text cannot be parsed into the message. This includes encountering an unknown field, unless ignore_unknown_fields is set. |
RecursionError
|
If messages are nested deeper than the supported limit. |
message_to_text
Serialize a message to the protobuf text format.
Unlike standard serialization, unset required fields will not raise an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
Message
|
The message to serialize. |
required |
registry
|
Registry | None
|
A registry for resolving google.protobuf.Any messages
and extensions. Without it, an Any is written as its raw
|
None
|
print_unknown_fields
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
str
|
The message in protobuf text format. |
message_from_text
Create a new message by parsing the protobuf text format.
To merge into an existing message, use merge_from_text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message_type
|
type[T]
|
The type of message to create. |
required |
text
|
str | bytes | bytearray
|
The text data to parse. |
required |
registry
|
Registry | None
|
Required to read |
None
|
ignore_unknown_fields
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the text cannot be parsed into the message. This includes encountering an unknown field, unless ignore_unknown_fields is set. |
RecursionError
|
If messages are nested deeper than the supported limit. |