semi-experimentally, add fallback code for message_to_json, allowing older protobuf library versions

This commit is contained in:
Ian McEwen
2024-11-15 11:47:17 -07:00
parent fb88ee114c
commit 426795fccd
3 changed files with 728 additions and 766 deletions

View File

@@ -675,5 +675,8 @@ def check_if_newer_version() -> Optional[str]:
def message_to_json(message: Message, multiline: bool=False) -> str:
"""Return protobuf message as JSON. Always print all fields, even when not present in data."""
json = MessageToJson(message, always_print_fields_with_no_presence=True)
try:
json = MessageToJson(message, always_print_fields_with_no_presence=True)
except TypeError:
json = MessageToJson(message, including_default_value_fields=True)
return stripnl(json) if not multiline else json