codekingpro/portable-devtools
114k
1# Protocol Buffers - Google's data interchange format2# Copyright 2008 Google Inc. All rights reserved.3#4# Use of this source code is governed by a BSD-style5# license that can be found in the LICENSE file or at6# https://developers.google.com/open-source/licenses/bsd7 8"""Contains the Nextgen Pythonic Protobuf JSON APIs."""9 10from typing import Optional, Type11 12from google.protobuf.message import Message13from google.protobuf.descriptor_pool import DescriptorPool14from google.protobuf import json_format15 16def serialize(17 message: Message,18 always_print_fields_with_no_presence: bool=False,19 preserving_proto_field_name: bool=False,20 use_integers_for_enums: bool=False,21 descriptor_pool: Optional[DescriptorPool]=None,22 float_precision: int=None,23) -> dict:24 """Converts protobuf message to a dictionary.25 26 When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.27 28 Args:29 message: The protocol buffers message instance to serialize.30 always_print_fields_with_no_presence: If True, fields without31 presence (implicit presence scalars, repeated fields, and map fields) will32 always be serialized. Any field that supports presence is not affected by33 this option (including singular message fields and oneof fields).34 preserving_proto_field_name: If True, use the original proto field names as35 defined in the .proto file. If False, convert the field names to36 lowerCamelCase.37 use_integers_for_enums: If true, print integers instead of enum names.38 descriptor_pool: A Descriptor Pool for resolving types. If None use the39 default.40 float_precision: If set, use this to specify float field valid digits.41 42 Returns:43 A dict representation of the protocol buffer message.44 """45 return json_format.MessageToDict(46 message,47 always_print_fields_with_no_presence=always_print_fields_with_no_presence,48 preserving_proto_field_name=preserving_proto_field_name,49 use_integers_for_enums=use_integers_for_enums,50 float_precision=float_precision,51 )52 53def parse(54 message_class: Type[Message],55 js_dict: dict,56 ignore_unknown_fields: bool=False,57 descriptor_pool: Optional[DescriptorPool]=None,58 max_recursion_depth: int=10059) -> Message:60 """Parses a JSON dictionary representation into a message.61 62 Args:63 message_class: The message meta class.64 js_dict: Dict representation of a JSON message.65 ignore_unknown_fields: If True, do not raise errors for unknown fields.66 descriptor_pool: A Descriptor Pool for resolving types. If None use the67 default.68 max_recursion_depth: max recursion depth of JSON message to be deserialized.69 JSON messages over this depth will fail to be deserialized. Default value70 is 100.71 72 Returns:73 A new message passed from json_dict.74 """75 new_message = message_class()76 json_format.ParseDict(77 js_dict=js_dict,78 message=new_message,79 ignore_unknown_fields=ignore_unknown_fields,80 descriptor_pool=descriptor_pool,81 max_recursion_depth=max_recursion_depth,82 )83 return new_message84 