@@ -72,6 +72,7 @@ class ParseError(Error):
7272
7373class EnumStringValueParseError (ParseError ):
7474 """Thrown if unknown string enum value is encountered.
75+
7576 This exception is suppressed if ignore_unknown_fields is set.
7677 """
7778
@@ -91,10 +92,10 @@ def MessageToJson(
9192
9293 Args:
9394 message: The protocol buffers message instance to serialize.
94- always_print_fields_with_no_presence: If True, fields without
95- presence (implicit presence scalars, repeated fields, and map fields) will
96- always be serialized. Any field that supports presence is not affected by
97- this option (including singular message fields and oneof fields).
95+ always_print_fields_with_no_presence: If True, fields without presence
96+ (implicit presence scalars, repeated fields, and map fields) will always
97+ be serialized. Any field that supports presence is not affected by this
98+ option (including singular message fields and oneof fields).
9899 preserving_proto_field_name: If True, use the original proto field names as
99100 defined in the .proto file. If False, convert the field names to
100101 lowerCamelCase.
@@ -105,7 +106,8 @@ def MessageToJson(
105106 use_integers_for_enums: If true, print integers instead of enum names.
106107 descriptor_pool: A Descriptor Pool for resolving types. If None use the
107108 default.
108- float_precision: If set, use this to specify float field valid digits.
109+ float_precision: Deprecated. If set, use this to specify float field valid
110+ digits.
109111 ensure_ascii: If True, strings with non-ASCII characters are escaped. If
110112 False, Unicode strings are returned unchanged.
111113
@@ -117,7 +119,7 @@ def MessageToJson(
117119 use_integers_for_enums ,
118120 descriptor_pool ,
119121 float_precision ,
120- always_print_fields_with_no_presence
122+ always_print_fields_with_no_presence ,
121123 )
122124 return printer .ToJsonString (message , indent , sort_keys , ensure_ascii )
123125
@@ -136,17 +138,18 @@ def MessageToDict(
136138
137139 Args:
138140 message: The protocol buffers message instance to serialize.
139- always_print_fields_with_no_presence: If True, fields without
140- presence (implicit presence scalars, repeated fields, and map fields) will
141- always be serialized. Any field that supports presence is not affected by
142- this option (including singular message fields and oneof fields).
141+ always_print_fields_with_no_presence: If True, fields without presence
142+ (implicit presence scalars, repeated fields, and map fields) will always
143+ be serialized. Any field that supports presence is not affected by this
144+ option (including singular message fields and oneof fields).
143145 preserving_proto_field_name: If True, use the original proto field names as
144146 defined in the .proto file. If False, convert the field names to
145147 lowerCamelCase.
146148 use_integers_for_enums: If true, print integers instead of enum names.
147149 descriptor_pool: A Descriptor Pool for resolving types. If None use the
148150 default.
149- float_precision: If set, use this to specify float field valid digits.
151+ float_precision: Deprecated. If set, use this to specify float field valid
152+ digits.
150153
151154 Returns:
152155 A dict representation of the protocol buffer message.
@@ -251,10 +254,7 @@ def _RegularMessageToJsonObject(self, message, js):
251254
252255 # always_print_fields_with_no_presence doesn't apply to
253256 # any field which supports presence.
254- if (
255- self .always_print_fields_with_no_presence
256- and field .has_presence
257- ):
257+ if self .always_print_fields_with_no_presence and field .has_presence :
258258 continue
259259
260260 if self .preserving_proto_field_name :
@@ -674,7 +674,8 @@ def _ConvertFieldValuePair(self, js, message, path):
674674 )
675675 )
676676 self ._ConvertAndAppendScalar (
677- message , field , item , '{0}.{1}[{2}]' .format (path , name , index ))
677+ message , field , item , '{0}.{1}[{2}]' .format (path , name , index )
678+ )
678679 elif field .cpp_type == descriptor .FieldDescriptor .CPPTYPE_MESSAGE :
679680 if field .is_extension :
680681 sub_message = message .Extensions [field ]
@@ -684,9 +685,13 @@ def _ConvertFieldValuePair(self, js, message, path):
684685 self .ConvertMessage (value , sub_message , '{0}.{1}' .format (path , name ))
685686 else :
686687 if field .is_extension :
687- self ._ConvertAndSetScalarExtension (message , field , value , '{0}.{1}' .format (path , name ))
688+ self ._ConvertAndSetScalarExtension (
689+ message , field , value , '{0}.{1}' .format (path , name )
690+ )
688691 else :
689- self ._ConvertAndSetScalar (message , field , value , '{0}.{1}' .format (path , name ))
692+ self ._ConvertAndSetScalar (
693+ message , field , value , '{0}.{1}' .format (path , name )
694+ )
690695 except ParseError as e :
691696 if field and field .containing_oneof is None :
692697 raise ParseError (
@@ -801,7 +806,9 @@ def _ConvertStructMessage(self, value, message, path):
801806 def _ConvertWrapperMessage (self , value , message , path ):
802807 """Convert a JSON representation into Wrapper message."""
803808 field = message .DESCRIPTOR .fields_by_name ['value' ]
804- self ._ConvertAndSetScalar (message , field , value , path = '{0}.value' .format (path ))
809+ self ._ConvertAndSetScalar (
810+ message , field , value , path = '{0}.value' .format (path )
811+ )
805812
806813 def _ConvertMapFieldValue (self , value , message , field , path ):
807814 """Convert map field value for a message map field.
@@ -839,13 +846,17 @@ def _ConvertMapFieldValue(self, value, message, field, path):
839846 field ,
840847 key_value ,
841848 value [key ],
842- path = '{0}[{1}]' .format (path , key_value ))
849+ path = '{0}[{1}]' .format (path , key_value ),
850+ )
843851
844- def _ConvertAndSetScalarExtension (self , message , extension_field , js_value , path ):
852+ def _ConvertAndSetScalarExtension (
853+ self , message , extension_field , js_value , path
854+ ):
845855 """Convert scalar from js_value and assign it to message.Extensions[extension_field]."""
846856 try :
847857 message .Extensions [extension_field ] = _ConvertScalarFieldValue (
848- js_value , extension_field , path )
858+ js_value , extension_field , path
859+ )
849860 except EnumStringValueParseError :
850861 if not self .ignore_unknown_fields :
851862 raise
@@ -854,9 +865,8 @@ def _ConvertAndSetScalar(self, message, field, js_value, path):
854865 """Convert scalar from js_value and assign it to message.field."""
855866 try :
856867 setattr (
857- message ,
858- field .name ,
859- _ConvertScalarFieldValue (js_value , field , path ))
868+ message , field .name , _ConvertScalarFieldValue (js_value , field , path )
869+ )
860870 except EnumStringValueParseError :
861871 if not self .ignore_unknown_fields :
862872 raise
@@ -865,16 +875,23 @@ def _ConvertAndAppendScalar(self, message, repeated_field, js_value, path):
865875 """Convert scalar from js_value and append it to message.repeated_field."""
866876 try :
867877 getattr (message , repeated_field .name ).append (
868- _ConvertScalarFieldValue (js_value , repeated_field , path ))
878+ _ConvertScalarFieldValue (js_value , repeated_field , path )
879+ )
869880 except EnumStringValueParseError :
870881 if not self .ignore_unknown_fields :
871882 raise
872883
873- def _ConvertAndSetScalarToMapKey (self , message , map_field , converted_key , js_value , path ):
884+ def _ConvertAndSetScalarToMapKey (
885+ self , message , map_field , converted_key , js_value , path
886+ ):
874887 """Convert scalar from 'js_value' and add it to message.map_field[converted_key]."""
875888 try :
876- getattr (message , map_field .name )[converted_key ] = _ConvertScalarFieldValue (
877- js_value , map_field .message_type .fields_by_name ['value' ], path ,
889+ getattr (message , map_field .name )[converted_key ] = (
890+ _ConvertScalarFieldValue (
891+ js_value ,
892+ map_field .message_type .fields_by_name ['value' ],
893+ path ,
894+ )
878895 )
879896 except EnumStringValueParseError :
880897 if not self .ignore_unknown_fields :
0 commit comments