proto_json.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. float_precision: int=None,
  19. ) -> dict:
  20. """Converts protobuf message to a dictionary.
  21. When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.
  22. Args:
  23. message: The protocol buffers message instance to serialize.
  24. always_print_fields_with_no_presence: If True, fields without
  25. presence (implicit presence scalars, repeated fields, and map fields) will
  26. always be serialized. Any field that supports presence is not affected by
  27. this option (including singular message fields and oneof fields).
  28. preserving_proto_field_name: If True, use the original proto field names as
  29. defined in the .proto file. If False, convert the field names to
  30. lowerCamelCase.
  31. use_integers_for_enums: If true, print integers instead of enum names.
  32. descriptor_pool: A Descriptor Pool for resolving types. If None use the
  33. default.
  34. float_precision: If set, use this to specify float field valid digits.
  35. Returns:
  36. A dict representation of the protocol buffer message.
  37. """
  38. return json_format.MessageToDict(
  39. message,
  40. always_print_fields_with_no_presence=always_print_fields_with_no_presence,
  41. preserving_proto_field_name=preserving_proto_field_name,
  42. use_integers_for_enums=use_integers_for_enums,
  43. float_precision=float_precision,
  44. )
  45. def parse(
  46. message_class: Type[Message],
  47. js_dict: dict,
  48. ignore_unknown_fields: bool=False,
  49. descriptor_pool: Optional[DescriptorPool]=None,
  50. max_recursion_depth: int=100
  51. ) -> Message:
  52. """Parses a JSON dictionary representation into a message.
  53. Args:
  54. message_class: The message meta class.
  55. js_dict: Dict representation of a JSON message.
  56. ignore_unknown_fields: If True, do not raise errors for unknown fields.
  57. descriptor_pool: A Descriptor Pool for resolving types. If None use the
  58. default.
  59. max_recursion_depth: max recursion depth of JSON message to be deserialized.
  60. JSON messages over this depth will fail to be deserialized. Default value
  61. is 100.
  62. Returns:
  63. A new message passed from json_dict.
  64. """
  65. new_message = message_class()
  66. json_format.ParseDict(
  67. js_dict=js_dict,
  68. message=new_message,
  69. ignore_unknown_fields=ignore_unknown_fields,
  70. descriptor_pool=descriptor_pool,
  71. max_recursion_depth=max_recursion_depth,
  72. )
  73. return new_message