@@ -72,6 +72,7 @@ class ParseError(Error):
72
72
73
73
class EnumStringValueParseError (ParseError ):
74
74
"""Thrown if unknown string enum value is encountered.
75
+
75
76
This exception is suppressed if ignore_unknown_fields is set.
76
77
"""
77
78
@@ -91,10 +92,10 @@ def MessageToJson(
91
92
92
93
Args:
93
94
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).
98
99
preserving_proto_field_name: If True, use the original proto field names as
99
100
defined in the .proto file. If False, convert the field names to
100
101
lowerCamelCase.
@@ -105,7 +106,8 @@ def MessageToJson(
105
106
use_integers_for_enums: If true, print integers instead of enum names.
106
107
descriptor_pool: A Descriptor Pool for resolving types. If None use the
107
108
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.
109
111
ensure_ascii: If True, strings with non-ASCII characters are escaped. If
110
112
False, Unicode strings are returned unchanged.
111
113
@@ -117,7 +119,7 @@ def MessageToJson(
117
119
use_integers_for_enums ,
118
120
descriptor_pool ,
119
121
float_precision ,
120
- always_print_fields_with_no_presence
122
+ always_print_fields_with_no_presence ,
121
123
)
122
124
return printer .ToJsonString (message , indent , sort_keys , ensure_ascii )
123
125
@@ -136,17 +138,18 @@ def MessageToDict(
136
138
137
139
Args:
138
140
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).
143
145
preserving_proto_field_name: If True, use the original proto field names as
144
146
defined in the .proto file. If False, convert the field names to
145
147
lowerCamelCase.
146
148
use_integers_for_enums: If true, print integers instead of enum names.
147
149
descriptor_pool: A Descriptor Pool for resolving types. If None use the
148
150
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.
150
153
151
154
Returns:
152
155
A dict representation of the protocol buffer message.
@@ -251,10 +254,7 @@ def _RegularMessageToJsonObject(self, message, js):
251
254
252
255
# always_print_fields_with_no_presence doesn't apply to
253
256
# 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 :
258
258
continue
259
259
260
260
if self .preserving_proto_field_name :
@@ -674,7 +674,8 @@ def _ConvertFieldValuePair(self, js, message, path):
674
674
)
675
675
)
676
676
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
+ )
678
679
elif field .cpp_type == descriptor .FieldDescriptor .CPPTYPE_MESSAGE :
679
680
if field .is_extension :
680
681
sub_message = message .Extensions [field ]
@@ -684,9 +685,13 @@ def _ConvertFieldValuePair(self, js, message, path):
684
685
self .ConvertMessage (value , sub_message , '{0}.{1}' .format (path , name ))
685
686
else :
686
687
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
+ )
688
691
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
+ )
690
695
except ParseError as e :
691
696
if field and field .containing_oneof is None :
692
697
raise ParseError (
@@ -801,7 +806,9 @@ def _ConvertStructMessage(self, value, message, path):
801
806
def _ConvertWrapperMessage (self , value , message , path ):
802
807
"""Convert a JSON representation into Wrapper message."""
803
808
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
+ )
805
812
806
813
def _ConvertMapFieldValue (self , value , message , field , path ):
807
814
"""Convert map field value for a message map field.
@@ -839,13 +846,17 @@ def _ConvertMapFieldValue(self, value, message, field, path):
839
846
field ,
840
847
key_value ,
841
848
value [key ],
842
- path = '{0}[{1}]' .format (path , key_value ))
849
+ path = '{0}[{1}]' .format (path , key_value ),
850
+ )
843
851
844
- def _ConvertAndSetScalarExtension (self , message , extension_field , js_value , path ):
852
+ def _ConvertAndSetScalarExtension (
853
+ self , message , extension_field , js_value , path
854
+ ):
845
855
"""Convert scalar from js_value and assign it to message.Extensions[extension_field]."""
846
856
try :
847
857
message .Extensions [extension_field ] = _ConvertScalarFieldValue (
848
- js_value , extension_field , path )
858
+ js_value , extension_field , path
859
+ )
849
860
except EnumStringValueParseError :
850
861
if not self .ignore_unknown_fields :
851
862
raise
@@ -854,9 +865,8 @@ def _ConvertAndSetScalar(self, message, field, js_value, path):
854
865
"""Convert scalar from js_value and assign it to message.field."""
855
866
try :
856
867
setattr (
857
- message ,
858
- field .name ,
859
- _ConvertScalarFieldValue (js_value , field , path ))
868
+ message , field .name , _ConvertScalarFieldValue (js_value , field , path )
869
+ )
860
870
except EnumStringValueParseError :
861
871
if not self .ignore_unknown_fields :
862
872
raise
@@ -865,16 +875,23 @@ def _ConvertAndAppendScalar(self, message, repeated_field, js_value, path):
865
875
"""Convert scalar from js_value and append it to message.repeated_field."""
866
876
try :
867
877
getattr (message , repeated_field .name ).append (
868
- _ConvertScalarFieldValue (js_value , repeated_field , path ))
878
+ _ConvertScalarFieldValue (js_value , repeated_field , path )
879
+ )
869
880
except EnumStringValueParseError :
870
881
if not self .ignore_unknown_fields :
871
882
raise
872
883
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
+ ):
874
887
"""Convert scalar from 'js_value' and add it to message.map_field[converted_key]."""
875
888
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
+ )
878
895
)
879
896
except EnumStringValueParseError :
880
897
if not self .ignore_unknown_fields :
0 commit comments