Skip to content

Commit

Permalink
Preserve proto field names (#228)
Browse files Browse the repository at this point in the history
Co-authored-by: Kyle Coble <kyle@farm-ng.com>
  • Loading branch information
guilhermedemouraa and Hackerman342 authored May 2, 2024
1 parent 4766425 commit 34c0823
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions py/farm_ng/core/events_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
]


def proto_to_json_file(file_path: str | Path, proto_message: Message) -> bool:
def proto_to_json_file(
file_path: str | Path,
proto_message: Message,
preserving_proto_field_name=False,
) -> bool:
"""Write a proto Message to a JSON file. The parent directory of the file must exist.
Args:
file_path (str | Path): The path to the JSON file to create / overwrite.
proto_message (Message): The proto message to write to the JSON file.
preserving_proto_field_name (bool): See MessageToJson in https://googleapis.dev/python/protobuf/latest/google/protobuf/json_format.html
Returns:
True if the file was written successfully, False otherwise.
Expand All @@ -43,7 +48,12 @@ def proto_to_json_file(file_path: str | Path, proto_message: Message) -> bool:
return False

with Path(file_path).open("w", encoding="utf-8") as file:
file.write(MessageToJson(proto_message))
file.write(
MessageToJson(
proto_message,
preserving_proto_field_name=preserving_proto_field_name,
),
)
file.write("\n")

return True
Expand Down

0 comments on commit 34c0823

Please sign in to comment.