proto_json.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Contains the Nextgen Pythonic Protobuf JSON APIs."""
  8. from typing import Optional, Type
  9. from google.protobuf.message import Message
  10. from google.protobuf.descriptor_pool import DescriptorPool
  11. from google.protobuf import json_format
  12. def serialize(
  13. message: Message,
  14. always_print_fields_with_no_presence: bool=False,
  15. preserving_proto_field_name: bool=False,
  16. use_integers_for_enums: bool=False,
  17. descriptor_pool: Optional[DescriptorPool]=None,
  18. ) -> dict:
  19. """Converts protobuf message to a dictionary.
  20. When the dictionary is encoded to JSON, it conforms to ProtoJSON spec.
  21. Args:
  22. message: The protocol buffers message instance to serialize.
  23. always_print_fields_with_no_presence: If True, fields without presence
  24. (implicit presence scalars, repeated fields, and map fields) will always
  25. be serialized. Any field that supports presence is not affected by this
  26. option (including singular message fields and oneof fields).
  27. preserving_proto_field_name: If True, use the original proto field names as
  28. defined in the .proto file. If False, convert the field names to
  29. lowerCamelCase.
  30. use_integers_for_enums: If true, print integers instead of enum names.
  31. descriptor_pool: A Descriptor Pool for resolving types. If None use the
  32. default.
  33. Returns:
  34. A dict representation of the protocol buffer message.
  35. """
  36. return json_format.MessageToDict(
  37. message,
  38. always_print_fields_with_no_presence=always_print_fields_with_no_presence,
  39. preserving_proto_field_name=preserving_proto_field_name,
  40. use_integers_for_enums=use_integers_for_enums,
  41. )
  42. def parse(
  43. message_class: Type[Message],
  44. js_dict: dict,
  45. ignore_unknown_fields: bool=False,
  46. descriptor_pool: Optional[DescriptorPool]=None,
  47. max_recursion_depth: int=100
  48. ) -> Message:
  49. """Parses a JSON dictionary representation into a message.
  50. Args:
  51. message_class: The message meta class.
  52. js_dict: Dict representation of a JSON message.
  53. ignore_unknown_fields: If True, do not raise errors for unknown fields.
  54. descriptor_pool: A Descriptor Pool for resolving types. If None use the
  55. default.
  56. max_recursion_depth: max recursion depth of JSON message to be deserialized.
  57. JSON messages over this depth will fail to be deserialized. Default value
  58. is 100.
  59. Returns:
  60. A new message passed from json_dict.
  61. """
  62. new_message = message_class()
  63. json_format.ParseDict(
  64. js_dict=js_dict,
  65. message=new_message,
  66. ignore_unknown_fields=ignore_unknown_fields,
  67. descriptor_pool=descriptor_pool,
  68. max_recursion_depth=max_recursion_depth,
  69. )
  70. return new_message